赞
踩
step1:准备好opencv,numpy和face_recognition三个库
step4:将图片转为特征向量,并将向量和名字添加到列表中,一一对应
step10:最后进行opencv的imshow显示图片,最后按q退出循环
先来了解下face_recognition的各个函数的作用
开始进行识别:
- frame1 = cv2.imread(r'images/liudehua.jpeg')
- frame2 = cv2.imread(r'images/shileipeng.png')
- frame3 = cv2.imread(r'images/linluocheng.jpg')
- frame1_encoding = face_recognition.face_encodings(frame1)[0]
- encodings.append(frame1_encoding)
- names.append('liudehua')
-
- frame2_encoding = face_recognition.face_encodings(frame2)[0]
- encodings.append(frame2_encoding)
- names.append('shileipeng')
-
- frame3_encoding = face_recognition.face_encodings(frame3)[0]
- encodings.append(frame3_encoding)
- names.append('linluocheng')
- while True:
- ret, img = cap.read()
change_img = cv2.resize(img,(0,0),fx=0.25,fy=0.25)
- frame_RGB = change_img[:, :, ::-1]
- faces_locations = face_recognition.face_locations(frame_RGB)
- faces_encodings = face_recognition.face_encodings(frame_RGB, faces_locations)
- for face_encoding in faces_encodings:
- matches = face_recognition.compare_faces(encodings,face_encoding)
- distances = face_recognition.face_distance(encodings, face_encoding)
- min_distance_index = np.argmin(distances)
- name = 'unknown'
- if matches[min_distance_index]:
- name = names[min_distance_index]
- detect_names.append(name)
- for (top, right, bottom, left), name in zip(faces_locations, detect_names):
- top *= 4
- right *= 4
- bottom *= 4
- left *= 4
-
- cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
- cv2.putText(img, name, (left, top - 6), font, 1.0, (255, 255, 255), 1)
- cv2.imshow('test',img)
-
- key = cv2.waitKey(1)
- if key == ord('q'):
- break
- import face_recognition
- import cv2
- import numpy as np
-
- cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
-
- encodings = []
- names = []
- font = cv2.FONT_HERSHEY_SIMPLEX
- detect_names=[]
-
- class init():
- frame1 = face_recognition.load_image_file(r'images/liudehua.jpeg')
- frame2 = face_recognition.load_image_file(r'images/shileipeng.png')
- frame3 = face_recognition.load_image_file(r'images/linluocheng.jpg')
-
- frame1_encoding = face_recognition.face_encodings(frame1)[0]
- encodings.append(frame1_encoding)
- names.append('liudehua')
-
-
- frame2_encoding = face_recognition.face_encodings(frame2)[0]
- encodings.append(frame2_encoding)
- names.append('shileipeng')
-
- frame3_encoding = face_recognition.face_encodings(frame3)[0]
- encodings.append(frame3_encoding)
- names.append('linluocheng')
-
-
- class show():
- while True:
- ret, img = cap.read()
- change_img = cv2.resize(img,(0,0),fx=0.25,fy=0.25)
-
- frame_RGB = change_img[:, :, ::-1]
- faces_locations = face_recognition.face_locations(frame_RGB)
- faces_encodings = face_recognition.face_encodings(frame_RGB, faces_locations)
-
- for face_encoding in faces_encodings:
- matches = face_recognition.compare_faces(encodings,face_encoding)
- distances = face_recognition.face_distance(encodings, face_encoding)
- min_distance_index = np.argmin(distances)
- name = 'unknown'
- if matches[min_distance_index]:
- name = names[min_distance_index]
- detect_names.append(name)
-
- for (top, right, bottom, left), name in zip(faces_locations, detect_names):
- top *= 4
- right *= 4
- bottom *= 4
- left *= 4
-
- cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
- cv2.putText(img, name, (left, top - 6), font, 1.0, (255, 255, 255), 1)
-
- cv2.imshow('test',img)
-
- key = cv2.waitKey(1)
- if key == ord('q'):
- break
-
- if __name__ == '__main__':
- init()
- show()
- cap.release()
- cv2.destroyAllWindows()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。