Not quite sure where I’m going wrong – I’m trying to train OpenCV for object detection with +/- images that I’ve taken myself. All the steps work ok, but ultimately my Python script won’t read my XML cascade file (but will load one of the built-in face detection files).
For what it’s worth, I’m on Mac Lion running Python 2.7.3.
My process:
- Create a collection file with bounding boxes on the positive images
- Create a list of negative images
- Use
opencv_createsamplesusing the following command:opencv_createsamples -info collection.txt -bg negativeImages.txt -vec positiveVectorFile.vec -num 20 -w 32 -h 24 - Check vector file: images are a bit squished but look ok
- Run
traincascadeprogram using the following command: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
Then I run the following Python script (which works with the usual face-detection 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()
The result is the error:
cv2.error: The node does not represent a user object (unknown type?)
I’ve uploaded the cascade file here: http://pastebin.com/w7uRjyN7. Not sure if it’s my cascade file, some other problem along the way, or something obvious?
Well, it appears that cyberdecker’s suggestion was one of a few problems: that cv2 has entirely different commands for everything and is required when using
opencv_traincascade. My code, which now works (though my cascade doesn’t quite yet):