-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpedestrian-stepping.py
214 lines (137 loc) · 5.31 KB
/
pedestrian-stepping.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import os
import sys
import tensorflow as tf
from imutils.video import VideoStream
import cv2
import imutils
import time
from imutils.video import FPS
from sklearn.metrics import pairwise
import copy
import pathlib
from collections import defaultdict
from utils import ops as utils_ops
from utils import label_map_util
from utils import visualization_utils as vis_util
utils_ops.tf = tf.compat.v1
tf.gfile = tf.io.gfile
PATH_TO_LABELS = '../bigdata/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
model_name = 'ssd_inception_v2_coco_2018_01_28'
model_dir = "../bigdata/models/" + model_name + "/saved_model"
detection_model = tf.saved_model.load(str(model_dir))
detection_model = detection_model.signatures['serving_default']
# print(category_index)
print(detection_model.inputs)
print(detection_model.output_dtypes)
print(detection_model.output_shapes)
font = cv2.FONT_HERSHEY_SIMPLEX
flag = 0
area = 0
areaDetails = []
def estimate_stepping(output_dict,height,width,image_np):
pedes_present = 0
global flag,area,areaDetails
details = []
for ind,scr in enumerate(output_dict['detection_classes']):
if scr==1:
ymin, xmin, ymax, xmax = output_dict['detection_boxes'][ind]
score = output_dict['detection_scores'][ind]
if score>0.4:
area = int((xmax - xmin)*width * (ymax - ymin)*height)
print(output_dict['detection_boxes'][ind],output_dict['detection_scores'][ind],area)
if area>9000:
pedes_present = 1
flag=5
details.append([int(xmin*width), int(ymin*height), int((xmax - xmin)*width), int((ymax-ymin)*height)])
if pedes_present == 0:
flag=flag-1
else:
area = 0
for box in details:
xmin, ymin, w, h = box
boxArea = w * h
cv2.rectangle(image_np, (xmin, ymin), (xmin + w, ymin + h), (0,0,0), 3)
if boxArea > area:
area = boxArea
areaDetails = details
if flag > 0:
for box in areaDetails:
xmin, ymin, w, h = box
cv2.rectangle(image_np, (xmin, ymin), (xmin + w, ymin + h), (0,0,0), 3)
# cv2.putText(image_np, str(areaPerson), (xmin, ymin), font , 1.2, [0,0,0], 2)
if area > 15000:
cv2.putText(image_np,"STOP IT !!! DON'T HIT HIM " + str(area),(50,50), font, 1.2,(0,0,255),2,cv2.LINE_AA)
else:
cv2.putText(image_np,"BE CAREFUL !!! Someone is in front " + str(area),(50,50), font, 1.2,(0,255,255),2,cv2.LINE_AA)
def run_inference_for_single_image(model, image):
image = np.asarray(image)
input_tensor = tf.convert_to_tensor(image)
input_tensor = input_tensor[tf.newaxis,...]
# output_dict is a dict with keys detection_classes , num_detections , detection_boxes(4 coordinates of each box) , detection_scores for 100 boxes
output_dict = model(input_tensor)
# num_detections gives number of objects in current frame
num_detections = int(output_dict.pop('num_detections'))
# output_dict is a dict with keys detection_classes , detection_boxes(4 coordinates of each box) , detection_scores for num_detections boxes
output_dict = {key:value[0, :num_detections].numpy()
for key,value in output_dict.items()}
# adding num_detections that was earlier popped out
output_dict['num_detections'] = num_detections
# converting all values in detection_classes as ints.
output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
# print(5,output_dict)
return output_dict
def show_inference(model, image_path):
image_np = np.array(image_path)
height,width,channel = image_np.shape
print(image_np.shape)
# Actual detection.
output_dict = run_inference_for_single_image(model, image_np)
estimate_stepping(output_dict,height,width,image_np)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks_reframed', None),
use_normalized_coordinates=True,
line_thickness=8)
return image_np
# cap=cv2.VideoCapture(0)
cap=cv2.VideoCapture('../videos/a.mp4')
time.sleep(2.0)
cap.set(1,25*843)
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# out1 = cv2.VideoWriter('pedestrian.avi', fourcc, 30, (1280,720))
fps = FPS().start()
ctt = 0
while True:
(grabbed, frame) = cap.read()
frame=imutils.resize(frame, width=1280)
print('frame',frame.shape)
# print(ctt)
# ctt = ctt + 1
# if ctt==334:
# break
frame=show_inference(detection_model, frame)
cv2.imshow("version", frame)
# out1.write(frame)
fps.update()
key=cv2.waitKey(1)
if key & 0xFF == ord("q"):
break
# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cap.release()
# out1.release()
cv2.destroyAllWindows()
# a.mp4(25) 104*25 843*25 913*25
# m.mp4(24) 6 25
# o.mp4(30) 2
# p.mp4(30) 3
# q.mp4(30) 20 0