我正在尝试在 ubuntu 上使用 opencv python 从网络摄像头中检测人脸。我得到了这个在线代码并尝试运行这个程序,我得到了 as NULL 数组指针被传递,我猜它无法从网络摄像头捕获视频,但使用相同的代码(仅捕获摄像头)我打开了摄像头并且它捕获了视频。这是我的代码:
import cv
from opencv import highgui
HAAR_CASCADE_PATH = "/home/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_default.xml"
CAMERA_INDEX = 0
def detect_faces(image):
faces = []
detected = cv.HaarDetectObjects(image, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (100,100))
if detected:
for (x,y,w,h),n in detected:
faces.append((x,y,w,h))
return faces
if __name__ == "__main__":
cv.NamedWindow("Video", cv.CV_WINDOW_AUTOSIZE)
capture = cv.CaptureFromCAM(0)
storage = cv.CreateMemStorage()
cascade = cv.Load(HAAR_CASCADE_PATH)
print cascade
faces = []
i = 0
c = -1
while (c == -1):
image = cv.QueryFrame(capture)
# Only run the Detection algorithm every 5 frames to improve performance
#if i%5==0:
faces = detect_faces(image)
#print image
for (x,y,w,h) in faces:
cv.Rectangle(image, (x,y), (x+w,y+h), 255)
cv.ShowImage("w1", image)
i += 1
我得到的错误是:
Traceback (most recent call last):
File "/home/OpenCV-2.3.1/webcam_try.py", line 38, in <module>
faces = detect_faces(frame)
File "/home/OpenCV-2.3.1/webcam_try.py", line 13, in detect_faces
detected = cv.cvHaarDetectObjects(frame, cascade, storage, 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING,(100,100))
File "/usr/lib/pymodules/python2.7/opencv/cv.py", line 1626, in cvHaarDetectObjects
return _cv.cvHaarDetectObjects(*args)
NotImplementedError: Wrong number of arguments for overloaded function 'cvHaarDetectObjects'.
Possible C/C++ prototypes are:
cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int,int,CvSize)
cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int,int)
cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double,int)
cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *,double)
cvHaarDetectObjects_Shadow(CvArr const *,CvHaarClassifierCascade *,CvMemStorage *)