Python OpenCV HOG People Detection-如何判断检测到的人是否与之前检测到的人相同?

Python OpenCV HOG People Detection-如何判断检测到的人是否与之前检测到的人相同?,python,opencv,computer-vision,opencv3.0,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, thickn

我是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()
def内部(r,q):
rx,ry,rw,rh=r
qx,qy,qw,qh=q
返回rx>qx和ry>qy和rx+rw
此代码查找人员并在其周围绘制一个矩形。我该如何确定检测到的人是否与视频前一帧中检测到的人相同?或者我怎样才能知道猪发现的人以前是否被发现过


我知道我可以保存HOG找到的位置并进行比较,以查看哪些位置大致相同,但如果视频中的人离开帧然后返回,我认为这种方法行不通,因为他们会被视为新人。是否有可能将他们衣服的颜色与特定的被识别和使用的人联系起来

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