- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_tracker.py
More file actions
Latest commit
153 lines (125 loc) · 5.3 KB
/
Copy pathobject_tracker.py
File metadata and controls
153 lines (125 loc) · 5.3 KB
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
fromstatisticsimportmode
importimutils
importcv2
importnumpyasnp
fromimutils.videoimportVideoStream
importtime
importdatetime
frompreprocessorimportpreprocess_input
fromtracker.centroidtrackerimportCentroidTracker
importpickleaspkl
defdetect_faces(detection_model, gray_image_array, conf):
frame=gray_image_array
(h,w) =frame.shape[:2]
blob=cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
detection_model.setInput(blob)
predictions=detection_model.forward()
coord_list= []
count=0
foriinrange(0, predictions.shape[2]):
confidence=predictions[0,0,i,2]
ifconfidence>conf:
# Find box coordinates rescaled to original image
box_coord=predictions[0,0,i,3:7] *np.array([w,h,w,h])
conf_text='{:.2f}'.format(confidence)
# Find output coordinates
xmin, ymin, xmax, ymax=box_coord.astype('int')
coord_list.append([xmin, ymin, (xmax-xmin), (ymax-ymin)])
print('Coordinate list:', coord_klist)
returncoord_list
defdraw_text(coordinates, image_array, text, color, x_offset=0, y_offset=0,
font_scale=2, thickness=2):
x, y=coordinates[:2]
cv2.putText(image_array, text, (x+x_offset, y+y_offset),
cv2.FONT_HERSHEY_SIMPLEX,
font_scale, color, thickness, cv2.LINE_AA)
defdraw_bounding_box(face_coordinates, image_array, color, identity):
x, y, w, h=face_coordinates
if"_"notinidentity:
cv2.rectangle(image_array, (x, y), (x+w, y+h), color, 3)
cv2.putText(image_array, str(identity), (x+5,y-5), font, 1, color, 2)
else:
cv2.rectangle(image_array, (x, y), (x+w, y+h), color, 3)
defapply_offsets(face_coordinates, offsets):
x, y, width, height=face_coordinates
x_off, y_off=offsets
return (x-x_off, x+width+x_off, y-y_off, y+height+y_off)
defload_detection_model(prototxt, weights):
detection_model=cv2.dnn.readNetFromCaffe(prototxt, weights)
returndetection_model
defverify_employee_id(key_list):
key_list=key_list
unique_val=np.unique(np.array(key_list))
list_id= []
list_count= []
forjinrange(len(unique_val)):
count=key_list.count(unique_val[j])
list_id.append(unique_val[j])
list_count.append(count)
print(list_id)
print(list_count)
index=np.array(list_count).argmax()
iid=list_id[index]
returniid
font=cv2.FONT_HERSHEY_SIMPLEX
frame_window=10
face_offsets= (30, 40)
confidence=0.6
defvideo_predict(file_name, face_detection):
ct=CentroidTracker()
face_detection_size= (40, 40)
counter=0
frame_process_counter=0
cv2.namedWindow('Tracking', cv2.WINDOW_NORMAL)
video_capture=cv2.VideoCapture(file_name)
time.sleep(1.0)
while (video_capture.isOpened()):
ret, bgr_image=video_capture.read()
ifret==False:
break
counter+=1
ifcounter%1==0:
frame_process_counter+=1
gray_image=cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
rgb_image=cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
rgb_image1=rgb_image
faces=detect_faces(face_detection, bgr_image,confidence)
identity="Person"
print("frame_process_counter : ", frame_process_counter)
forface_coordinatesinfaces:
x1, x2, y1, y2=apply_offsets(face_coordinates, face_offsets)
rgb_face=rgb_image[y1:y2, x1:x2]
try:
rgb_face=cv2.resize(rgb_face, (face_detection_size))
except:
continue
rgb_face=np.expand_dims(rgb_face, 0)
rgb_face=preprocess_input(rgb_face, False)
color= (0, 0, 255)
draw_bounding_box(face_coordinates, rgb_image, (35,255,255), identity)
objects=ct.update(faces)
for (key, centroid), face_coordinatesinzip(objects.items(), faces):
x1, x2, y1, y2=apply_offsets(face_coordinates, face_offsets)
rgb_face=rgb_image1[y1:y2, x1:x2]
try:
rgb_face=cv2.resize(rgb_face, (face_detection_size))
except:
continue
rgb_face=np.expand_dims(rgb_face, 0)
rgb_face=preprocess_input(rgb_face, False)
rgb_image=imutils.resize(rgb_image, width=1080)
text="ID - {}".format(key)
cv2.putText(rgb_image, text, (centroid[0] -10, centroid[1] -10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv2.circle(rgb_image, (centroid[0], centroid[1]), 4, (0, 255, 0), -1)
bgr_image=cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
frame=bgr_image
frame=cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
cv2.imshow('Tracking', bgr_image)
ifcv2.waitKey(1) &0xFF==ord('q'):
print('Total frames processed:', counter, frame_process_counter)
break
video_capture.release()
cv2.destroyAllWindows()
return"successful"