forked from tobybreckon/python-examples-cv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhog.py
114 lines (74 loc) · 3.58 KB
/
hog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#####################################################################
# Example : HOG pedestrain detection from a video file
# specified on the command line (e.g. FILE.py video_file) or from an
# attached web camera
# Author : Toby Breckon, [email protected]
# Copyright (c) 2015 / 2016 School of Engineering & Computing Science,
# Durham University, UK
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
#####################################################################
import cv2
import argparse
import sys
#####################################################################
keep_processing = True;
EVENT_LOOP_DELAY = 40; # delay for GUI window
# 40 ms equates to 1000ms/25fps = 40ms per frame
# parse command line arguments for camera ID or video file
parser = argparse.ArgumentParser(description='Perform ' + sys.argv[0] + ' example operation on incoming camera/video image')
parser.add_argument("-c", "--camera_to_use", type=int, help="specify camera to use", default=0)
parser.add_argument('video_file', metavar='video_file', type=str, nargs='?', help='specify optional video file')
args = parser.parse_args()
#####################################################################
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)
#####################################################################
# define video capture object
cap = cv2.VideoCapture();
# define display window name
windowName = "HOG pedestrain detection"; # window name
# if command line arguments are provided try to read video_name
# otherwise default to capture from attached H/W camera
if (((args.video_file) and (cap.open(str(args.video_file))))
or (cap.open(args.camera_to_use))):
# create window by name (as resizable)
cv2.namedWindow(windowName, cv2.WINDOW_NORMAL);
# set up HoG detector
hog = cv2.HOGDescriptor();
hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() );
while (keep_processing):
# if video file successfully open then read frame from video
if (cap.isOpened):
ret, img = cap.read();
# perform HOG based pedestrain detection
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)
# display image
cv2.imshow(windowName,img);
# if user presses "x" then exit
key = cv2.waitKey(40) & 0xFF; # wait 40ms (i.e. 1000ms / 25 fps = 40 ms)
# e.g. if user presses "x" then exit / press "f" for fullscreen display
if (key == ord('x')):
keep_processing = False;
elif (key == ord('f')):
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN);
# close all windows
cv2.destroyAllWindows()
else:
print("No video file specified or camera connected.");