-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclassifier.py
116 lines (94 loc) · 3.82 KB
/
classifier.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
import numpy as np
import json
#import tensorflow as tf
#import tensorflow.compat.v1 as tf
from PIL import Image, ImageOps
import cv2
import io
import config
model_file = config.model_file
label_file = config.label_file
input_layer = config.input_layer
output_layer = config.output_layer
classifier_input_size = config.classifier_input_size
def load_graph(model_file):
graph = tf.Graph()
graph_def = tf.GraphDef()
with open(model_file, "rb") as f:
graph_def.ParseFromString(f.read())
with graph.as_default():
tf.import_graph_def(graph_def)
return graph
def load_labels(label_file):
label = []
with open(label_file, "r", encoding='cp1251') as ins:
for line in ins:
label.append(line.rstrip())
return label
def resizeAndPad(img, size, padColor=0):
h, w = img.shape[:2]
sh, sw = size
# interpolation method
if h > sh or w > sw: # shrinking image
interp = cv2.INTER_AREA
else: # stretching image
interp = cv2.INTER_CUBIC
# aspect ratio of image
aspect = w/h # if on Python 2, you might need to cast as a float: float(w)/h
# compute scaling and pad sizing
if aspect > 1: # horizontal image
new_w = sw
new_h = np.round(new_w/aspect).astype(int)
pad_vert = (sh-new_h)/2
pad_top, pad_bot = np.floor(pad_vert).astype(int), np.ceil(pad_vert).astype(int)
pad_left, pad_right = 0, 0
elif aspect < 1: # vertical image
new_h = sh
new_w = np.round(new_h*aspect).astype(int)
pad_horz = (sw-new_w)/2
pad_left, pad_right = np.floor(pad_horz).astype(int), np.ceil(pad_horz).astype(int)
pad_top, pad_bot = 0, 0
else: # square image
new_h, new_w = sh, sw
pad_left, pad_right, pad_top, pad_bot = 0, 0, 0, 0
# set pad color
if len(img.shape) is 3 and not isinstance(padColor, (list, tuple, np.ndarray)): # color image but only one color provided
padColor = [padColor]*3
# scale and pad
scaled_img = cv2.resize(img, (new_w, new_h), interpolation=interp)
scaled_img = cv2.copyMakeBorder(scaled_img, pad_top, pad_bot, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT, value=padColor)
return scaled_img
class Classifier():
def __init__(self):
# uncomment the next 3 lines if you want to use CPU instead of GPU
#import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
self.graph = load_graph(model_file)
self.labels = load_labels(label_file)
input_name = "import/" + input_layer
output_name = "import/" + output_layer
self.input_operation = self.graph.get_operation_by_name(input_name)
self.output_operation = self.graph.get_operation_by_name(output_name)
self.sess = tf.Session(graph=self.graph)
self.sess.graph.finalize() # Graph is read-only after this statement.
def predict(self, img):
img = img[:, :, ::-1]
img = resizeAndPad(img, classifier_input_size)
# Add a forth dimension since Tensorflow expects a list of images
img = np.expand_dims(img, axis=0)
# Scale the input image to the range used in the trained network
img = img.astype(np.float32)
img /= 127.5
img -= 1.
results = self.sess.run(self.output_operation.outputs[0], {
self.input_operation.outputs[0]: img
})
results = np.squeeze(results)
top = 3
top_indices = results.argsort()[-top:][::-1]
classes = []
for ix in top_indices:
make_model = self.labels[ix].split('\t')
classes.append({"make": make_model[0], "model": make_model[1], "prob": str(results[ix])})
return(classes)