Today, I will share with you a simple picture face recognition technology. In fact, in practical applications, many of them are identified through video streams, such as face recognition channels.
Access control and attendance system, face dynamic tracking and recognition system, etc.
Case show
Here we still use the haar face feature classifier that comes with opencv, and recognize the face by reading a video.
Code:
Python学习交流Q群:906715085###
# -*- coding: utf-8 -*-
__author__ = "小柒"
__blog__ = "https://blog.52itstyle.vip/"
import cv2
import os
def CatchPICFromVideo(window_name, camera_idx, catch_pic_num, path_name):
cv2.namedWindow(window_name)
cap = cv2.VideoCapture(camera_idx)
classfier = cv2.CascadeClassifier(os.getcwd()+"\\haarcascade\\haarcascade_frontalface_alt.xml")
color = (0, 255, 0)
num = 0
while cap.isOpened():
ok, frame = cap.read()
if not ok:
break
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects) > 0:
for faceRect in faceRects:
x, y, w, h = faceRect
img_name = "%s/%d.jpg" % (path_name, num)
# print(img_name)
image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
cv2.imwrite(img_name, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
num += 1
if num > (catch_pic_num):
break
cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, 'num:%d/100' % (num), (x + 30, y + 30), font, 1, (255, 0, 255), 4)
if num > (catch_pic_num): break
# 显示图像
cv2.imshow(window_name, frame)
c = cv2.waitKey(10)
if c & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
CatchPICFromVideo("get face", os.getcwd()+"\\video\\kelake.mp4", 100, "E:\\VideoCapture")
The animation is a bit flowery, so pay attention to it:
If it is to capture the camera, just change the following code:
# 如果获取摄像头,参数修改为 0 即可
cap = cv2.VideoCapture(0)
At last
Sharing is a joy, and it also witnesses the personal growth process. Most of the articles are summaries of work experience and daily learning accumulation. If you think it is helpful to you, remember to like and favorite, like
No need to hide and tuck. Don't understand, remember to like and comment.