安装dlib
https://blog.csdn.net/zmdsjtu/article/details/72642521
如果要使用GPU,需要确认已经安装了cudnn-dev并且从源码编译dlib。
1 2 3 4 5 6 |
pip uninstall dlib # 卸载原来的dlib git clone https://github.com/davisking/dlib.git cd dlib python3 setup.py install |
安装face_recognition
1 2 |
sudo pip3 install face_recognition |
一个实时检测的demo
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# -*- coding:utf-8 -*- import cv2 import face_recognition import time # os.environ["CUDA_VISIBLE_DEVICES"] = '0' def demo_video(): known_face_encodings = [] known_face_names = [] obama_image = face_recognition.load_image_file('obama.jpg') obama_face_encoding = face_recognition.face_encodings(obama_image)[0] known_face_encodings.append(obama_face_encoding) known_face_names.append('obama') song = face_recognition.load_image_file('SongTiger.jpg') song_encoding = face_recognition.face_encodings(song)[0] known_face_encodings.append(song_encoding) known_face_names.append('SongTiger') vcap = cv2.VideoCapture(0) # run ls /dev/video* to see video id (here 0) if not vcap.isOpened(): print("video cannot open!\n") return -1 frames = 0 fps = 0 t = time.time() while True: ret, img = vcap.read() if not ret: break # get fps frames = frames + 1 nt = time.time() if nt - t > 1.0: fps = frames frames = 0 t = nt # Draw fps cv2.putText(img, 'fps:' + str(fps), (5, 0 + 15), 0, 0.6, (255, 255, 255), 1) if True: face_locations = face_recognition.face_locations(img) face_encodings = face_recognition.face_encodings(img, face_locations) # Loop through each face found in the unknown image for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): # See if the face is a match for the known face(s) face_distances = face_recognition.face_distance(known_face_encodings, face_encoding) name = "Unknown" # matches = face_recognition.compare_faces(known_face_encodings, face_encoding) min = 1.0 for i, face_distance in enumerate(face_distances): if face_distance < min: min = face_distance name = known_face_names[i] # draw.rectangle(((left, top), (right, bottom)), outline=(0, 255, 0)) cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3) # Draw a label with a name below the face cv2.putText(img, name, (left, top + 25), 0, 1, (0, 255, 0), 3) # Display the resulting image cv2.imshow("result", img) if cv2.waitKey(10) & 0xFF == ord('q'): vcap.release() cv2.destroyAllWindows() break if __name__ == '__main__': print("run demo_video...") demo_video() # demo_video() |
wa