-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideoDetector.py
86 lines (80 loc) · 3.6 KB
/
videoDetector.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
#importing necessary libraries
import cv2 as cv
import time
import geocoder
import os
def detectPotholeonVideo(filename):
# reading label name from obj.names file
class_name = []
with open(os.path.join("project_files", 'obj.names'), 'r') as f:
class_name = [cname.strip() for cname in f.readlines()]
# importing model weights and config file
# defining the model parameters
net1 = cv.dnn.readNet('project_files/yolov4_tiny.weights', 'project_files/yolov4_tiny.cfg')
net1.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
net1.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA_FP16)
model1 = cv.dnn_DetectionModel(net1)
model1.setInputParams(size=(640, 480), scale=1 / 255, swapRB=True)
# defining the video source (0 for camera or file name for video)
cap = cv.VideoCapture(filename)
width = cap.get(3)
height = cap.get(4)
result = cv.VideoWriter('PotholeVideoResult.mp4',
cv.VideoWriter_fourcc(*'MP4V'),
10, (int(width), int(height)))
# defining parameters for result saving and get coordinates
# defining initial values for some parameters in the script
g = geocoder.ip('me')
result_path = "pothole_coordinates"
starting_time = time.time()
Conf_threshold = 0.5
NMS_threshold = 0.4
frame_counter = 0
i = 0
b = 0
# detection loop
while True:
ret, frame = cap.read()
frame_counter += 1
if ret == False:
break
# analysis the stream with detection model
classes, scores, boxes = model1.detect(frame, Conf_threshold, NMS_threshold)
for (classid, score, box) in zip(classes, scores, boxes):
label = "Pothole"
x, y, w, h = box
recarea = w * h
area = width * height
# drawing detection boxes on frame for detected potholes and saving coordinates txt and photo
if (len(scores) != 0 and scores[0] >= 0.7):
if ((recarea / area) <= 0.1 and box[1] < 600):
cv.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)
cv.putText(frame, "%" + str(round(scores[0] * 100, 2)) + " " + label, (box[0], box[1] - 10),
cv.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 0), 1)
if (i == 0):
cv.imwrite(os.path.join(result_path, 'pothole' + str(i) + '.jpg'), frame)
with open(os.path.join(result_path, 'pothole' + str(i) + '.txt'), 'w') as f:
f.write(str(g.latlng))
i = i + 1
if (i != 0):
if ((time.time() - b) >= 2):
cv.imwrite(os.path.join(result_path, 'pothole' + str(i) + '.jpg'), frame)
with open(os.path.join(result_path, 'pothole' + str(i) + '.txt'), 'w') as f:
f.write(str(g.latlng))
b = time.time()
i = i + 1
# writing fps on frame
endingTime = time.time() - starting_time
fps = frame_counter / endingTime
cv.putText(frame, f'FPS: {fps}', (20, 50),
cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
# showing and saving result
cv.imshow('Pothole Detection (Press Q to Close)', frame)
result.write(frame)
key = cv.waitKey(1)
if key == ord('q'):
break
# end
cap.release()
result.release()
cv.destroyAllWindows()