Skip to content

Commit 728d90d

Browse files
author
Asadullah-Dal17
committed
adding recorder yolov4.py
1 parent e59e1cc commit 728d90d

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.mp4
2+
*.avi

yolov4.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import cv2 as cv
2+
import time
23
Conf_threshold = 0.4
34
NMS_threshold = 0.4
45
COLORS = [(0, 255, 0), (0, 0, 255), (255, 0, 0),
@@ -17,9 +18,11 @@
1718

1819

1920
cap = cv.VideoCapture('output.avi')
20-
21+
starting_time = time.time()
22+
frame_counter = 0
2123
while True:
2224
ret, frame = cap.read()
25+
frame_counter += 1
2326
if ret == False:
2427
break
2528
classes, scores, boxes = model.detect(frame, Conf_threshold, NMS_threshold)
@@ -28,8 +31,12 @@
2831
label = "%s : %f" % (class_name[classid[0]], score)
2932
cv.rectangle(frame, box, color, 1)
3033
cv.putText(frame, label, (box[0], box[1]-10),
31-
cv.FONT_HERSHEY_COMPLEX, 0.5, color, 2)
32-
34+
cv.FONT_HERSHEY_COMPLEX, 0.3, color, 1)
35+
endingTime = time.time() - starting_time
36+
fps = frame_counter/endingTime
37+
# print(fps)
38+
cv.putText(frame, f'FPS: {fps}', (20, 50),
39+
cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
3340
cv.imshow('frame', frame)
3441
key = cv.waitKey(1)
3542
if key == ord('q'):

yolov4_Recording.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import cv2 as cv
2+
import time
3+
Conf_threshold = 0.4
4+
NMS_threshold = 0.4
5+
COLORS = [(0, 255, 0), (0, 0, 255), (255, 0, 0),
6+
(255, 255, 0), (255, 0, 255), (0, 255, 255)]
7+
8+
class_name = []
9+
with open('classes.txt', 'r') as f:
10+
class_name = [cname.strip() for cname in f.readlines()]
11+
# print(class_name)
12+
net = cv.dnn.readNet('yolov4-tiny.weights', 'yolov4-tiny.cfg')
13+
net.setPreferableBackend(cv.dnn.DNN_BACKEND_CUDA)
14+
net.setPreferableTarget(cv.dnn.DNN_TARGET_CUDA_FP16)
15+
16+
model = cv.dnn_DetectionModel(net)
17+
model.setInputParams(size=(416, 416), scale=1/255, swapRB=True)
18+
19+
20+
cap = cv.VideoCapture('new.mp4')
21+
frame_width = cap.get(cv.CAP_PROP_FRAME_WIDTH)
22+
frame_height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)
23+
print(frame_height, frame_width)
24+
fourcc = cv.VideoWriter_fourcc('M', 'J', 'P', 'G')
25+
# cap.set(cv.CAP_PROP_FPS, 7)
26+
dim = (int(frame_width), int(frame_height))
27+
out = cv.VideoWriter('OutputVideo2.avi', fourcc, 30.0, dim)
28+
starting_time = time.time()
29+
frame_counter = 0
30+
while True:
31+
ret, frame = cap.read()
32+
33+
frame_counter += 1
34+
if ret == False:
35+
break
36+
37+
# if frame_counter == 100:
38+
# break
39+
40+
frame = cv.resize(frame, dim, interpolation=cv.INTER_AREA)
41+
classes, scores, boxes = model.detect(frame, Conf_threshold, NMS_threshold)
42+
for (classid, score, box) in zip(classes, scores, boxes):
43+
color = COLORS[int(classid) % len(COLORS)]
44+
label = "%s : %f" % (class_name[classid[0]], score)
45+
cv.rectangle(frame, box, color, 4)
46+
cv.putText(frame, label, (box[0], box[1]-10),
47+
cv.FONT_HERSHEY_COMPLEX, 0.6, color, 4)
48+
endingTime = time.time() - starting_time
49+
fps = frame_counter/endingTime
50+
# print(fps)
51+
cv.putText(frame, f'FPS: {round(fps,2)}', (20, 50),
52+
cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2)
53+
# cv.imshow('frame', frame)
54+
55+
out.write(frame)
56+
key = cv.waitKey(1)
57+
if key == ord('q'):
58+
break
59+
out.release()
60+
61+
cap.release()
62+
cv.destroyAllWindows()
63+
print('done')

0 commit comments

Comments
 (0)