test_peopledetect.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. '''
  3. example to detect upright people in images using HOG features
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import numpy as np
  8. import cv2 as cv
  9. def inside(r, q):
  10. rx, ry, rw, rh = r
  11. qx, qy, qw, qh = q
  12. return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh
  13. from tests_common import NewOpenCVTests, intersectionRate
  14. class peopledetect_test(NewOpenCVTests):
  15. def test_peopledetect(self):
  16. hog = cv.HOGDescriptor()
  17. hog.setSVMDetector( cv.HOGDescriptor_getDefaultPeopleDetector() )
  18. dirPath = 'samples/data/'
  19. samples = ['basketball1.png', 'basketball2.png']
  20. testPeople = [
  21. [[23, 76, 164, 477], [440, 22, 637, 478]],
  22. [[23, 76, 164, 477], [440, 22, 637, 478]]
  23. ]
  24. eps = 0.5
  25. for sample in samples:
  26. img = self.get_sample(dirPath + sample, 0)
  27. found, _w = hog.detectMultiScale(img, winStride=(8,8), padding=(32,32), scale=1.05)
  28. found_filtered = []
  29. for ri, r in enumerate(found):
  30. for qi, q in enumerate(found):
  31. if ri != qi and inside(r, q):
  32. break
  33. else:
  34. found_filtered.append(r)
  35. matches = 0
  36. for i in range(len(found_filtered)):
  37. for j in range(len(testPeople)):
  38. found_rect = (found_filtered[i][0], found_filtered[i][1],
  39. found_filtered[i][0] + found_filtered[i][2],
  40. found_filtered[i][1] + found_filtered[i][3])
  41. if intersectionRate(found_rect, testPeople[j][0]) > eps or intersectionRate(found_rect, testPeople[j][1]) > eps:
  42. matches += 1
  43. self.assertGreater(matches, 0)
  44. if __name__ == '__main__':
  45. NewOpenCVTests.bootstrap()