import cv2
import numpy as np
from os import listdir
from os.path import isfile,join
import random
import os
def face_detector(img,size=0.5):
    
    gray = cv2.cvtColor(img ,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray,1.3,5)
    
    if faces ==():
        return img,[]
    for(x,y,w,h) in faces :
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),2)
        roi=img[y:y+h,x:x+w]
        roi=cv2.resize(roi,(200,200))
        
    return img,roi
data_path='C:/Users/pravesh/Desktop/Program2/face/'
onlyfiles=[f for f in listdir(data_path) if isfile(join(data_path,f))]
training_data,labels=[],[]
for i,files in enumerate(onlyfiles):
    image_path = data_path+onlyfiles[i]
    images=cv2.imread(image_path,cv2.IMREAD_GRAYSCALE)
    
    training_data.append(np.asarray(images,dtype=np.uint8))
    labels.append(i)
    
labels= np.asarray(labels,dtype=np.int32)
model=cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(training_data),np.asarray(labels))
print("Model Traing Complete")
face_classifier=cv2.CascadeClassifier("C:/Users/pravesh/Desktop/Program2/haarcascade_frontalface_default.xml")
cap=cv2.VideoCapture(0)
while True:
        ret,frame = cap.read()
        image,face = face_detector(frame)
        if ret:
                try:
                        face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
                        result = model.predict(face)
                        if result[1]<500:
                                confidence= int(100*(1-(result[1])/300))
                                display_string=str(confidence)+"% confidence it is User"
                                cv2.putText(image,display_string,(100,120),cv2.FONT_HERSHEY_COMPLEX,1,(255,120,0))
                        if (confidence>75):
                                cv2.putText(image, "Face Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0))
                                cv2.imshow("Face Cropper",image)
                                music_dir=r"C:\Users\pravesh\Desktop\Program2\song\Normal" # 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\Normal")
                                
                                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))# for random song which is more attractive
                                print(ak)
                                os.startfile(os.path.join(music_dir,songs[ak]))
                                print(songs[ak])
                                break # if you wanna control this loop then you can control this by assuming a variable and make the value is 0 or 1 with user interest.
                                                               
  
                            
                        else:
                                cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255))
                                cv2.imshow("Face Cropper", image)
                except:
                        cv2.putText(image, "Face Not found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0))
                        
                        cv2.imshow("Face Cropper", image)
                        pass
                if cv2.waitKey(1)==13 or 0xFF==ord('q'):
                        break
        
cap.release()
cv2.destroyAllWindows()
