-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
56 lines (48 loc) · 1.57 KB
/
script.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
from darkflow.net.build import TFNet
import cv2
import tensorflow as tf
from time import time as timer
import sys
import os
input_file = sys.argv[1]
output_file = sys.argv[2]
#options of darkflow - model, weights
options = {"model": "cfg/yolo_new.cfg", "load": -1, "threshold": 0.65, "gpu":0.7}
#create a tfnet object
tfnet = TFNet(options)
#read video from file
cap = cv2.VideoCapture(input_file)
#cap = cv2.VideoCapture(0)
#save parameters of video
width=int(cap.get(3))
height=int(cap.get(4))
frame_rate=int(cap.get(5))
#initializing videocodec and video writer
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter(output_file,fourcc, frame_rate, (width,height))
elapsed = int()
start = timer()
#reading video file an image by image while there is images
#to see images in realtimework you can uncomment string cv2.imshow('',preprocess)
while cap.isOpened():
ret, frame = cap.read()
if ret==False: break
preprocessed = tfnet.framework.preprocess(frame)
feed_dict = {tfnet.inp: [preprocessed]}
net_out = tfnet.sess.run(tfnet.out,feed_dict)[0]
processed = tfnet.framework.postprocess(net_out, frame, False)
out.write(processed)
#cv2.imshow('', processed)
elapsed += 1
if elapsed % 5 == 0:
sys.stdout.write('\r')
sys.stdout.write('{0:3.3f} FPS'.format(
elapsed / (timer() - start)))
sys.stdout.flush()
choice = cv2.waitKey(1)
if choice == 27: break
#writing video to file
sys.stdout.write('\n')
cap.release()
out.release()
cv2.destroyAllWindows()