gpt4 book ai didi

python - 如何在opencv中正确使用peopledetect.py?

转载 作者:行者123 更新时间:2023-12-02 17:48:50 26 4
gpt4 key购买 nike

我是opencv的新手,我确实需要在某些图像中检测人/人,我找到名为peopledetect.py的python接口(interface),我像这样查看代码..

#!/usr/bin/env python

import numpy as np
import cv2

help_message = '''
USAGE: peopledetect.py <image_names> ...

Press any key to continue, ESC to stop.
'''

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)


if __name__ == '__main__':
import sys
from glob import glob
import itertools as it

print help_message

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

for fn in it.chain(*map(glob, sys.argv[1:])):
print fn, ' - ',
try:
img = cv2.imread(fn)
except:
print 'loading error'
continue

found, w = hog.detectMultiScale(img, winStride=(8,8), 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))
cv2.imshow('img', img)
ch = 0xFF & cv2.waitKey()
if ch == 27:
break
cv2.destroyAllWindows()

老实说,我不太了解HOG的原理。

我从opencv网站准备了一些图像,并尝试使用此代码进行一些基本测试,也许像这样..
$ ./peopledetect.py abba.png
$ ./peopledetect.py luna.jpg

但我没有看到在显示的代码中绘制任何重新排列,也许我做错了..有人可以帮助我吗?非常感谢..

abba.png luan.jpg

最佳答案

此代码使用 OpenCV 的 HOG 检测器实现,参见 this tutorial对算法的一个很好的解释。
这个分类器在人或多或少直立的全身图像上进行训练,这就是它要检测的内容。
如果您想在可以看到他们的脸而不是整个 body 时检测到人,请查看 OpenCV's face detection algorithms反而。

关于python - 如何在opencv中正确使用peopledetect.py?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476343/

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