-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpredict.py
141 lines (117 loc) · 4.48 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
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
#! /usr/bin/env python3
from tqdm import tqdm
from utils import draw_boxes, get_session
from frontend import YOLO
from utils import list_images
import tensorflow as tf
import numpy as np
import argparse
import os
import cv2
import keras
import json
import time
from imutils.video import FileVideoStream, VideoStream, FPS
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
argparser = argparse.ArgumentParser(
description='Train and validate YOLO_v2 model on any dataset')
argparser.add_argument(
'-c',
'--conf',
default='config.json',
help='path to configuration file')
argparser.add_argument(
'-w',
'--weights',
default='',
help='path to pretrained weights')
argparser.add_argument(
'-i',
'--input',
help='path to an image or an video (mp4 format)')
def _main_(args):
config_path = args.conf
weights_path = args.weights
image_path = args.input
keras.backend.tensorflow_backend.set_session(get_session())
with open(config_path) as config_buffer:
config = json.load(config_buffer)
if weights_path == '':
weights_path = config['train']['pretrained_weights"']
###############################
# Make the model
###############################
input_size = (config['model']['input_size_h'],config['model']['input_size_w'])
yolo = YOLO(backend = config['model']['backend'],
input_size = (config['model']['input_size_h'],config['model']['input_size_w']),
labels = config['model']['labels'],
max_box_per_image = config['model']['max_box_per_image'],
anchors = config['model']['anchors'],
gray_mode = config['model']['gray_mode'])
if config['model']['gray_mode']:
depth = 1
else:
depth = 3
yolo.load_weights(weights_path)
if image_path[-4:] == '.mp4':
video_out = image_path[:-4] + '_detected' + image_path[-4:]
#cap = FileVideoStream(image_path).start()
cap = cv2.VideoCapture(image_path)
time.sleep(1.0)
# fps = FPS().start()
fps_img = 0.0
counter = 0
while True:
start = time.time()
ret, image = cap.read()
if depth == 1:
# convert video to gray
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC)
image = np.expand_dims(image, 2)
#image = np.array(image, dtype='f')
else:
if counter == 1:
print("Color image")
image = cv2.resize(image, input_size, interpolation = cv2.INTER_CUBIC)
#image = np.array(image, dtype='f')
#image = np.divide(image, 255.)
tm_inf = time.time()
boxes = yolo.predict(image)
fps_img = ( fps_img + ( 1 / (time.time() - start) ) ) / 2
print( "Inference time: {:.4f}".format(time.time() - tm_inf) )
image = draw_boxes(image, boxes, config['model']['labels'])
image = cv2.putText(image, "fps: {:.2f}".format(fps_img), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 1, 4 )
cv2.imshow("Press q to quit", image)
# fps.update()
#if counter == 10:
#print(image.sum(), boxes)
# time.sleep(1)
counter += 1
if cv2.getWindowProperty( "Press q to quit", cv2.WND_PROP_ASPECT_RATIO ) < 0.0:
print("Window closed" )
break
elif cv2.waitKey( 1 ) & 0xFF == ord( 'q' ):
print( "Q pressed" )
break
# fps.stop()
# print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
# print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cap.release()
else:
images = list(list_images(image_path))
for fname in images[100:]:
image = cv2.imread(fname)
tm_inf = time.time()
boxes = yolo.predict(image)
print( "Inference time: {:.4f}".format(time.time() - tm_inf) )
image = draw_boxes(image, boxes, config['model']['labels'])
cv2.imshow("Press q to quit", image)
if cv2.waitKey( 1 ) & 0xFF == ord( 'q' ):
break
time.sleep(2)
cv2.destroyAllWindows()
if __name__ == '__main__':
args = argparser.parse_args()
_main_(args)