import face as fr test_img=cv2.imread(r'Test_Image.jpg') #Give path to the image which you want to test faces_detected,gray_img=fr.faceDetection(test_img) #print("face Detected: ",faces_detected) face_recognizer=cv2.face.LBPHFaceRecognizer_create() face_recognizer.read(r'trainingData.json') #Give path of where trainingData.yml is saved name={0:"Sujit",1:"Roshan",2:"Neue"} #Change names accordingly. If you want to recognize only one person then write:- name={0:"name"} thats all. Dont write for id number 1. for face in faces_detected: (x,y,w,h)=face roi_gray=gray_img[y:y+h,x:x+h] label,confidence=face_recognizer.predict(roi_gray) print ("Confidence :",confidence) print("label :",label) fr.draw_rect(test_img,face) predicted_name=name[label] fr.put_text(test_img,predicted_name,x,y) resized_img=cv2.resize(test_img,(1000,700)) cv2.imshow("face detection ", resized_img) cv2.waitKey(0) cv2.destroyAllWindows
0: "Sujit", 1: "Roshan", 2: "Neue" } #Change names accordingly. If you want to recognize only one person then write:- name={0:"name"} thats all. Dont write for id number 1. while True: ret, test_img = cap.read() faces_detected, gray_img = fr.faceDetection(test_img) #print("face Detected: ",faces_detected) for (x, y, w, h) in faces_detected: cv2.rectangle(test_img, (x, y), (x + w, y + h), (0, 255, 0), thickness=5) for face in faces_detected: (x, y, w, h) = face roi_gray = gray_img[y:y + h, x:x + h] label, confidence = face_recognizer.predict(roi_gray) print("Confidence :", confidence) #print("label :",label) fr.draw_rect(test_img, face) predicted_name = name[label] if confidence > 55: fr.put_text(test_img, "unknown", x, y) continue fr.put_text(test_img, predicted_name, x, y) resized_img = cv2.resize(test_img, (1000, 700)) cv2.imshow("face detection ", resized_img) if cv2.waitKey(10) == ord('q'): break