-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeople_counter.py
131 lines (91 loc) · 3.66 KB
/
people_counter.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
example to show people detection using opencv SVM detector
Keys:
1 - Pause
2 - Continue
Keys:
ESC - exit
'''
from os import listdir
from os.path import isfile, isdir, join
from matplotlib import pyplot as plt
import cv2
root_folder_path = "E:\Data/UCSD_Anomaly_Dataset/UCSD_Anomaly_Dataset.v1p2/UCSDped1\Train/".replace('\\', '/')
# root_folder_path = "E:\Data/UCSD_Anomaly_Dataset/UCSD_Anomaly_Dataset.v1p2/UCSDped1\Test/".replace('\\', '/')
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)
if __name__ == '__main__':
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
folders = [f for f in listdir(root_folder_path)]
for folder_path in folders:
if '.DS_Store' in folder_path:
continue
folder_path = root_folder_path + folder_path
files = [f for f in listdir(folder_path) if isfile(join(folder_path, f))]
for f in files:
if '.DS_Store' in f:
continue
f = folder_path + '/' + f
im = cv2.imread(f, 0) # read image in greyscale
frame = im
found, _ = hog.detectMultiScale(frame, 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(frame, found)
draw_detections(frame, found_filtered, 3)
cv2.imshow("Display", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# cv2.imshow('display', im)
# cv2.waitKey(2)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print(__doc__)
track_range = list(range(5))
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
filepath = "E:\Projects\computer-vision\computer-vision-python\opencv-starter\data/video\AVSS/AVSS_AB_Easy.avi".replace('\\', '/')
filepath = "C:/Users\pc\Downloads\Datasets/sidewalk_people.mp4".replace('\\', '/')
filepath = "C:/Users\pc\Downloads\Datasets/vtest.avi".replace('\\', '/')
cap = cv2.VideoCapture(filepath)
isRecording = True
while True:
if isRecording:
ch = cv2.waitKey(5)
if ch == ord('1'): # Pause
isRecording = not isRecording
else:
ch = cv2.waitKey(5)
if ch == ord('2'): # Pause
isRecording = not isRecording
continue
_, frame = cap.read()
found, _ = hog.detectMultiScale(frame, 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(frame, found)
draw_detections(frame, found_filtered, 3)
cv2.imshow("Display", frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cv2.destroyAllWindows()