Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/python/test/test_peopledetect.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
example to detect upright people in images using HOG features
5
'''
6
7
# Python 2/3 compatibility
8
from __future__ import print_function
9
10
import numpy as np
11
import cv2 as cv
12
13
14
def inside(r, q):
15
rx, ry, rw, rh = r
16
qx, qy, qw, qh = q
17
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
18
19
from tests_common import NewOpenCVTests, intersectionRate
20
21
class peopledetect_test(NewOpenCVTests):
22
def test_peopledetect(self):
23
24
hog = cv.HOGDescriptor()
25
hog.setSVMDetector( cv.HOGDescriptor_getDefaultPeopleDetector() )
26
27
dirPath = 'samples/data/'
28
samples = ['basketball1.png', 'basketball2.png']
29
30
testPeople = [
31
[[23, 76, 164, 477], [440, 22, 637, 478]],
32
[[23, 76, 164, 477], [440, 22, 637, 478]]
33
]
34
35
eps = 0.5
36
37
for sample in samples:
38
39
img = self.get_sample(dirPath + sample, 0)
40
41
found, _w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
42
found_filtered = []
43
for ri, r in enumerate(found):
44
for qi, q in enumerate(found):
45
if ri != qi and inside(r, q):
46
break
47
else:
48
found_filtered.append(r)
49
50
matches = 0
51
52
for i in range(len(found_filtered)):
53
for j in range(len(testPeople)):
54
55
found_rect = (found_filtered[i][0], found_filtered[i][1],
56
found_filtered[i][0] + found_filtered[i][2],
57
found_filtered[i][1] + found_filtered[i][3])
58
59
if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:
60
matches += 1
61
62
self.assertGreater(matches, 0)
63
64
if __name__ == '__main__':
65
NewOpenCVTests.bootstrap()
66
67