#Use your camera for processing the video. Stop by pressing Q
import cv2
import imageProcess as ip
import faceAnalysis as fa
import random
from config import emotions

cap = cv2.VideoCapture(0)   #0 means we capture the first camera, your webcam probably
score = 0
N = 15

def smileyRandom(emotionToDodge):
    emotionNbr = random.randrange(0,6)
    emotion = emotions[emotionNbr]
    if emotion == emotionToDodge: return smileyRandom(emotion)
    smileyImagePath = "data/smileys/"+emotion+".png"
    smiley = cv2.imread(smileyImagePath)
    return smiley, emotion



ismain = __name__ == "__main__"
if ismain:

    smiley, emotion = smileyRandom("")

    while cap.isOpened():		 #or while 1. cap.isOpened() is false if there is a problem
        ret, frame = cap.read()  #Read next video frame, stop if frame not well read
        if not ret: break
        
        emotionsList = ip.imageProcess(frame, returnEmotion=True)
        
        if emotion in emotionsList: #If emotion recognized, increase score, reset smiley to mimick and write "GG!"
            score += 1
            cv2.putText(smiley, "Emotion reconnue !", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
            cv2.imshow("Smiley", smiley)         
            smiley, emotion = smileyRandom(emotion)


        cv2.imshow("Caméra", frame)  			#Show you making emotional faces
        cv2.putText(smiley, "Score: "+str(score), (40,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
        cv2.imshow("Smiley", smiley)            #Show the smiley to mimic

        if cv2.waitKey(1) & 0xFF == ord('q'):			#If you press Q, stop the while and so the capture
            break   

        elif cv2.waitKey(1) & 0xFF == ord('p'):			#If you press P, pass the smiley but lower your score
            score -= 1
            smiley, emotion = smileyRandom(emotion)


    cap.release()
    cv2.destroyAllWindows()