0

どこが間違っているのかよくわかりません-自分で撮った+/-画像を使用して、オブジェクト検出のためにOpenCVをトレーニングしようとしています。すべての手順は正常に機能しますが、最終的に Python スクリプトは XML カスケード ファイルを読み取れません (ただし、組み込みの顔検出ファイルの 1 つが読み込まれます)。

ちなみに、私は Mac Lion で Python 2.7.3 を実行しています。

私のプロセス:

  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

さて、サイバーデッカーの提案は数少ない問題の 1 つだったようです。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 の代わりに、detectMultiScale の正しいラッパー関数を使用していますか? カスケードは、関数 CascadeClassifier::detectMultiScale でのみ機能するカスケードを生成する traincascade でトレーニングされたためです。cvHaarDetect では機能しません。

Python で使用する必要がある関数は、こちらcv2.CascadeClassifier.detectMultiScaleのドキュメントをご覧ください。

于 2012-10-09T16:26:31.673 に答える