haar-cascade没有提出xml文件

时间:2017-06-21 09:16:01

标签: python xml opencv haar-classifier

我是学生,我正在尝试用基本的手识别来创建我自己的第一个haar-cascade(用于学习)。我收集了510个阳性样本和1276个阴性样本。我的训练一切都很好(我想,我让你看到终端窗口)。

唯一的问题是当我使用带有基本脚本的xml文件时(你看到它之后)没有任何结果。我用我的终端启动它,我没有错误。之后我的相机掉了几圈!(但它的速度比较慢)但是没有窗口出现。如果我正在使用来自网络的其他xml文件,它可以工作......我不是没有原因......

有人可以帮助我吗?

Python 3.5和opencv 3.2

 Charless-MacBook-Pro:desktop charles$ opencv_traincascade -data data -vec Positive_image.vec -bg Negative_image.text -numPos 449 -numNeg 1276 -numStages 10 -w 50 -h 50 -featureType LBP
    PARAMETERS:
    cascadeDirName: data
    vecFileName: Positive_image.vec
    bgFileName: Negative_image.text
    numPos: 449
    numNeg: 1276
    numStages: 10
    precalcValBufSize[Mb] : 1024
    precalcIdxBufSize[Mb] : 1024
    acceptanceRatioBreakValue : -1
    stageType: BOOST
    featureType: LBP
    sampleWidth: 50
    sampleHeight: 50
    boostType: GAB
    minHitRate: 0.995
    maxFalseAlarmRate: 0.5
    weightTrimRate: 0.95
    maxDepth: 1
    maxWeakCount: 100

    ===== TRAINING 0-stage =====
    <BEGIN
    POS count : consumed   449 : 449
    NEG count : acceptanceRatio    1276 : 1
    Precalculation time: 18
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        1|
    +----+---------+---------+
    |   2|        1|        1|
    +----+---------+---------+
    |   3|        1| 0.402821|
    +----+---------+---------+
    END>
    Training until now has taken 0 days 0 hours 1 minutes 31 seconds.

    ===== TRAINING 1-stage =====
    <BEGIN
    POS count : consumed   449 : 449
    NEG count : acceptanceRatio    1276 : 0.403542
    Precalculation time: 19
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        1|
    +----+---------+---------+
    |   2|        1|        1|
    +----+---------+---------+
    |   3| 0.995546| 0.300157|
    +----+---------+---------+
    END>
    Training until now has taken 0 days 0 hours 3 minutes 14 seconds.

    ===== TRAINING 2-stage =====
    <BEGIN
    POS count : consumed   449 : 451
    NEG count : acceptanceRatio    1276 : 0.121674
    Precalculation time: 19
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        1|
    +----+---------+---------+
    |   2|        1|        1|
    +----+---------+---------+
    |   3|        1| 0.413009|
    +----+---------+---------+
    END>
    Training until now has taken 0 days 0 hours 4 minutes 59 seconds.

    ===== TRAINING 3-stage =====
    <BEGIN
    POS count : consumed   449 : 451
    NEG count : acceptanceRatio    1276 : 0.0501789
    Precalculation time: 20
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        1|
    +----+---------+---------+
    |   2|        1|        1|
    +----+---------+---------+
    |   3|        1|0.0783699|
    +----+---------+---------+
    END>
    Training until now has taken 0 days 0 hours 6 minutes 46 seconds.

    ===== TRAINING 4-stage =====
    <BEGIN
    POS count : consumed   449 : 451
    NEG count : acceptanceRatio    1276 : 0.00391926
    Precalculation time: 20
    +----+---------+---------+
    |  N |    HR   |    FA   |
    +----+---------+---------+
    |   1|        1|        0|
    +----+---------+---------+
    END>
    Training until now has taken 0 days 0 hours 8 minutes 44 seconds.

    ===== TRAINING 5-stage =====
    <BEGIN
    POS count : consumed   449 : 451
    Train dataset for temp stage can not be filled. Branch training terminated.

这是我使用python的基本脚本:

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('/Users/charles/Desktop/closedFist.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

编辑:

有效。我收集了2100个阳性样本和4500个阴性样本。我的xml文件有25个阶段。我的错误是具有相同分辨率的负片和正片样品:50x50像素。现在负样品的比例为16:9。 160x90px。它的工作原理!!!

1 个答案:

答案 0 :(得分:0)

我有一些制作HAAR级联分类器的经验。在您的培训阶段,您提供的终端转储具有 TRAINING 5阶段终止。所以训练阶段已经中止。(你已经在你的代码中指定了10个阶段的训练,但是第5阶段已经失败并且中止了整个训练过程......)

我怀疑你需要更多阳性样本来识别物体。有太多的负面文件。我使用了2:1的正负样本比例。用不同比例进行实验,但优选具有更多阳性样品。 (还取决于您的矢量文件创建选项!)

如果您可以描述如何创建矢量文件并启动列车命令,那么SO社区可以更好地帮助您。 :)

编辑:

我刚刚发现“错误”消息指出“训练临时阶段的数据集无法填充。分支训练终止”意味着算法已达到逻辑结束。这意味着使用给定的图像数据集无法使分类模型更好。所以使用stage = 5作为stage = 6会导致这个问题...希望它有所帮助!