2016-02-04 22:12:32 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
'''
|
|
|
|
example to detect upright people in images using HOG features
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2016-02-04 22:12:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-02-05 22:46:52 +08:00
|
|
|
from tests_common import NewOpenCVTests, intersectionRate
|
2016-02-04 22:12:32 +08:00
|
|
|
|
|
|
|
class peopledetect_test(NewOpenCVTests):
|
|
|
|
def test_peopledetect(self):
|
|
|
|
|
2017-12-11 17:55:03 +08:00
|
|
|
hog = cv.HOGDescriptor()
|
|
|
|
hog.setSVMDetector( cv.HOGDescriptor_getDefaultPeopleDetector() )
|
2016-02-04 22:12:32 +08:00
|
|
|
|
|
|
|
dirPath = 'samples/data/'
|
|
|
|
samples = ['basketball1.png', 'basketball2.png']
|
|
|
|
|
|
|
|
testPeople = [
|
|
|
|
[[23, 76, 164, 477], [440, 22, 637, 478]],
|
|
|
|
[[23, 76, 164, 477], [440, 22, 637, 478]]
|
|
|
|
]
|
|
|
|
|
|
|
|
eps = 0.5
|
|
|
|
|
|
|
|
for sample in samples:
|
|
|
|
|
|
|
|
img = self.get_sample(dirPath + sample, 0)
|
|
|
|
|
2017-09-03 19:17:15 +08:00
|
|
|
found, _w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
|
2016-02-04 22:12:32 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
matches = 0
|
|
|
|
|
|
|
|
for i in range(len(found_filtered)):
|
|
|
|
for j in range(len(testPeople)):
|
|
|
|
|
|
|
|
found_rect = (found_filtered[i][0], found_filtered[i][1],
|
|
|
|
found_filtered[i][0] + found_filtered[i][2],
|
|
|
|
found_filtered[i][1] + found_filtered[i][3])
|
|
|
|
|
|
|
|
if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:
|
|
|
|
matches += 1
|
|
|
|
|
2017-09-03 20:01:25 +08:00
|
|
|
self.assertGreater(matches, 0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
NewOpenCVTests.bootstrap()
|