如何正确使用opencv中的peopledetect.py? [英] how to correctly use peopledetect.py in opencv?
问题描述
我是opencv的新手,我确实需要在某些图像中检测人/人,我发现了名为peopledetect.py的python接口,并且我会像这样浏览代码.
I'm newbie of opencv and I have real need for detecting people/human within some images, I find python interface named peopledetect.py and I look through code just like this..
#!/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的原理.
To be honest, I don't really understand principle of HOG.
我从opencv网站准备了一些图像,并尝试使用此代码进行一些基本测试,也许是这样.
I prepare some image from opencv website and try to do some basic test by using this code, maybe like this..
$ ./peopledetect.py abba.png
$ ./peopledetect.py luna.jpg
但是我看不到显示的代码中有任何纠结,也许我做错了..有人可以帮我吗?非常感谢..
but I don't see any retangle drawed within code on display, maybe I did wrong.. could someone help me? thanks so much..
推荐答案
此代码使用的是OpenCV的HOG检测器实现,请参见而是OpenCV的人脸检测算法.
This code is using OpenCV's implementation of the HOG detector, see this tutorial for a good explanation of the algorithm. This classifier is trained on whole body images of people standing more or less upright, and that is what it will detect. If you want to detect people when you can see their face, but not the whole of their body, then take a look at OpenCV's face detection algorithms instead.
这篇关于如何正确使用opencv中的peopledetect.py?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!