2012-10-09 60 views
0

不太確定我要去哪裏錯 - 我試圖訓練OpenCV進行對象檢測,使用我自己拍攝的+/-圖像。所有步驟都可以正常工作,但最終我的Python腳本不會讀取我的XML級聯文件(但會加載其中一個內置的人臉檢測文件)。OpenCV traincascade 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。不知道它是我的級聯文件,還是其他一些問題,或明顯的東西?

回答

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() 
0

我不確定,因爲我不使用python-opencv,只使用C++部分。你確定你正在爲detectMultiScale使用正確的包裝函數,而不是舊的C cvHaarDetect?因爲你的級聯是用traincascade訓練過的,所以cascade只產生CascadeClassifier :: detectMultiScale函數。它不適用於cvHaarDetect。

我認爲你需要在python中使用的函數是cv2.CascadeClassifier.detectMultiScale,請看文檔here

+0

嗯,感謝您的建議(這是我認爲可能會導致問題)。該錯誤實際上似乎來自cv.Load--在CascadeClassifier可能發生錯誤之前拋出。 – JeffThompson

+0

編輯:更近一步,它似乎cv2需要級聯加載爲'cv2.CascadeClassifier(「filename.xml」)' – JeffThompson

+0

是的,當您切換到cv2和traincascade時,它在功能上會發生很大變化。關於您的級聯,培訓已正確終止?你說你的代碼現在運行,很好!你有沒有測試過一些opencv級聯以確保它成功加載級聯? – cyberdecker