使用简单代码实现摄像头进行在线人脸识别
import cv2 import sys import logging as log import datetime as dt from time import sleep cascPath = "D:\\Python27\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml" faceCascade = cv2.CascadeClassifier(cascPath) # 打开视频捕获设备 video_capture = cv2.VideoCapture(0) while True: if not video_capture.isOpened(): print(‘Unable to load camera.‘) sleep(5) pass # 读视频帧 ret, frame = video_capture.read() # 转为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 调用分类器进行检测 faces = faceCascade.detectMultiScale( gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), #flags=cv2.cv.CV_HAAR_SCALE_IMAGE ) # 画矩形框 for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) # 显示视频 cv2.imshow(‘Video‘, frame) if cv2.waitKey(1) & 0xFF == ord(‘q‘): break # 关闭摄像头设备 video_capture.release() # 关闭所有窗口 cv2.destroyAllWindows()
运行程序后按q键退出程序。注意一些模块的导入,否则代码无法运行。笔者使用笔记本运行程序,可以调用笔记本的摄像头进行人脸识别。
原文:https://www.cnblogs.com/Hello-Yun/p/14783367.html