-
Notifications
You must be signed in to change notification settings - Fork 0
/
recog_image.py
74 lines (53 loc) · 2.1 KB
/
recog_image.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import numpy as np
from PIL import Image
import tensorflow as tf
print(tf.VERSION)
def get_input_paras():
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--image_file", default="daisy.bmp", \
help="image to recognize")
parser.add_argument("-W", "--input_width", default=128, \
help="image width the model accepts as input")
parser.add_argument("-H", "--input_height", default=128, \
help="image height the model accepts as input")
parser.add_argument("-l", "--tflite_file", default="my_tflite_model.tflite", \
help="Tflite model file used to recognize image")
parser.add_argument("-v", "--verbose", default=0, \
help="print more information if set to 1")
args = parser.parse_args()
return args
def prepare_image(args):
img = Image.open(args.image_file)
img = img.resize((args.input_width, args.input_height))
# add N dim
image = np.expand_dims(img, axis=0)
image = (np.float32(image))
image /= 255.
return image
def recognize_image(args, img_tensor):
# Create a tf-lite interpreater from the tf-lite model file.
# We are invoking the Tf-lite model to recognize an image at RPI.
f = tf.contrib.lite.Interpreter(args.tflite_file)
f.allocate_tensors()
i = f.get_input_details()[0]
if int(args.verbose) == 1:
print("=== input of tflite model: ", i)
o = f.get_output_details()[0]
if int(args.verbose) == 1:
print("=== output of tflite model: ", o)
f.set_tensor(i['index'], img_tensor)
f.invoke()
y = f.get_tensor(o['index'])
if int(args.verbose) == 1:
print("=== Results from recognizing the image with Tf-lite model: ", y)
print("\n=== Label of the image that we recognize with Tf-lite model: {:d} \n".format(np.argmax(y)))
if __name__ == "__main__":
# Parse and get parameters that you input on ternimal when you type:
# python3 generate_my_tflite_model.py
args = get_input_paras()
img = prepare_image(args)
recognize_image(args, img)