|
12、从摄像头进行实时人脸识别的简单Demo附源码
- # -*- coding: utf-8 -*-
- import face_recognition
- import cv2
- import numpy as np
- # 12、从摄像头进行实时人脸识别的简单Demo附源码
- # Get a reference to webcam #0 (the default one)
- video_capture = cv2.VideoCapture(0)
- # Load a sample picture and learn how to recognize it.
- obama_image = face_recognition.load_image_file("obama.jpg")
- obama_face_loc = face_recognition.face_locations(obama_image)
- # 只有一个人脸位置
- obama_face_encoding = face_recognition.face_encodings(obama_image, obama_face_loc)[0]
- dfy_image = face_recognition.load_image_file("dfy.jpg")
- dfy_face_loc = face_recognition.face_locations(dfy_image)
- # 问题:face_encodings 是对大图还是小的人脸图获取128维embedding
- # face_image: The image that contains one or more faces 结论:大图 还有一个参数known_face_locations
- dfy_face_encoding = face_recognition.face_encodings(face_image=dfy_image, known_face_locations=dfy_face_loc)[0]
- # Load a second sample picture and learn how to recognize it.
- biden_image = face_recognition.load_image_file("biden.jpg")
- biden_face_loc = face_recognition.face_locations(biden_image)
- biden_face_encoding = face_recognition.face_encodings(biden_image, biden_face_loc)[0]
- # Create arrays of known face encodings and their names
- # 知道的人脸编码 是唯一的每个人
- known_face_encodings = [
- obama_face_encoding,
- dfy_face_encoding,
- biden_face_encoding
- ]
- known_face_names = [
- "Barack Obama",
- "Dfy",
- "Joe Biden"
- ]
- # Initialize some variables
- face_locations = []
- face_encodings = []
- face_names = []
- process_this_frame = True
- while True:
- # Grab a single frame of video
- ret, frame = video_capture.read()
- # Resize frame of video to 1/4 size for faster face recognition processing
- # 问题:这里的(0,0)代表中心 缩放到原来的四分之一
- # 默认的插值方法为:双线性插值 cv2.INTER_NEAREST最近邻插值法
- small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
- # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
- rgb_small_frame = small_frame[:, :, ::-1]
- # 仅每隔一帧处理一次视频以节省时间
- if process_this_frame:
- # Find all the faces and face encodings in the current frame of video
- face_locations = face_recognition.face_locations(rgb_small_frame)
- # 找到所有的人脸 和 人脸的特征编码
- face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
- face_names = [] # 这里并没有重复 为了下一次的清空
- for face_encoding in face_encodings:
- # See if the face is a match for the known face(s)
- matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
- name = "Unknown"
- # # If a match was found in known_face_encodings, just use the first one.
- # if True in matches:
- # first_match_index = matches.index(True)
- # name = known_face_names[first_match_index]
- # Or instead, use the known face with the smallest distance to the new face
- face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
- best_match_index = np.argmin(face_distances)
- # 同时考虑两个API:compare_faces face_distance
- if matches[best_match_index]:
- # 识别出来 人脸是谁了
- name = known_face_names[best_match_index]
- face_names.append(name)
- # 这是为了降低处理速度的
- process_this_frame = not process_this_frame
- # Display the results 人脸位置的格式:(y1, x2, y2, x1)
- for (top, right, bottom, left), name in zip(face_locations, face_names):
- # Scale back up face locations since the frame we detected in was scaled to 1/4 size
- top *= 4
- right *= 4
- bottom *= 4
- left *= 4
- # Draw a box around the face cv2里坐标必须都是整数
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # Draw a label with a name below the face
- # 也是画了一个矩形 但是有背景的 cv2.filled
- cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
- font = cv2.FONT_HERSHEY_DUPLEX
- cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
- # Display the resulting image
- cv2.imshow('Video', frame)
- # Hit 'q' on the keyboard to quit!
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # Release handle to the webcam
- video_capture.release()
- cv2.destroyAllWindows()
复制代码
|
|