Guys in this lesson we learn how to not only detect faces in an image, but also recognize who the person is. We start by training our model with known faces, and then find and identify those faces in other pictures. For your convenience, I include the code developed in this lesson below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import cv2 import face_recognition as FR font=cv2.FONT_HERSHEY_SIMPLEX donFace=FR.load_image_file('C:/Users/Valued Customer/Documents/Python/demoImages/known/Donald Trump.jpg') faceLoc=FR.face_locations(donFace)[0] donFaceEncode=FR.face_encodings(donFace)[0] nancyFace=FR.load_image_file('C:/Users/Valued Customer/Documents/Python/demoImages/known/Nancy Pelosi.jpg') faceLoc=FR.face_locations(nancyFace)[0] nancyFaceEncode=FR.face_encodings(nancyFace)[0] penceFace=FR.load_image_file('C:/Users/Valued Customer/Documents/Python/demoImages/known/Mike Pence.jpg') faceLoc=FR.face_locations(penceFace)[0] penceFaceEncode=FR.face_encodings(penceFace)[0] knownEncodings=[donFaceEncode,nancyFaceEncode,penceFaceEncode] names=['Donald Trump','Nancy Pelosi','Mike Pence'] unknownFace=FR.load_image_file('C:/Users/Valued Customer/Documents/Python/demoImages/unknown/u1.jpg') unknownFaceBGR=cv2.cvtColor(unknownFace,cv2.COLOR_RGB2BGR) faceLocations=FR.face_locations(unknownFace) unknownEncodings=FR.face_encodings(unknownFace,faceLocations) for faceLocation,unknownEncoding in zip(faceLocations,unknownEncodings): top,right,bottom,left=faceLocation print(faceLocation) cv2.rectangle(unknownFaceBGR,(left,top),(right,bottom),(255,0,0),3) name='Unknown Person' matches=FR.compare_faces(knownEncodings,unknownEncoding) print(matches) if True in matches: matchIndex=matches.index(True) print(matchIndex) print(names[matchIndex]) name=names[matchIndex] cv2.putText(unknownFaceBGR,name,(left,top),font,.75,(0,0,255),2) cv2.imshow('My Faces',unknownFaceBGR) cv2.waitKey(10000) |