Repository navigation
Expand file tree
/
Copy pathtempCodeRunnerFile.py
More file actions
303 lines (227 loc) · 9.49 KB
/
Copy pathtempCodeRunnerFile.py
File metadata and controls
303 lines (227 loc) · 9.49 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import cv2
import numpy as np
import time
import math
import random
videoSource = 0
cap = cv2.VideoCapture(videoSource)
'''
UNCOMMENT LINES 28 AND 29 IF YOU HAVE A GPU AND CUDA.
DEFAULT CONFIGURATION IS TO USE THE CPU.
'''
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
# net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
# net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
global people
global personId
global colors
global enteredPeople
global exitedPeople
global doorThresh
global doorCoord
people = []
personId = 0
colors = [(255, 0, 0), (0, 255, 0) ,(0, 0, 255)]
enteredPeople = 0
exitedPeople = 0
doorThresh = 50
doorCoord = (670, 400)
appendThresh = 80
class Person:
def __init__(self, personId, location):
self.id = personId
self.curLocation = [x,y]
self.trajectory = []
self.state = []
self.flag = 0
if personId < len(colors):
self.color = colors[personId]
else:
self.color = colors[personId % len(colors)]
def addPointToTrajectory(location):
self.trajectory.append(location)
def plotTrajectories(frame):
global people
global personId
global colors
global enteredPeople
global exitedPeople
global doorThresh
global doorCoord
for person in people:
prev_point = None # Store the previous point
if len(person.trajectory) < 5:
traj = person.trajectory[int(0.8 * len(person.trajectory)) - 1:]
else:
traj = person.trajectory[0:5]
flag = 0
for i in traj:
x, y = i[0], i[1]
color = person.color
frame = cv2.circle(frame, (x, y), 3, color, cv2.FILLED)
if calcDist(doorCoord, i)>50:
flag = 1
if prev_point is not None:
frame = cv2.line(frame, prev_point, (x, y), color, 1) # Draw a line to the previous point
prev_point = (x, y) # Update the previous point
return frame
def calcDist(a, b):
x1 = a[0]
y1 = a[1]
x2 = b[0]
y2 = b[1]
distance = math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)))
return distance
def trackerHandle(curCoords, frame):
global people
global personId
global colors
global enteredPeople
global exitedPeople
global doorThresh
global doorCoord
if len(people)>0:
if len(curCoords) > 0:
if not(len(people) == len(curCoords)):
## EXISTING PERSON EXITED FROM THE FRAME
if len(people) > len(curCoords):
for person in people:
while len(people) > len(curCoords):
# print("adding")
dists = []
coords = []
for curCoord in curCoords:
dists.append(calcDist(curCoord, person.curLocation))
coords.append(curCoord)
maxIndex = dists.index(max(dists))
# ## CHECK THE PREVIOUS 20% OF THE PERSON's TRAJECTORIES AND IDENTIFY IF THE PERSON WALKED TOWARDS OR AWAY
# traj = people[maxIndex].trajectory
# initialCoord = traj[int(0.8 * len(traj))]
# finalCoord = people[maxIndex].trajectory[len[traj] - 1]
# initialDistance = calcDist(doorCoord, initialCoord)
# finalDistance = calcDist(doorCoord, finalCoord)
# if initialDistance<finalDistance:
# enteredPeople += 1
people.pop(maxIndex)
## NEW PERSON IS ADDED
if len(people) < len(curCoords):
for person in people:
while len(people) < len(curCoords):
# print("removing")
dists = []
coords = []
for curCoord in curCoords:
dists.append(calcDist(curCoord, person.curLocation))
coords.append(curCoord)
maxIndex = dists.index(max(dists))
curCoords.pop(maxIndex)
# if len(person.trajectory) == 1:
# if calcDist(doorCoord, person.trajectory[0]) < doorThresh:
# exitedPeople += 1
createPerson(coords[maxIndex])
if len(people) == len(curCoords):
for person in people:
dists = []
coords = []
for curCoord in curCoords:
dists.append(calcDist(curCoord, person.curLocation))
coords.append(curCoord)
minIndex = dists.index(min(dists))
if calcDist(person.curLocation, coords[minIndex]) < appendThresh:
person.trajectory.append(coords[minIndex])
person.curLocation = coords[minIndex]
traj = person.trajectory
if len(traj) > 0 and person.flag==0:
initialCoord = traj[int(0.8 * len(traj))]
finalCoord = person.trajectory[len(traj) - 1]
initialDistance = calcDist(doorCoord, initialCoord)
finalDistance = calcDist(doorCoord, finalCoord)
initialx = initialCoord[0]
finalx = finalCoord[0]
if (initialx-finalx<50 and finalDistance<80):
enteredPeople += 1
person.flag = 1
elif (initialDistance < 20 and finalDistance > 50):
exitedPeople += 1
person.flag = 1
# print(calcDist(doorCoord, person.curLocation))
curCoords.pop(minIndex)
else:
people = []
elif len(people) == 0:
if len(curCoords)>0:
for i in curCoords:
createPerson(i)
curCoords = []
frame = plotTrajectories(frame)
return frame
def createPerson(currentCoord):
global people
global personId
global colors
person = Person(personId, [currentCoord[0], currentCoord[1]])
people.append(person)
personId += 1
font = cv2.FONT_HERSHEY_PLAIN
processFrame = True
with open("coco.names", "r") as f:
classes = f.read().strip().split('\n')
## Set the window size (adjust as needed)
cv2.namedWindow("Person Tracking", cv2.WINDOW_NORMAL)
while True:
startTime = time.time()
ret, frame = cap.read()
frame = cv2.resize(frame, (800, 600))
frame = cv2.circle(frame, doorCoord, 5, (255, 0, 0))
if processFrame == True:
# Detect objects in the frame
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5 and classes[class_id] == 'person':
center_x, center_y, w, h = (detection[0:4] * np.array([frame.shape[1], frame.shape[0], frame.shape[1], frame.shape[0]])).astype(int)
x, y = int(center_x - w / 2), int(center_y - h / 2)
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
indices = cv2.dnn.NMSBoxes(boxes, confidences, score_threshold=0.5, nms_threshold=0.4)
curCoords = []
for i in indices:
box = boxes[i]
curCoords.append([box[0]+int(box[2]/2), box[1]+int(box[3]/2)])
x, y, w, h = box[0], box[1], box[2], box[3]
# Draw a rectangle around each person
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, 'Person', (x, y - 10), font, 1, (0, 255, 0), 1)
frame = trackerHandle(curCoords, frame)
peopleCount = len(people)
# Exit the loop when 'q' is pressed
key = None
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('a'):
processFrame = not processFrame
if not processFrame:
people = []
endTime = time.time()
if startTime == endTime:
fps = 60
else:
fps = 1/(endTime-startTime)
cv2.putText(frame, f"People Count: {peopleCount}", (550, 30), font, 1.5, (0,0,255), 2)
cv2.putText(frame, f"Entered Count: {enteredPeople}", (550, 60), font, 1.5, (0,0,255), 2)
cv2.putText(frame, f"Exited Count: {exitedPeople}", (550, 90), font, 1.5, (0,0,255), 2)
cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30), font, 1.5, (0,0,255), 2)
cv2.imshow("Person Tracking", frame)
# Release the video capture and close the OpenCV windows
cap.release()
cv2.destroyAllWindows()