import os
import sys
import logging
import ConfigParser

import cpuinfo
import cv2
import vlc


if "X86_64" == cpuinfo.get_cpu_info()["arch"]:
    import getchar as interface


config = ConfigParser.ConfigParser()
face_cascade = None
video = None
stay_alive = True   # Keep the program running?
speaking = False    # Am I talking when I detect someone else prescence


def initialize():
    """Initialize program"""
    global video
    global face_cascade
    config.readfp(open('../config.ini'))
    face_cascade = cv2.CascadeClassifier("../"
            + config.get('DEFAULT','facexml'))
    video = cv2.VideoCapture(0)

def get_frame():
    """Capture frames from camera"""
    success, frame = video.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.1,
            minNeighbors=5,
            minSize=(30, 30),
            flags=cv2.cv.CV_HAAR_SCALE_IMAGE
            )

    ## Draw a rectangle around faces
    for (x, y, w, h) in faces:
       cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    #cv2.imshow('Video', frame)
    return frame


def jpeg_encode(frame):
    success, jpeg = cv2.imencode('.jpg', frame)

    return jpeg.tobytes()


def main():
    """Monroe waits for external sensors input and greet people"""

    log_init()                      # Initialize loging system
    logging.info("Starting")
    initialize()                    # Initilize global variables
    read = interface.read_input()

    while stay_alive:
        # Feel
        # Watch
        cv2.imshow('Video', get_frame())
        if cv2.waitKey(1) & 0xFF == ord('q'):
            exit(stay_alive)
            break

def exit(flag):
    """Exit the app"""
    if (stay_alive != True):
        video.release()
        cv2.destroyAllWindows()
        logging.info("Bye!")
        sys.exit(0)


def log_init():
    """Initialize log"""
    logging.basicConfig(filename="monroe.log", level=logging.DEBUG)




if __name__ == "__main__":
    main()

@@ -240,16 +240,50 @@
  = None%0A
+vlc_instance = None%0Aplayer = None%0A
 stay_ali
@@ -460,16 +460,58 @@
 l video%0A
+    global vlc_instance%0A    global player%0A
     glob
@@ -693,16 +693,337 @@
 ture(0)%0A
+    # Create basic VLC instance%0A    vlc_instance = vlc.Instance()%0A    # Create VLC player%0A    player = vlc_instance.media_player_new()%0A    # TODO Make it load a playlist and set it up to play random%0A    promos = vlc_instance.media_new(%22file://%22%0A            + os.environ%5B%22HOME%22%5D + %22/01.ogg%22)%0A    player.set_media(promos)%0A%0A
 %0Adef get
@@ -1500,40 +1500,8 @@
 2)%0A%0A
-    #cv2.imshow('Video', frame)%0A
     
@@ -1695,16 +1695,37 @@
 ople%22%22%22%0A
+    global stay_alive
 %0A    log
@@ -1878,16 +1878,17 @@
 les%0A    
+#
 read = i
@@ -1908,16 +1908,51 @@
 _input()
+ TODO replace it for something else
 %0A%0A    wh
@@ -2047,18 +2047,92 @@
         
-if
+# Wait for input, TODO make it more generic loose from cv2%0A        r_input =
  cv2.wai
@@ -2145,16 +2145,35 @@
 ) & 0xFF
+%0A        if r_input
  == ord(
@@ -2178,16 +2178,47 @@
 d('q'):%0A
+            stay_alive = False%0A
         
@@ -2256,16 +2256,260 @@
   break%0A
+        elif r_input == ord('1'):%0A            shout_out()%0A%0A%0Adef shout_out(snd_file=None):%0A    %22%22%22Say something%22%22%22%0A    if (snd_file != None):%0A        speech = vlc_instance.media_new(snd_file)%0A        player.set_media(speech)%0A%0A    player.play()%0A%0A
 %0Adef exi

UPDATE sight and promo capable