4

我正在尝试在 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 *)
4

2 回答 2

5

您的代码对我来说运行良好(尽管我运行的是 OpenCV v2.4.3 而不是您的版本 2.3.1)。上周我开始使用相同的在线代码(在此处发布),最终我放弃使用cv并切换到新cv2库。

所以。我已经更新了您的代码,以便它使用新cv2界面。

用于运行 Haar Cascade Classifiers的cv2Python 界面更易于使用。查看cv2.CascadeClassifier.detectMultiScale() 此处的文档。新cv2界面显着简化了您的代码。以下是重点:

  1. 您不再需要担心创建内存缓冲区。
  2. 从返回的结果detectMultiScale以超级有用的形式返回,无需旧代码的detect_faces()功能!
  3. 您只需要提供一个参数:图像本身。所有其他参数都是可选的。我已经在下面的修改后的代码中包含了您使用的参数,但可以随意删除它们。

一条建议:如果您的代码运行缓慢,您可以做的最好的事情之一就是增加 minSize。对于我的网络摄像头,使用 (100,100) 会导致大约 0.2fps 的超慢帧速率。将其更改为 (300,300) 可将其提升到可观的 20fps。

由于您运行的是 2.3.1,因此该代码应该可以在您现有的安装上运行,但如果不能,请尝试升级到最新版本。

import cv2
import cv2.cv as cv

HAAR_CASCADE_PATH = "/home/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_default.xml";

CAMERA_INDEX = 0;

if __name__ == "__main__":

    # Open window, load webcam and load Haar cascade classifier
    cv2.namedWindow("Video", cv.CV_WINDOW_AUTOSIZE)
    capture = cv2.VideoCapture(CAMERA_INDEX);
    cascade = cv2.CascadeClassifier(HAAR_CASCADE_PATH);

    i = 0;

    while True:
        # Grab frame from webcam
        retVal, image = capture.read(); # note: ignore retVal

        # Only run the Detection algorithm every 5 frames to improve performance
        #if i%5==0:
        faces = cascade.detectMultiScale(image, scaleFactor=1.2, 
                                        minNeighbors=2, minSize=(100,100), 
                                        flags=cv.CV_HAAR_DO_CANNY_PRUNING);

        # Draw rectangles on image, and then show it
        for (x,y,w,h) in faces:
            cv2.rectangle(image, (x,y), (x+w,y+h), 255)
        cv2.imshow("Video", image)

        i += 1;
于 2013-01-03T23:20:18.223 回答
2

在参考http://goo.gl/UziMVU之后,我稍微更改了 Brandon 的代码。这对我来说是一个工作代码。

import cv2
import cv2.cv as cv

HAAR_CASCADE_PATH = "/usr/local/Cellar/opencv/2.4.6.1/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0);
cascade = cv2.CascadeClassifier(HAAR_CASCADE_PATH);

retVal, frame = vc.read(); # note: ignore retVal

while True:
    if frame is not None:   
        faces = cascade.detectMultiScale(frame, scaleFactor=1.2, minNeighbors=2, flags=cv.CV_HAAR_DO_CANNY_PRUNING, minSize=(100,100));

        for (x,y,w,h) in faces:
            cv2.rectangle(frame, (x,y), (x+w,y+h), 255)

        cv2.imshow("preview", frame)
    rval, frame = vc.read()

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
于 2013-11-02T09:46:46.883 回答