Path: blob/master/modules/python/test/test_peopledetect.py
16337 views
#!/usr/bin/env python12'''3example to detect upright people in images using HOG features4'''56# Python 2/3 compatibility7from __future__ import print_function89import numpy as np10import cv2 as cv111213def inside(r, q):14rx, ry, rw, rh = r15qx, qy, qw, qh = q16return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh1718from tests_common import NewOpenCVTests, intersectionRate1920class peopledetect_test(NewOpenCVTests):21def test_peopledetect(self):2223hog = cv.HOGDescriptor()24hog.setSVMDetector( cv.HOGDescriptor_getDefaultPeopleDetector() )2526dirPath = 'samples/data/'27samples = ['basketball1.png', 'basketball2.png']2829testPeople = [30[[23, 76, 164, 477], [440, 22, 637, 478]],31[[23, 76, 164, 477], [440, 22, 637, 478]]32]3334eps = 0.53536for sample in samples:3738img = self.get_sample(dirPath + sample, 0)3940found, _w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)41found_filtered = []42for ri, r in enumerate(found):43for qi, q in enumerate(found):44if ri != qi and inside(r, q):45break46else:47found_filtered.append(r)4849matches = 05051for i in range(len(found_filtered)):52for j in range(len(testPeople)):5354found_rect = (found_filtered[i][0], found_filtered[i][1],55found_filtered[i][0] + found_filtered[i][2],56found_filtered[i][1] + found_filtered[i][3])5758if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:59matches += 16061self.assertGreater(matches, 0)6263if __name__ == '__main__':64NewOpenCVTests.bootstrap()656667