In this video lesson we show you how to train the NVIDIA Jetson Xavier NX to recognize faces, and then demonstrate finding those faces in a new unknown image. We show step by step instruction in this easy to follow tutorial. We develop two python programs. The first one simply finds the faces in an unknown image, and the second program actually identifies the known faces. For your convenience, the code is included below.
Simple face detection Python code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import face_recognition import cv2 print(cv2.__version__) image=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/unknown/u1.jpg') face_locations=face_recognition.face_locations(image) print(face_locations) image=cv2.cvtColor(image,cv2.COLOR_RGB2BGR) for (row1,col1,row2,col2) in face_locations: cv2.rectangle(image,(col1,row1),(col2,row2),(255,0,0),2) cv2.imshow('myWindow',image) cv2.moveWindow('myWindow',0,0) if cv2.waitKey(0)==ord('q'): cv2.destroyAllWindows() |
This next python program will learn faces, and then recognize them in new images.
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 | import face_recognition import cv2 print(cv2.__version__) donFace=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/known/Donald Trump.jpg') nancyFace=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/known/Nancy Pelosi.jpg') mikeFace=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/known/Mike Pence.jpg') donEncode=face_recognition.face_encodings(donFace)[0] nancyEncode=face_recognition.face_encodings(nancyFace)[0] mikeEncode=face_recognition.face_encodings(mikeFace)[0] Encodings=[donEncode,nancyEncode,mikeEncode] Names=['Donald Trump','Nancy Pelosi','Mike Pence'] font=cv2.FONT_HERSHEY_SIMPLEX testImage=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/unknown/u11.jpg') facePositions=face_recognition.face_locations(testImage) allEncodings=face_recognition.face_encodings(testImage,facePositions) testImage=cv2.cvtColor(testImage,cv2.COLOR_RGB2BGR) for (top,right,bottom,left), face_encoding in zip(facePositions,allEncodings): name='Unknown Life Form' matches=face_recognition.compare_faces(Encodings,face_encoding) if True in matches: first_match_index=matches.index(True) name=Names[first_match_index] cv2.rectangle(testImage,(left,top),(right,bottom),(255,0,0),2) cv2.rectangle(testImage,(left,top),(left+200, top+30),(0,255,255),-1) cv2.putText(testImage,name,(left,top+20),font,.75,(255,0,0),2) cv2.imshow('myWindow',testImage) cv2.moveWindow('myWindow',0,0) if cv2.waitKey(0)==ord('q'): cv2.destroyAllWindows() |