# python openCV调用摄像头进行人脸识别 https://blog.csdn.net/wjh2622075127/article/details/90712424
# 报错 SystemError: <class 'cv2.CascadeClassifier'> returned a result with an error set, 大概是找不到 xml
# pip --version 查看python安装目录
# c:\users\abc\appdata\local\programs\python\python37\lib\site-packages\pip
# 在安装cv2这个模块的时候,会在你python安装路径下面生成”C:\Users\abc\AppData\Local\Programs\Python\Python37\Lib\site-packages\cv2\data“,在这个路径下面就有haarcascade_frontalface_alt.xml
# 如果你的python安装路径下面没有生成,可以看一下链接:https://blog.csdn.net/qq_20156437/article/details/80702022
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
xmlfile = r'C:\Users\abc\AppData\Local\Programs\Python\Python37\Lib\site-packages\cv2\data\haarcascade_frontalface_alt.xml' # 坑坑
face_cascade = cv2.CascadeClassifier(xmlfile)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.15,
minNeighbors=5,
minSize=(5, 5),
)
print("发现{0}个目标!".format(len(faces)))
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 2)
cv2.imshow("frame", frame)
# Display the resulting frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
【 实测可用 】python + CV2 视频人脸识别
最新推荐文章于 2022-12-28 21:20:33 发布
本文介绍如何使用Python的OpenCV库进行摄像头人脸识别过程中遇到的‘SystemError’报错,着重于解决haarcascade_frontalface_alt.xml文件缺失问题,并提供安装与路径配置指导。
摘要由CSDN通过智能技术生成