forked from thtrieu/darkflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
68 lines (56 loc) · 2.28 KB
/
predict.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
import os
import cv2
import numpy as np
from config_options import options, base_data_dir
from darkflow.net.build import TFNet
options = options(base_data_dir, cfg_file="yolov2.cfg", weights_file="yolov2.weights", train=False)
tfnet = TFNet(options)
tfnet.predict()
def boxing(original_img, box):
if box["confidence"] > options["threshold"]:
original_img = cv2.rectangle(original_img,
(box['topleft']['x'], box['topleft']['y']),
(box['bottomright']['x'], box['bottomright']['y']),
box['color'], box["thick"] // 2)
original_img = cv2.putText(original_img, box['label'],
(box['topleft']['x'], box['topleft']['y'] - 12), 0,
1e-3 * original_img.shape[0], box['color'], int(box["thick"] // 3))
return original_img
def predict_on_image():
path = options["imgdir"]
for image in os.listdir(path):
if image == "out":
continue
original_img = cv2.imread(path + image)
# original_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB)
results = tfnet.return_predict(original_img)
if not results:
continue
print("For image: {} \n Results: \n {}".format(image, results))
for box in results:
boxing(original_img, box)
cv2.imwrite(path + 'out/' + 'prediction_' + image, original_img)
def predict_on_video():
cap = cv2.VideoCapture('GP010080.mov')
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('/outputs/GP010080_preds.mov', fourcc, 20.0, (int(width), int(height)))
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
frame = np.asarray(frame)
results = tfnet.return_predict(frame)
for box in results:
new_frame = boxing(frame, box)
# Display the resulting frame
out.write(new_frame)
else:
break
# When everything done, release the capture
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
predict_on_image()