本例例用人脸特征库实现电脑摄像头实时追踪人脸。相关知识见前面博客
python-计算机视觉-OpenCV-人脸特征提取
python-计算机视觉-OpenCV-Image
python-计算机视觉-OpenCV-video
下面是代码实例:
import cv2
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml') # 加载人脸特征库
face_cascade.load(r'D:\python\myTest\face\haarcascade_frontalface_alt2.xml')
while(True):
ret, frame = cap.read() # 读取一帧的图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转灰
faces = face_cascade.detectMultiScale(gray, scaleFactor = 1.15, minNeighbors = 5, minSize = (5, 5)) # 检测人脸
for(x, y, w, h) in faces:
cv2.rectangle(gray, (x, y), (x + w, y + h), (0, 255, 0), 2) # 用矩形圈出人脸
cv2.imshow('Face Recognition', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # 释放摄像头
cv2.destroyAllWindows()