【问题标题】:Python and openCV : HOG descriptor detect multiscale returns negative bounding boxPython和openCV:HOG描述符检测多尺度返回负边界框
【发布时间】:2023-04-03 04:53:01
【问题描述】:

我正在使用 OpenCV 的 HOG 检测器来检测视频中的行人。但是detectMultiScale() 返回的边界框只有一个负值。到目前为止,我在互联网上找不到任何有用或有用的东西来理解和解决这个问题。我什至不知道为什么会出现这个问题。这就是输出。

RECTS:  [[183  -6  68 137]
[ 76  -7  76 152]]
WEIGHTS:  [[ 1.21099767]
[ 0.37004868]]

这是我的代码:

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

webcam = cv2.VideoCapture ('/home/irum/Desktop/Test-Videos/pedistrianTestVideoLONG.mp4')




while True:
    # read each frame
    ret, frame = webcam.read()
    # resize it
    image = imutils.resize(frame, width=min(300, frame.shape[1]))
    orig = image.copy()

    # detect people in the frame
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
        padding=(8, 8), scale=1.1)
    print('RECTS: ',rects)
    print('WEIGHTS: ',weights)
    print('LENGTH: ',len(rects))

    # draw the original bounding boxes
    #for (x, y, w, h) in rects:
    for i in range(len(rects)):

        body_i = rects[i]
        print('BODY_I: ',body_i)

        (x, y, w, h) = [v * 1 for v in body_i]
        print ('DETECTION (x, y, w, h)',x, y, w, h)

        cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # apply non-maxima suppression
        rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
        pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)

        # draw the final bounding boxes

        for i in range(len(pick)):

            g += 1 

            body_p = pick[i]

            (xA, yA, xB, yB) = [int(v * 1) for v in body_p]
            print('DETECTION NMS (xA, yA, xB, yB)', xA, yA, xB, yB)

            # rect on scaled image
            cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) 

            # rects to map on original frame
            (x1, y1, w1, h1) = [int(v * 4.28) for v in body_p]
            print('(x1, y1, w1, h1) ' ,x1, y1, w1, h1)

            cv2.rectangle(frame, (x1, y1), (w1, h1), (0, 45, 255), 2)

            # Crop body from Original frame
            body_big = frame[y1:h1, x1:w1]

            print('DISPLAY')
            cv2.imshow("BODY", body_big)

            # Save body
            save_body_path = '/home/irum/Desktop/pedestrian-detection/BIG_BODY' 

            cur_date = (time.strftime("%Y-%m-%d"))
            cur_time = (time.strftime("%H:%M:%S"))
            new_pin =cur_date+"-"+cur_time
            filename1 = 'BIG'
            filename2 = str(g)+str(filename1)+'-'+str(new_pin)
            #print ("IMAGE TO SEND: ",filename2)

            sampleFile = ('%s/%s.jpg' % (save_body_path, filename2))
            #print ("sampleFile",sampleFile)

            cv2.imwrite('%s/%s.jpg' % (save_body_path, filename2), body_big)
            #pyplot.imsave('%s.jpg' % (sampleFile), body_big)



    # show the output images
    cv2.imshow("Before NMS", orig)
    cv2.imshow("After NMS", image)
    cv2.imshow("BIG BODY", frame)
    # cv2.imshow("FACE", body_big2)
    key = cv2.waitKey(10)
    if key == 27:
        break

【问题讨论】:

    标签:
    python
    python-2.7
    opencv
    svm
    detection