gpt4 book ai didi

python - 实时人脸检测 OpenCV、Python

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:41 27 4
gpt4 key购买 nike

我正在尝试制作一个实时人脸检测的基本程序。这是我的代码(我是 OpenCV 的新手):

import numpy as np
import cv2
cam = cv2.VideoCapture(0)
name = 'detect'
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)
while True:
s, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
#print s
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow(name, img)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyWindow("Detect")
break

但是当我运行这段代码时,我得到了这个错误:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.4.2+dfsg/modules/core/src/array.cpp, line   2482
Traceback (most recent call last):
File "mytry.py", line 27, in <module>
cv2.imshow(name, img)
cv2.error: /build/buildd/opencv-2.4.2+dfsg/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat

我是 OpenCV 的新手!请告诉我代码有什么问题,为什么会出现此错误我应该进行哪些更改?

最佳答案

你的线路:

img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

会在图片中绘制一个矩形,但是返回值为None,所以img变为None无法绘制。

尝试

cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

关于python - 实时人脸检测 OpenCV、Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com