gpt4 book ai didi

python - OpenCV Traincascade XML文件错误

转载 作者:行者123 更新时间:2023-12-02 16:57:15 25 4
gpt4 key购买 nike

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

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

我的过程:

  • 在正面图像上创建带有边界框的收集文件
  • 创建负图像列表
  • 使用以下命令使用opencv_createsamples:opencv_createsamples -info collection.txt -bg negativeImages.txt -vec positiveVectorFile.vec -num 20 -w 32 -h 24
  • 检查 vector 文件:图像有些压缩,但看起来不错
  • 使用以下命令运行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。不知道这是我的级联文件,还是其他问题,还是很明显?

    最佳答案

    好吧,似乎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()

    关于python - OpenCV Traincascade XML文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803783/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com