代码之家  ›  专栏  ›  技术社区  ›  irum zahra

Python和openCV:HOG描述符检测多尺度返回负边界框

  •  2
  • irum zahra  · 技术社区  · 9 年前

    我正在使用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
    
    1 回复  |  直到 9 年前
        1
  •  3
  •   irum zahra    8 年前

    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
            padding=(0, 0), scale=1.1)
    

    我改变论点 padding 价值到 (0,0)