-
Notifications
You must be signed in to change notification settings - Fork 1
/
TM2_tflite.py
88 lines (64 loc) · 2.3 KB
/
TM2_tflite.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append("/usr/lib/python3/dist-packages")
import io
import time
import numpy as np
#import picamera
from PIL import Image
from tflite_runtime.interpreter import Interpreter
from line import line_bot
from config import *
from args import load_args
from OpenCV import *
def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def classify_image(interpreter, image, top_k=1):
"""Returns a sorted array of classification results."""
set_input_tensor(interpreter, image)
interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = np.squeeze(interpreter.get_tensor(output_details['index']))
# If the model is quantized (uint8 data), then dequantize the results
if output_details['dtype'] == np.uint8:
scale, zero_point = output_details['quantization']
output = scale * (output - zero_point)
ordered = np.argpartition(-output, top_k)
return [(i, output[i]) for i in ordered[:top_k]]
def main():
args, labels = load_args()
interpreter = Interpreter(args.model)
interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape']
#with picamera.PiCamera(resolution=(640, 480), framerate=30) as camera:
#camera.start_preview()
cap = set_cap()
times=1
old_labels = ""
frame_count = 0
diff_frame_count = 0
while True:
image = read_cap(cap)
start_time = time.time()
results = classify_image(interpreter, image)
elapsed_ms = (time.time() - start_time) * 1000
label_id, prob = results[0]
if frame_count == 0:
old_labels = labels[label_id]
if old_labels != labels[label_id]:
diff_frame_count = diff_frame_count+1
else:
diff_frame_count = 0
if diff_frame_count>=THRESHOLD:
line_bot("this is "+ labels[label_id])
old_labels = labels[label_id]
diff_frame_count = 0
frame_count = frame_count + 1
print("diff_frame_count:"+str(diff_frame_count) +" tensor id:" + labels[label_id] + " and old id: " + str(old_labels))
close_cap()
if __name__ == '__main__':
main()