-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdemo_video.py
executable file
·144 lines (109 loc) · 4.51 KB
/
demo_video.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
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
import os
from argparse import ArgumentParser
import darknet.python.darknet as dn
import time
#cfg_path = "/home/czchen/stevenwork/plate_detection_ML_hw3/darknet/cfg/yolov3_plate.cfg"
#weights_path = "/data1000G/steven/backup_v0/yolov3_plate_final.weights"
parser = ArgumentParser(description='demo application for video')
parser.add_argument(
'--cfg_path', type=str, default="/home/czchen/stevenwork/plate_detection_ML_hw3/darknet/cfg/yolov3_1_cls.cfg",
help='path of the existed pokemon video. ')
parser.add_argument(
'--weights_path', type=str, default="/data1000G/steven/backup/yolov3_1_cls_final.weights",
help='path of the existed pokemon video. ')
parser.add_argument(
'--meta_path', type=str, default="/home/czchen/stevenwork/plate_detection_ML_hw3/darknet/cfg/plate.data",
help='path of the existed pokemon video. ')
parser.add_argument(
'--input_video_dir', type=str, default='/data1000G/steven/video/',
help='path of the existed pokemon video. ')
parser.add_argument(
'--input_video_name', type=str, default='pokemon_1mp4',
help='path of the existed pokemon video. ')
parser.add_argument(
'--output_video_dir', type=str, default='video_output',
help='output dir of the pokemon video after processing')
parser.add_argument(
'--output_video_name', type=str, default='output',
help='output dir of the pokemon video after processing')
def video_process(args):
dn.perform_detect(args.cfg_path, args.weights_path, args.meta_path, None, init=True)
cap = cv2.VideoCapture(os.path.join(args.input_video_dir, args.input_video_name))
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
if not os.path.isdir(args.output_video_dir):
os.mkdir(args.output_video_dir)
avi = True
if avi == True:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_name = os.path.join(args.output_video_dir, args.output_video_name) + '.avi'
else:
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
output_name = os.path.join(args.output_video_dir, args.output_video_name) + '.mp4'
#fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
out = cv2.VideoWriter(output_name, fourcc, fps//5, size)
iteration = 1
while(cap.isOpened()):
frame_buffer = []
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is false
if ret == False and type(frame) is type(None):
break
#horizontal_frame = cv2.flip(frame, 1)
frame_buffer.append(frame)
#frame_buffer.append(horizontal_frame)
# Our operations on the frame come here
if iteration % 5 == 0:
frame_buffer = np.array(frame_buffer)
start_time=time.time()
batch_imgs = perform_predict(args, frame_buffer, frame_buffer.shape[0])
end_time=time.time()
print(str(round(1/(end_time-start_time), 2))+'fps')
for i in range(0,len(batch_imgs)):
out.write(batch_imgs[i])
frame_buffer = []
# Display the resulting frame
#cv2.imshow('frame', frame)
iteration += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
out.release()
cap.release()
cv2.destroyAllWindows()
print('Video file has been written to ', output_name)
def draw_chinese_to_img(img, word, place, color, size):
from PIL import ImageFont, ImageDraw, Image
color += (0,)
fontpath = "./font/SourceHanSerifTC-Bold.otf" # <== 這裡是宋體路徑
font_size = int(60 * (size[0]*size[1]) / (1280*720))
font = ImageFont.truetype(fontpath, font_size)
img_pil = Image.fromarray(img)
draw = ImageDraw.Draw(img_pil)
draw.text(place, word, font=font, fill=color)
_img = np.array(img_pil)
return _img
def perform_predict(args, _ims, batch):
"""
im: 4D-array_batches image [batch, h, w, c]
return type: 4D-array_batches image [batch, h, w, c] that bbox is drawed
"""
det_ims = []
for _im in _ims:
det_im, ret = dn.perform_detect(args.cfg_path, args.weights_path, args.meta_path, _im)
det_ims.append(det_im)
return det_ims
def main():
args = parser.parse_args()
print('args--------------------')
print(args)
print('------------------------')
print()
video_process(args)
if __name__ == "__main__":
main()