from keras.models import load_model
from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
import random
import os

face_classifier = cv2.CascadeClassifier(r'C:\Users\pravesh\Desktop\Live project\haarcascade_frontalface_default.xml')
classifier = load_model(r'C:\Users\pravesh\Desktop\Live project\Emotion_little_vggi.h5')

class_labels = ['Angry', 'Happy', 'Neutral', 'Sad', 'Surprise']

cap = cv2.VideoCapture(0)

value = 1
while value:
    # Grab a single frame of video
    ret, frame = cap.read()
    labels = []
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray, 1.3, 5)

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_gray = cv2.resize(roi_gray, (48, 48), interpolation=cv2.INTER_AREA)
        # rect,face,image = face_detector(frame)

        if np.sum([roi_gray]) != 0:
            roi = roi_gray.astype('float') / 255.0
            roi = img_to_array(roi)
            roi = np.expand_dims(roi, axis=0)

            # make a prediction on the ROI, then lookup the class

            preds = classifier.predict(roi)[0]
            label = class_labels[preds.argmax()]
            label_position = (x, y)
            cv2.putText(frame, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
            # print(label)
            if label == class_labels[preds.argmax()]:
                if class_labels[preds.argmax()] == 'Angry':
                    music_dir = r"C:\Users\pravesh\Desktop\Program2\song\Angry"  # make one folder which only contains music and give the full path of that folder here
                    songs = listdir(music_dir)
                    # print(songs)
                    os.startfile(r"C:\Users\pravesh\Desktop\Program2\song\Angry")

                    print(len(songs))
                    # if you will see an "list index out of range" error then make sure that
                    # number of songs is less than that of random number generated by function.
                    ak = random.randint(0, len(songs) - 1)  # for random song which is more attractive
                    print(ak)
                    os.startfile(os.path.join(music_dir, songs[ak]))
                    print(songs[ak])
                    value = 0
                elif class_labels[preds.argmax()] == 'Happy':
                    music_dir = r"C:\Users\pravesh\Desktop\Program2\song\Happy"  # make one folder which only contains music and give the full path of that folder here
                    songs = listdir(music_dir)
                    # print(songs)
                    os.startfile(r"C:\Users\pravesh\Desktop\Program2\song\Happy")

                    print(len(songs))
                    # if you will see an "list index out of range" error then make sure that
                    # number of songs is less than that of random number generated by function.
                    ak = random.randint(0, len(songs) - 1)  # for random song which is more attractive
                    print(ak)
                    os.startfile(os.path.join(music_dir, songs[ak]))
                    print(songs[ak])
                    value = 0
                elif class_labels[preds.argmax()] == 'Neutral':
                    music_dir = r"C:\Users\pravesh\Desktop\Program2\song\Neutral"  # make one folder which only contains music and give the full path of that folder here
                    songs = listdir(music_dir)
                    # print(songs)
                    os.startfile(r"C:\Users\pravesh\Desktop\Program2\song\Neutral")

                    print(len(songs))
                    # if you will see an "list index out of range" error then make sure that
                    # number of songs is less than that of random number generated by function.
                    ak = random.randint(0, len(songs) - 1)  # for random song which is more attractive
                    print(ak)
                    os.startfile(os.path.join(music_dir, songs[ak]))
                    print(songs[ak])
                    value = 0
                elif class_labels[preds.argmax()] == 'Sad':
                    music_dir = r"C:\Users\pravesh\Desktop\Program2\song\Sad"  # make one folder which only contains music and give the full path of that folder here
                    songs = listdir(music_dir)
                    # print(songs)
                    os.startfile(r"C:\Users\pravesh\Desktop\Program2\song\Sad")

                    print(len(songs))
                    # if you will see an "list index out of range" error then make sure that
                    # number of songs is less than that of random number generated by function.
                    ak = random.randint(0, len(songs) - 1)  # for random song which is more attractive
                    print(ak)
                    os.startfile(os.path.join(music_dir, songs[ak]))
                    print(songs[ak])
                    value = 0
                elif class_labels[preds.argmax()] == 'Surprise':
                    music_dir = r"C:\Users\pravesh\Desktop\Program2\song\Surprise"  # make one folder which only contains music and give the full path of that folder here
                    songs = listdir(music_dir)
                    # print(songs)
                    os.startfile(r"C:\Users\pravesh\Desktop\Program2\song\Surprise")

                    print(len(songs))
                    # if you will see an "list index out of range" error then make sure that
                    # number of songs is less than that of random number generated by function.
                    ak = random.randint(0, len(songs) - 1)  # for random song which is more attractive
                    print(ak)
                    os.startfile(os.path.join(music_dir, songs[ak]))
                    print(songs[ak])
                    value = 0
            value = 0


        else:
            cv2.putText(frame, 'No Face Found', (20, 60), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
            value = 0
    cv2.imshow('Emotion Detector', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


























