-
Notifications
You must be signed in to change notification settings - Fork 1
/
car_detection_refPoints.py
279 lines (250 loc) · 13.1 KB
/
car_detection_refPoints.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
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
# import the necessary packages
import os, shutil, datetime, webbrowser, settings
import car_counting_refPoints as counting_cars
import ocr_license_plate
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
import json
import matplotlib.pyplot as plt
'''
change this path to your project directory!
(or maybe automatically detect path somehow)
'''
# testvideoPath = settings.getBaseDir() + '/testfiles/cropAlternativ.mp4'
#testvideoPath = settings.getBaseDir() + '/testfiles/out8.mp4'
# testvideoPath = settings.getBaseDir() + '/testfiles/highway.mov'
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--confidence", type=float, default=0.47,
help="minimum probability to filter weak detections")
ap.add_argument("-f", "--fromfile", type=str, default='',
help="give relative path to prerecorded")
ap.add_argument("-p", "--lanepoints", type=int, default=2, metavar="[1-2]",
help="number of necessary lane points")
ap.add_argument("-l", "--lanes", type=int, default=2, metavar="[1-10]",
help="number of lanes")
ap.add_argument("-o", "--overlay", action='store_false',
help="dont display the counter overlay")
args = vars(ap.parse_args())
# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
"bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
"dog", "horse", "motorbike", "person", "pottedplant", "sheep",
"sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# load our serialized model from disk
print("[INFO] loading model...")
## net = cv2.dnn.readNetFromCaffe(settings.getBaseDir() + "/models/mobileNet_cars/mobilenet_yolov3_deploy.prototxt",
## settings.getBaseDir() + "/models/mobileNet_cars/mobilenet_yolov3_deploy_iter_63000.caffemodel")
net = cv2.dnn.readNetFromCaffe(settings.getBaseDir() + "/models/MobileNetSSD/MobileNetSSD_deploy.prototxt.txt",
settings.getBaseDir() + "/models/MobileNetSSD/MobileNetSSD_deploy.caffemodel")
##net = cv2.dnn.readNetFromCaffe(settings.getBaseDir() + "/models/googlenet_cars/MobileNetSSD_deploy.prototxt.txt",
## settings.getBaseDir() + "/models/googlenet_cars/googlenet_finetune_web_car_iter_10000.caffemodel")
def on_mouse(event,x,y,flags,params):
print(x, y)
def createNewCar(bbox):
firstRefPointMatches = list(filter(lambda n: n.rIndex == 0,bbox.matches))
bbox.setTargetMatch(firstRefPointMatches[-1])
detectedCars.append(counting_cars.DetectedCar([bbox]))
def cropCar(frame, bbox):
(startX, startY, endX, endY) = bbox.astype("int")
startY = max(0, startY)
endY = max(0, endY)
startX = max(0, startX)
endX = max(0, endX)
return frame[startY:endY, startX:endX]
def updatePlot(index, data):
pltData[index - 1] = data
objects = ('Lane 1', 'Lane 2')
yPos = np.arange(len(objects))
plt.bar(yPos, pltData, align='center', color=(0.2, 0.4, 0.6, 1))
plt.xticks(yPos, objects)
plt.draw()
plt.pause(0.000001)
def appendToDetectionNewRefPoint(frame, index, bbox, videoFileDir, box, currElement):
detectedCars[index].detectedBboxArr.append(bbox)
bbox.matches[-1].getLane().onCarDetected()
lane = bbox.matches[-1].getLane()
print("Lane " + str(lane.index) + ": Car " + str(lane.counter) + " detected")
if not live:
updatePlot(lane.index, lane.counter)
color = [255, 0 , 0]
#cv2.putText(frame, str(lane.counter), (673, 279),cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
cv2.imwrite(videoFileDir + "lane%d_car%d.jpg" % (lane.index, lane.counter),
cropCar(frame, box) )
collectedDetections[lane.index-1].append([lane.counter, currElement])
pass
def appendToDetection(index, bbox):
if not bbox.targetMatch:
bbox.setTargetMatch(detectedCars[index].detectedBboxArr[-1].targetMatch)
detectedCars[index].detectedBboxArr.append(bbox)
collectedDetections = [[],[],[],[],[] ,[],[],[],[],[]]
# stop_condition: determines, when the analysis is halted (eg when the videostream is over)
# vs: the videostream (could be live from camera or from prerecorded)
# frame_skip: the amount of frames that are skipped from the video stream (to counter the lag)
def analyze_video(stop_condition,vs,frame_skip):
counter = 0
# counts the frames
frame_counter = 0
# loop over the frames from the video stream
while stop_condition:
# grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
if live:
print("frame")
frame = vs.read()
else:
ret, frame = vs.read()
if not ret:
print("[ERROR] There was an error reading the frame. Its value is:")
print(frame)
break
if frame_counter > frame_skip - 1:
frame = imutils.resize(frame, width=1000)
# grab the frame dimensions and convert it to a blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
0.007843, (300, 300), 127.5)
# pass the blob through the network and obtain the detections and
# predictions
net.setInput(blob)
detections = net.forward()
# loop over the detections
newCar = False
for i in np.arange(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with
# the prediction
confidence = detections[0, 0, i, 2]
idx = int(detections[0, 0, i, 1])
name = CLASSES[idx]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if (name == "car" or name == "bus") and confidence > args["confidence"]:
# extract the index of the class label from the
# `detections`, then compute the (x, y)-coordinates of
# the bounding box for the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# check if the car was already detected before
millis = int(round(time.time() * 1000))
#detectedCars.append(json.dumps([str(confidence), millis, str(startX), str(startY), str(endX), str(endY)]))
cropped_frame = frame[startY:endY, startX:endX]
bbox = counting_cars.Rectangle(startX, startY, endX, endY)
matches = bbox.containsAny()
currDetectedBBox = counting_cars.DetectedBbox(float(confidence), millis, bbox, matches)
if len(matches) > 0:
if len(matches) == 1:
currDetectedBBox.setTargetMatch(matches[-1]) # case of only one match found
index = counting_cars.sameCarInRefPoint(detectedCars, currDetectedBBox)
if index == -1:
firstRefPointMatches = list(filter(lambda n: n.rIndex == 0,currDetectedBBox.matches))
followingRefPointMatches = list(filter(lambda n: n.rIndex > 0,currDetectedBBox.matches))
if len(firstRefPointMatches) == 0:
# only points for second ref points are found -> assign it to its car group
refPointIndex = counting_cars.nextRefPointIndex(detectedCars, currDetectedBBox)
if refPointIndex >= 0:
# is probably the following ref point
#TODO: assign as 2nd ref point
millis = int(round(time.time() * 1000))
currElement = [float(confidence), millis, startX, startY, endX, endY, (startX+endX)/2, (startY+endY)/2]
appendToDetectionNewRefPoint(frame, refPointIndex, currDetectedBBox, videoFileDir, box, currElement)
newCar = True
# else: car was not found, so ignore the bounding box
elif len(firstRefPointMatches) == 1:
if len(followingRefPointMatches) == 0:
# only matches for 1st ref point -> create new car
createNewCar(currDetectedBBox)
else:
refPointIndex= counting_cars.nextRefPointIndex(detectedCars, currDetectedBBox)
if refPointIndex >= 0:
# is probably the following ref point
#TODO: assign as 2nd ref point
millis = int(round(time.time() * 1000))
currElement = [float(confidence), millis, startX, startY, endX, endY, (startX+endX)/2, (startY+endY)/2]
appendToDetectionNewRefPoint(frame, refPointIndex, currDetectedBBox, videoFileDir, box, currElement)
newCar = True
else:
# only matches for 1st ref point -> create new car
createNewCar(currDetectedBBox)
else:
# scenario: mutliple matches for first point -> ignore detection
pass
else:
# append bbox to detected (already existing) car
appendToDetection(index, currDetectedBBox)
#print(counting_cars.matchesToString(matches), millis)
#currCounter = None
#cv2.imwrite(videoFileDir + "car%d_%d.jpg" % (currCounter, counting_cars.numberBoxes(detectedCars, currCounter)),cropped_frame )
# draw the
label = "{}: {:.2f}%".format(CLASSES[idx],confidence * 100)# + " " + str(currCounter)
cv2.rectangle(frame, (startX, startY), (endX, endY),COLORS[idx], 2)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(frame, label, (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
# show the output frame
copy = frame.copy()
if(args["overlay"]):
for idx in range(args["lanes"]):
detections = len(collectedDetections[idx])
cv2.putText(copy, 'lane ' + str(idx+1) + ': ' + str(detections), (50, (30*(idx+1))), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2, cv2.LINE_AA)
cv2.imshow("Frame", copy)
#cv2.setMouseCallback('Frame', on_mouse)
frame_counter = 0
continue
frame_counter += 1
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
detectedCars = []
live = args["fromfile"] == ""
testvideoPath = settings.getBaseDir() + args["fromfile"]
if not live:
plt.ylabel('# cars detected')
plt.title('Detections per lane')
pltData = [0, 0]
plt.pause(0.0000001)
if live:
ending = "live"
else:
ending = settings.getEnding(testvideoPath)
if not os.path.isdir(settings.getOutputDir() + ending):
os.makedirs(settings.getOutputDir() + ending)
videoFileDir = settings.getOutputDir() + ending + "/"
if live:
# initialize the video stream, allow the cammera sensor to warmup
print("[INFO] starting video stream...")
testvideoPath = settings.getBaseDir() + ''
temp_vs = VideoStream(src=0).start()
counting_cars.configure_refPoints(temp_vs, args["lanes"], args["lanepoints"], live)
analyze_video(True,temp_vs, 0)
time.sleep(2.0)
else:
print("[INFO] starting prerecorded video...")
temp_vs = cv2.VideoCapture(testvideoPath)
counting_cars.configure_refPoints(temp_vs, args["lanes"], args["lanepoints"], live)
analyze_video(temp_vs.isOpened,temp_vs, 10)
# save collected data to csv
i=0
while os.path.exists(f'visualization/output{i}.csv'):
i += 1
output_file_path = f'visualization/output{i}.csv'
if not os.path.isfile(output_file_path):
with open(output_file_path, 'a') as f:
f.write("lane_id,car_id,type,license_plate,date,time")
f.write('\n')
with open(output_file_path, 'a') as f:
for idx, detection in enumerate(collectedDetections, start=1):
for bboxes in detection:
license_plate = ocr_license_plate.detect_license_plate(bboxes[0], videoFileDir)
last = bboxes[-1]
dt = datetime.datetime.fromtimestamp(last[1]/1000.0)
f.write(str(idx) + "," +str(bboxes[0]+addCounter) + ",car," + str(license_plate) + "," + str(dt.date()) + "," + str(dt.time())[:5])
f.write('\n')
shutil.rmtree(videoFileDir)
cv2.destroyAllWindows()
#temp_vs.stop()