forked from experiencor/keras-yolo2
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpredict.py
143 lines (119 loc) · 4.59 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
142
143
#! /usr/bin/env python3
from keras_yolov2.utils import draw_boxes, enable_memory_growth
from keras_yolov2.frontend import YOLO
from keras_yolov2.utils import list_images
from tqdm import tqdm
import numpy as np
import argparse
import json
import cv2
import os
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(
'-r',
'--real_time',
default=False,
type=bool,
help='use a camera for real time prediction')
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
use_camera = args.real_time
videos_format = [".mp4", "avi"]
enable_memory_growth()
with open(config_path) as config_buffer:
config = json.load(config_buffer)
if weights_path == '':
weights_path = config['train']['pretrained_weights"']
###################
# Make the model
###################
yolo = YOLO(backend=config['model']['backend'],
input_size=(config['model']['input_size_h'], config['model']['input_size_w']),
labels=config['model']['labels'],
anchors=config['model']['anchors'],
gray_mode=config['model']['gray_mode'])
#########################
# Load trained weights
#########################
yolo.load_weights(weights_path)
###########################
# Predict bounding boxes
###########################
if use_camera:
video_reader = cv2.VideoCapture(int(image_path))
pbar = tqdm()
while True:
pbar.update(1)
ret, frame = video_reader.read()
if not ret:
break
boxes = yolo.predict(frame)
frame = draw_boxes(frame, boxes, config['model']['labels'])
cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == ord("q") or key == 27:
break
pbar.close()
elif os.path.splitext(image_path)[1] in videos_format:
file, ext = os.path.splitext(image_path)
video_out = '{}_detected.avi'.format(file)
video_reader = cv2.VideoCapture(image_path)
nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
print(video_out)
video_writer = cv2.VideoWriter(video_out,
cv2.VideoWriter_fourcc(*'XVID'),
50.0,
(frame_w, frame_h))
for _ in tqdm(range(nb_frames)):
_, image = video_reader.read()
boxes = yolo.predict(image,
iou_threshold=config['valid']['iou_threshold'],
score_threshold=config['valid']['score_threshold'])
image = draw_boxes(image, boxes, config['model']['labels'])
video_writer.write(np.uint8(image))
video_reader.release()
video_writer.release()
else:
if os.path.isfile(image_path):
image = cv2.imread(image_path)
boxes = yolo.predict(image,
iou_threshold=config['valid']['iou_threshold'],
score_threshold=config['valid']['score_threshold'])
image = draw_boxes(image, boxes, config['model']['labels'])
print(len(boxes), 'boxes are found')
cv2.imwrite(image_path[:-4] + '_detected' + image_path[-4:], image)
else:
detected_images_path = os.path.join(image_path, "detected")
if not os.path.exists(detected_images_path):
os.mkdir(detected_images_path)
images = list(list_images(image_path))
for fname in tqdm(images):
image = cv2.imread(fname)
boxes = yolo.predict(image)
image = draw_boxes(image, boxes, config['model']['labels'])
fname = os.path.basename(fname)
cv2.imwrite(os.path.join(image_path, "detected", fname), image)
if __name__ == '__main__':
_args = argparser.parse_args()
_main_(_args)