OpenCV HOG人员检测 - 如何判断检测到的人是否与先前检测到的人相同?

时间:2015-11-15 23:52:47

标签: python opencv computer-vision opencv3.0

我是OpenCV的新手,我正在尝试编写一个可以检测视频中人物的程序。我有这个代码,它是peopledetect示例的变体。

def inside(r, q):
    rx, ry, rw, rh = r
    qx, qy, qw, qh = q
    return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh


def draw_detections(img, rects, thickness=1):
    for x, y, w, h in rects:
        # the HOG detector returns slightly larger rectangles than the real objects.
        # so we slightly shrink the rectangles to get a nicer output.
        pad_w, pad_h = int(0.15 * w), int(0.05 * h)
        cv2.rectangle(img, (x + pad_w, y + pad_h),
                      (x + w - pad_w, y + h - pad_h), (0, 255, 0), thickness)

def find_people(img):
        hog = cv2.HOGDescriptor()
        hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
        img = frame
        if img is None:
            return None
        #  print('Failed to load image file:', fn)
        #  continue
        # except:
        #  print('loading error')
        #  continue

        found, w = hog.detectMultiScale(
            img, winStride=(10, 10), padding=(32, 32), scale=1.05)
        found_filtered = []
        for ri, r in enumerate(found):
            for qi, q in enumerate(found):
                if ri != qi and inside(r, q):
                    break
                else:
                    found_filtered.append(r)
                draw_detections(img, found)
                draw_detections(img, found_filtered, 3)
                print('%d (%d) found' % (len(found_filtered), len(found)))
        return img

if __name__ == '__main__':


    import argparse
#   import itertools as it
    ap = argparse.ArgumentParser()
    ap.add_argument("-v", "--video",
                    help="path to the (optional) video file")
    ap.add_argument("-b", "--buffer", type=int, default=64,
                    help="max buffer size")
    # ap.add_argument("-f", "--blur-faces", action='blur_faces',
    #        help="Blur the faces contained in the video")
    args = vars(ap.parse_args())

    print(help_message)

    camera = cv2.VideoCapture(args["video"])

 # keep looping
    while True:
      # grab the current frame
        (grabbed, frame) = camera.read()

  # if we are viewing a video and we did not grab a frame,
  # then we have reached the end of the video
      # if args.get("video") and not grabbed:
      #    break
        img = find_people(frame)

        cv2.imshow('img', img)
        #Waitkey must be called for something to show up on the screen
        #It gives the computer time to process the image.
        cv2.waitKey(30)

    cv2.destroyAllWindows()

此代码查找人员并在其周围绘制一个矩形。我如何查明检测到的人是否与在视频的前一帧中检测到的人相同?或者我怎么能知道HOG检测到的人以前是否被检测过?

我知道我可以保存HOG找到并比较的位置以查看哪些是大致相同的但我不认为如果视频中的人离开框架然后返回因为他们会被视为一个新人。是否有可能将他们的衣服的颜色与特定的人识别并使用它?

1 个答案:

答案 0 :(得分:0)

实际上,我正在做类似的事情,因为我正在“跟踪”视频中出现的人数,以计算商店中的传入/正在进行的人数。 为了判断这是否是同一个人,我只是使用检测位置,并将其与之前检测中找到的矩形进行比较。 这非常有效,除非我有假阳性和假阴性。