forked from fizyr/keras-retinanet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_detection_file.py
127 lines (102 loc) · 4.29 KB
/
generate_detection_file.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
##########################Load necessary modules################
# show images inline
#%matplotlib inline
# automatically reload modules when they have changed
#%load_ext autoreload
#%autoreload 2
# import keras
import keras
# import keras_retinanet
from keras_retinanet import models
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
from keras_retinanet.utils.visualization import draw_box, draw_caption
from keras_retinanet.utils.colors import label_color
# import miscellaneous modules
import matplotlib.pyplot as plt
import cv2
import os
import numpy as np
import time
import argparse
# set tf backend to allow memory to grow, instead of claiming everything
import tensorflow as tf
def arg_parse():
"""
Parse arguements to the detect module
"""
parser = argparse.ArgumentParser(description='YOLO v3 Video Detection Module')
parser.add_argument("--imgpath", dest = 'image',help =
"Path to image")
parser.add_argument("--modelpath", dest = 'model',help =
"Path to image")
return parser.parse_args()
def get_session():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
return tf.Session(config=config)
# use this environment flag to change which GPU to use
#os.environ["CUDA_VISIBLE_DEVICES"] = "1"
# set the modified tf session as backend in keras
keras.backend.tensorflow_backend.set_session(get_session())
if __name__ == '__main__':
args = arg_parse()
img = args.image
model_path = args.model
####################Load RetinaNet model##########################
# adjust this to point to your downloaded/trained model
# models can be downloaded here: https://github.com/fizyr/keras-retinanet/releases
#model_path = os.path.join('..', 'snapshots', 'resnet50_coco_best_v2.1.0.h5')
# load retinanet model
model = models.load_model(model_path, backbone_name='resnet101')
# if the model is not converted to an inference model, use the line below
# see: https://github.com/fizyr/keras-retinanet#converting-a-training-model-to-inference-model
#model = models.convert_model(model)
#print(model.summary())
# load label to names mapping for visualization purposes
labels_to_names = {
1:'pedestrian',
2:'people',
3:'bicycle',
4:'car',
5:'van',
6:'truck',
7:'tricycle',
8:'awning-tricycle',
9:'bus',
10:'motor'}
#######################Run detection on examples##########################
images_path = "/mnt/data/nhuthuynh/sequences/"
results_path = "./results"
for folder in os.listdir(images_path):
folder_path = os.path.join(images_path,folder)
for image in os.listdir(folder_path):
image_path = os.path.join(folder_path,image)
print(image_path)
# load image
im = read_image_bgr(image_path)
# copy to draw on
draw = im.copy()
draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
# preprocess image for network
im = preprocess_image(im)
im, scale = resize_image(im)
# process image
start = time.time()
boxes, scores, labels = model.predict_on_batch(np.expand_dims(im, axis=0))
# correct for image scale
boxes /= scale
file_name = folder+'_'+os.path.splitext(image)[0]+'.txt' #detected result file
file_path = os.path.join(results_path,file_name) # full file path of detected result file
with open(file_path,'w') as file:
for box, score, label in zip(boxes[0], scores[0], labels[0]):
# scores are sorted so we can break
# if (int(label) != 1 and int(label)!=2):
# continue
if score < 0.5:
break
x = box[0]
y = box[1]
w = abs(box[2]-x)
h = abs(box[3]-y)
file.write(str(labels_to_names[label])+','+str(int(x))+','+str(int(y))+','+str(int(w))+','+str(int(h)))
file.write('\n')