-
Notifications
You must be signed in to change notification settings - Fork 5
/
classify.py
169 lines (144 loc) · 5.55 KB
/
classify.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
168
169
# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Main script to run image classification."""
import argparse
import sys
import time
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
# Visualization parameters
_ROW_SIZE = 20 # pixels
_LEFT_MARGIN = 24 # pixels
_TEXT_COLOR = (0, 0, 255) # red
_FONT_SIZE = 1
_FONT_THICKNESS = 1
_FPS_AVERAGE_FRAME_COUNT = 10
def run(model: str, max_results: int, score_threshold: float, num_threads: int,
enable_edgetpu: bool, camera_id: int, width: int, height: int) -> None:
"""Continuously run inference on images acquired from the camera.
Args:
model: Name of the TFLite image classification model.
max_results: Max of classification results.
score_threshold: The score threshold of classification results.
num_threads: Number of CPU threads to run the model.
enable_edgetpu: Whether to run the model on EdgeTPU.
camera_id: The camera id to be passed to OpenCV.
width: The width of the frame captured from the camera.
height: The height of the frame captured from the camera.
"""
# Initialize the image classification model
base_options = core.BaseOptions(
file_name=model, use_coral=enable_edgetpu, num_threads=num_threads)
# Enable Coral by this setting
classification_options = processor.ClassificationOptions(
max_results=max_results, score_threshold=score_threshold)
options = vision.ImageClassifierOptions(
base_options=base_options, classification_options=classification_options)
classifier = vision.ImageClassifier.create_from_options(options)
# Variables to calculate FPS
counter, fps = 0, 0
start_time = time.time()
# Start capturing video input from the camera
cap = cv2.VideoCapture(camera_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# Continuously capture images from the camera and run inference
while cap.isOpened():
success, image = cap.read()
if not success:
sys.exit(
'ERROR: Unable to read from webcam. Please verify your webcam settings.'
)
counter += 1
image = cv2.flip(image, 1)
# Convert the image from BGR to RGB as required by the TFLite model.
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Create TensorImage from the RGB image
tensor_image = vision.TensorImage.create_from_array(rgb_image)
# List classification results
categories = classifier.classify(tensor_image)
# Show classification results on the image
for idx, category in enumerate(categories.classifications[0].categories):
category_name = category.category_name
score = round(category.score, 2)
result_text = category_name + ' (' + str(score) + ')'
text_location = (_LEFT_MARGIN, (idx + 2) * _ROW_SIZE)
cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_PLAIN,
_FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)
# Calculate the FPS
if counter % _FPS_AVERAGE_FRAME_COUNT == 0:
end_time = time.time()
fps = _FPS_AVERAGE_FRAME_COUNT / (end_time - start_time)
start_time = time.time()
# Show the FPS
fps_text = 'FPS = ' + str(int(fps))
text_location = (_LEFT_MARGIN, _ROW_SIZE)
cv2.putText(image, fps_text, text_location, cv2.FONT_HERSHEY_PLAIN,
_FONT_SIZE, _TEXT_COLOR, _FONT_THICKNESS)
# Stop the program if the ESC key is pressed.
if cv2.waitKey(1) == 27:
break
cv2.imshow('image_classification', image)
cap.release()
cv2.destroyAllWindows()
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--model',
help='Name of image classification model.',
required=False,
default='efficientnet_lite0.tflite')
parser.add_argument(
'--maxResults',
help='Max of classification results.',
required=False,
default=3)
parser.add_argument(
'--scoreThreshold',
help='The score threshold of classification results.',
required=False,
type=float,
default=0.0)
parser.add_argument(
'--numThreads',
help='Number of CPU threads to run the model.',
required=False,
default=4)
parser.add_argument(
'--enableEdgeTPU',
help='Whether to run the model on EdgeTPU.',
action='store_true',
required=False,
default=False)
parser.add_argument(
'--cameraId', help='Id of camera.', required=False, default=0)
parser.add_argument(
'--frameWidth',
help='Width of frame to capture from camera.',
required=False,
default=640)
parser.add_argument(
'--frameHeight',
help='Height of frame to capture from camera.',
required=False,
default=480)
args = parser.parse_args()
run(args.model, int(args.maxResults),
args.scoreThreshold, int(args.numThreads), bool(args.enableEdgeTPU),
int(args.cameraId), args.frameWidth, args.frameHeight)
if __name__ == '__main__':
main()