forked from stevefielding/tensorflow-anpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_images.py
167 lines (141 loc) · 7.13 KB
/
predict_images.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Predict license plate boxes and license plate characters in images. Display the labelled images
# USAGE
# Example using single stage Faster RCNN
# python predict_images.py --model datasets/experiment_faster_rcnn/2018_07_25_14-00/exported_model/frozen_inference_graph.pb \
# --pred_stages 1 \
# --labels datasets/records/classes.pbtxt \
# --imagePath images/SJ7STAR_images/2018_02_24_9-00 \
# --num-classes 37 \
# --image_display True
# python predict_images.py --model datasets/experiment_ssd/2018_07_25_14-00/exported_model/frozen_inference_graph.pb \
# --pred_stages 2 \
# --labels datasets/records/classes.pbtxt \
# --imagePath images/SJ7STAR_images/2018_02_24_9-00 \
# --num-classes 37 \
# --image_display True
# import the necessary packages
import argparse
import time
import cv2
import numpy as np
import tensorflow as tf
from imutils import paths
from object_detection.utils import label_map_util
from base2designs.plates.plateFinder import PlateFinder
from base2designs.plates.predicter import Predicter
from base2designs.plates.plateDisplay import PlateDisplay
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def predictImages(modelArg, labelsArg, imagePathArg, num_classesArg, min_confidenceArg, image_displayArg, pred_stagesArg):
# initialize the model
model = tf.Graph()
# create a context manager that makes this model the default one for
# execution
with model.as_default():
# initialize the graph definition
graphDef = tf.GraphDef()
# load the graph from disk
with tf.gfile.GFile(modelArg, "rb") as f:
serializedGraph = f.read()
graphDef.ParseFromString(serializedGraph)
tf.import_graph_def(graphDef, name="")
# load the class labels from disk
labelMap = label_map_util.load_labelmap(labelsArg)
categories = label_map_util.convert_label_map_to_categories(
labelMap, max_num_classes=num_classesArg,
use_display_name=True)
categoryIdx = label_map_util.create_category_index(categories)
# create a plateFinder
plateFinder = PlateFinder(min_confidenceArg, categoryIdx,
rejectPlates=False, charIOUMax=0.3)
# create plate displayer
plateDisplay = PlateDisplay()
# create a session to perform inference
with model.as_default():
with tf.Session(graph=model) as sess:
# create a predicter, used to predict plates and chars
predicter = Predicter(model, sess, categoryIdx)
imagePaths = paths.list_images(imagePathArg)
frameCnt = 0
start_time = time.time()
platesReturn = []
numPlates = 0
# Loop over all the images
for imagePath in imagePaths:
frameCnt += 1
# load the image from disk
print("[INFO] Loading image \"{}\"".format(imagePath))
image = cv2.imread(imagePath)
(H, W) = image.shape[:2]
# If prediction stages == 2, then perform prediction on full image, find the plates, crop the plates from the image,
# and then perform prediction on the plate images
if pred_stagesArg == 2:
# Perform inference on the full image, and then select only the plate boxes
boxes, scores, labels = predicter.predictPlates(image, preprocess=True)
licensePlateFound_pred, plateBoxes_pred, plateScores_pred = plateFinder.findPlatesOnly(boxes, scores, labels)
# loop over the plate boxes, find the chars inside the plate boxes,
# and then scrub the chars with 'processPlates', resulting in a list of final plateBoxes, char texts, char boxes, char scores and complete plate scores
plates = []
for plateBox in plateBoxes_pred:
boxes, scores, labels = predicter.predictChars(image, plateBox)
chars = plateFinder.findCharsOnly(boxes, scores, labels, plateBox, image.shape[0], image.shape[1])
if len(chars) > 0:
plates.append(chars)
else:
plates.append(None)
plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateAverageScores_pred = plateFinder.processPlates(plates, plateBoxes_pred, plateScores_pred)
# If prediction stages == 1, then predict the plates and characters in one pass
elif pred_stagesArg == 1:
# Perform inference on the full image, and then find the plate text associated with each plate
boxes, scores, labels = predicter.predictPlates(image, preprocess=False)
licensePlateFound_pred, plateBoxes_pred, charTexts_pred, charBoxes_pred, charScores_pred, plateScores_pred = plateFinder.findPlates(
boxes, scores, labels)
else:
print("[ERROR] --pred_stages {}. The number of prediction stages must be either 1 or 2".format(pred_stagesArg))
quit()
# Print plate text
for charText in charTexts_pred:
print(" Found: ", charText)
# Display the full image with predicted plates and chars
if image_displayArg == True:
imageLabelled = plateDisplay.labelImage(image, plateBoxes_pred, charBoxes_pred, charTexts_pred)
cv2.imshow("Labelled Image", imageLabelled)
cv2.waitKey(0)
imageResults = []
for i, plateBox in enumerate(plateBoxes_pred):
imageResults.append({ 'plateText': charTexts_pred[i], 'plateBoxLoc': list(plateBox), 'charBoxLocs': list([list(x) for x in charBoxes_pred[i]])})
numPlates += 1
platesReturn.append({'imagePath': imagePath.split("/")[-1], 'imageResults': imageResults})
# print some performance statistics
curTime = time.time()
processingTime = curTime - start_time
fps = frameCnt / processingTime
print("[INFO] Processed {} frames in {:.2f} seconds. Frame rate: {:.2f} Hz".format(frameCnt, processingTime, fps))
#results = results.encode('utf-8')
return {"processingTime": processingTime, "numPlates": numPlates, "numImages": len(platesReturn), "images": platesReturn}
if __name__ == '__main__':
#tf.app.run()
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", required=True,
help="base path for frozen checkpoint detection graph")
ap.add_argument("-l", "--labels", required=True,
help="labels file")
ap.add_argument("-i", "--imagePath", required=True,
help="path to input image path")
ap.add_argument("-n", "--num-classes", type=int, required=True,
help="# of class labels")
ap.add_argument("-c", "--min-confidence", type=float, default=0.5,
help="minimum probability used to filter weak detections")
ap.add_argument("-d", "--image_display", type=str2bool, default=False,
help="Enable display of annotated images")
ap.add_argument("-p", "--pred_stages", type=int, required=True,
help="number of prediction stages")
args = vars(ap.parse_args())
results = predictImages(args["model"], args["labels"], args["imagePath"], args["num_classes"], args["min_confidence"],
args["image_display"], args["pred_stages"])