0

不太确定我哪里出错了 - 我正在尝试使用我自己拍摄的 +/- 图像训练 OpenCV 进行对象检测。所有步骤都正常,但最终我的 Python 脚本不会读取我的 XML 级联文件(但会加载内置的人脸检测文件之一)。

对于它的价值,我在运行 Python 2.7.3 的 Mac Lion 上。

我的过程:

  1. 在正图像上创建带有边界框的集合文件
  2. 创建负面图像列表
  3. 使用opencv_createsamples以下命令:opencv_createsamples -info collection.txt -bg negativeImages.txt -vec positiveVectorFile.vec -num 20 -w 32 -h 24
  4. 检查矢量文件:图像有点挤压但看起来还可以
  5. traincascade使用以下命令运行程序:opencv_traincascade -data directoryToStoreFiles -vec positiveVectorFile.vec -bg negativeImageList.txt -numPos 16 -numNeg 20 -numStages 5 -mem 1000 -maxHitRate 0.95 -w 32 -h 24

然后我运行以下 Python 脚本(它适用于通常的面部检测 XML):

import cv
img = cv.LoadImage("test.jpg", 0)

# load detection file (various files for different views and uses)
cascade = cv.Load("cascade.xml")        # doesn't work
#cascade = cv.Load("frontalface.xml")   # works

# detect faces, return as list
detected = cv.HaarDetectObjects(img, cascade, cv.CreateMemStorage())

# iterate detected objects, drawing a rectangle around each
for (x,y, w,h), n in detected:
    cv.Rectangle(img, (x,y), (x+w, y+h), 255)

# create a window to display the results
windowTitle = "Test Cascade"
cv.NamedWindow(windowTitle, cv.CV_WINDOW_AUTOSIZE)

# display tested image until the escape key is pressed
while True:
    cv.ShowImage(windowTitle, img)

    # watch for escape key (ASCII 20)
    key = cv.WaitKey(20)
    if key == 27:

        # save the image to file is specified
        if saveIt == True:
            cv.SaveImage("detected.png", img)

        # ... and quit
        exit()

结果是错误: cv2.error: The node does not represent a user object (unknown type?)

我在这里上传了级联文件:http: //pastebin.com/w7uRjyN7。不确定这是我的级联文件,还是其他问题,还是明显的问题?

4

2 回答 2

3

好吧,cyberdecker 的建议似乎是几个问题之一:cv2 对所有内容都有完全不同的命令,并且在使用opencv_traincascade. 我的代码,现在可以工作了(虽然我的级联还没有):

#import library - MUST use cv2 if using opencv_traincascade
import cv2

# rectangle color and stroke
color = (0,0,255)       # reverse of RGB (B,G,R) - weird
strokeWeight = 1        # thickness of outline

# set window name
windowName = "Object Detection"

# load an image to search for faces
img = cv2.imread("test.jpg")

# load detection file (various files for different views and uses)
cascade = cv2.CascadeClassifier("cascade.xml")

# preprocessing, as suggested by: http://www.bytefish.de/wiki/opencv/object_detection
# img_copy = cv2.resize(img, (img.shape[1]/2, img.shape[0]/2))
# gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
# gray = cv2.equalizeHist(gray)

# detect objects, return as list
rects = cascade.detectMultiScale(img)

# display until escape key is hit
while True:

    # get a list of rectangles
    for x,y, width,height in rects:
        cv2.rectangle(img, (x,y), (x+width, y+height), color, strokeWeight)

    # display!
    cv2.imshow(windowName, img)

    # escape key (ASCII 27) closes window
    if cv2.waitKey(20) == 27:
        break

# if esc key is hit, quit!
exit()
于 2012-10-10T14:41:16.267 回答
0

我不确定,因为我不使用 python-opencv,只使用 C++ 部分。您确定您使用的是正确的检测多尺度包装函数,而不是旧的 C cvHaarDetect?因为您的级联是使用 traincascade 训练的,它生成的级联仅适用于函数 CascadeClassifier::detectMultiScale。它不适用于 cvHaarDetect。

我认为你需要在 python 中使用的功能是cv2.CascadeClassifier.detectMultiScale,看看这里的文档。

于 2012-10-09T16:26:31.673 回答