不太确定我哪里出错了 - 我正在尝试用我自己拍摄的+/-图像训练OpenCV进行物体检测。所有步骤都可以正常工作,但最终我的Python脚本不会读取我的XML级联文件(但会加载其中一个内置的人脸检测文件)。
对于它的价值,我在Mac Lion上运行Python 2.7.3。
我的流程:
opencv_createsamples
使用opencv_createsamples -info collection.txt -bg negativeImages.txt -vec positiveVectorFile.vec -num 20 -w 32 -h 24
: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
程序: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()
然后我运行以下Python脚本(使用通常的面部检测XML):
cv2.error: The node does not represent a user object (unknown type?)
结果是错误: {{1}}
我在这里上传了级联文件:http://pastebin.com/w7uRjyN7。不确定这是我的级联文件,还是其他一些问题,还是一些明显的问题?
答案 0 :(得分: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()
答案 1 :(得分:0)
我不确定,因为我不使用python-opencv,只使用C ++部分。您确定使用了detectMultiScale的正确包装函数而不是旧的C cvHaarDetect吗?因为您的级联是使用traincascade训练的,它会生成只能使用CascadeClassifier :: detectMultiScale函数的级联。它不适用于cvHaarDetect。
我认为您需要在python中使用的函数是cv2.CascadeClassifier.detectMultiScale
,请查看文档here。