-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodelo.py
50 lines (42 loc) · 2.05 KB
/
modelo.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
from PIL import Image
import tensorflow as tf
import numpy as np
class Model:
def __init__(self, model_file, dict_file):
with open(dict_file, 'r') as f:
self.labels = [line.strip().replace('_', ' ') for line in f.readlines()]
self.interpreter = tf.lite.Interpreter(model_path=model_file)
self.interpreter.allocate_tensors()
self.input_details = self.interpreter.get_input_details()
self.output_details = self.interpreter.get_output_details()
self.floating_model = self.input_details[0]['dtype'] == np.float32
self.height = self.input_details[0]['shape'][1]
self.width = self.input_details[0]['shape'][2]
def classify(self, file, maxResults, min_confidence):
# print("Confidence level %f" % min_confidence);
with Image.open(file).convert('RGB').resize((self.width, self.height)) as img:
input_data = np.expand_dims(img, axis=0)
if self.floating_model:
input_data = (np.float32(input_data) - 127.5) / 127.5
self.interpreter.set_tensor(self.input_details[0]['index'], input_data)
self.interpreter.invoke()
output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
results = np.squeeze(output_data)
top_categories = results.argsort()[::-1]
if maxResults != None:
top_categories = top_categories[:maxResults]
# print("==> %s <==" % file)
final_results = []
used_labels = set()
for i in top_categories:
if self.floating_model:
r = float(results[i])
else:
r = float(results[i] / 255.0)
if min_confidence != None and r < min_confidence:
break
if self.labels[i] not in used_labels:
res = ("{:6.2f}%".format(r*100), self.labels[i])
final_results.append(res)
used_labels.add(self.labels[i])
return final_results