gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:57 24 4
gpt4 key购买 nike

我是 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 找到的位置并进行比较,看看哪些位置大致相同,但我认为如果视频中的人离开画面然后又返回,这种方法不会奏效,因为他们会被视为一个新人。是否有可能将他们衣服的颜色与特定的人联系起来并使用它?

最佳答案

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

关于python - OpenCV HOG 人物检测 - 如何判断检测到的人是否与之前检测到的人相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33726569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com