/
image_classifier_demo.cc
197 lines (178 loc) · 7.81 KB
/
image_classifier_demo.cc
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Copyright 2020 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.
==============================================================================*/
// Example usage:
// bazel run -c opt \
// tensorflow_lite_support/examples/task/vision/desktop:image_classifier_demo \
// -- \
// --model_path=/path/to/model.tflite \
// --image_path=/path/to/image.jpg
#include <iostream>
#include "absl/flags/flag.h" // from @com_google_absl
#include "absl/flags/parse.h" // from @com_google_absl
#include "absl/status/status.h" // from @com_google_absl
#include "absl/strings/str_format.h" // from @com_google_absl
#include "tensorflow_lite_support/cc/port/statusor.h"
#include "tensorflow_lite_support/cc/task/core/external_file_handler.h"
#include "tensorflow_lite_support/cc/task/core/proto/external_file_proto_inc.h"
#include "tensorflow_lite_support/cc/task/vision/image_classifier.h"
#include "tensorflow_lite_support/cc/task/vision/proto/class_proto_inc.h"
#include "tensorflow_lite_support/cc/task/vision/proto/classifications_proto_inc.h"
#include "tensorflow_lite_support/cc/task/vision/proto/image_classifier_options_proto_inc.h"
#include "tensorflow_lite_support/cc/task/vision/utils/frame_buffer_common_utils.h"
#include "tensorflow_lite_support/cc/task/vision/utils/image_utils.h"
ABSL_FLAG(std::string, model_path, "",
"Absolute path to the '.tflite' image classifier model.");
ABSL_FLAG(std::string, image_path, "",
"Absolute path to the image to classify. The image must be RGB or "
"RGBA (grayscale is not supported). The image EXIF orientation "
"flag, if any, is NOT taken into account.");
ABSL_FLAG(int32, max_results, 5,
"Maximum number of classification results to display.");
ABSL_FLAG(float, score_threshold, 0,
"Classification results with a confidence score below this value are "
"rejected. If >= 0, overrides the score threshold(s) provided in the "
"TFLite Model Metadata. Ignored otherwise.");
ABSL_FLAG(
std::vector<std::string>, class_name_whitelist, {},
"Comma-separated list of class names that acts as a whitelist. If "
"non-empty, classification results whose 'class_name' is not in this list "
"are filtered out. Mutually exclusive with 'class_name_blacklist'.");
ABSL_FLAG(
std::vector<std::string>, class_name_blacklist, {},
"Comma-separated list of class names that acts as a blacklist. If "
"non-empty, classification results whose 'class_name' is in this list "
"are filtered out. Mutually exclusive with 'class_name_whitelist'.");
ABSL_FLAG(bool, use_coral, false,
"If true, inference will be delegated to a connected Coral Edge TPU "
"device.");
namespace tflite {
namespace task {
namespace vision {
namespace {
using std::chrono::microseconds;
using std::chrono::steady_clock;
} // namespace
ImageClassifierOptions BuildOptions() {
ImageClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(
absl::GetFlag(FLAGS_model_path));
options.set_max_results(absl::GetFlag(FLAGS_max_results));
if (absl::GetFlag(FLAGS_score_threshold) >= 0) {
options.set_score_threshold(absl::GetFlag(FLAGS_score_threshold));
}
for (const std::string& class_name :
absl::GetFlag(FLAGS_class_name_whitelist)) {
options.add_class_name_whitelist(class_name);
}
for (const std::string& class_name :
absl::GetFlag(FLAGS_class_name_blacklist)) {
options.add_class_name_blacklist(class_name);
}
if (absl::GetFlag(FLAGS_use_coral)) {
options.mutable_base_options()
->mutable_compute_settings()
->mutable_tflite_settings()
->set_delegate(::tflite::proto::Delegate::EDGETPU_CORAL);
}
return options;
}
void DisplayResult(const ClassificationResult& result) {
std::cout << "Results:\n";
for (int head = 0; head < result.classifications_size(); ++head) {
if (result.classifications_size() > 1) {
std::cout << absl::StrFormat(" Head index %d:\n", head);
}
const Classifications& classifications = result.classifications(head);
for (int rank = 0; rank < classifications.classes_size(); ++rank) {
const Class& classification = classifications.classes(rank);
std::cout << absl::StrFormat(" Rank #%d:\n", rank);
std::cout << absl::StrFormat(" index : %d\n",
classification.index());
std::cout << absl::StrFormat(" score : %.5f\n",
classification.score());
if (classification.has_class_name()) {
std::cout << absl::StrFormat(" class name : %s\n",
classification.class_name());
}
if (classification.has_display_name()) {
std::cout << absl::StrFormat(" display name: %s\n",
classification.display_name());
}
}
}
}
absl::Status Classify() {
// Build ImageClassifier.
const ImageClassifierOptions& options = BuildOptions();
ASSIGN_OR_RETURN(std::unique_ptr<ImageClassifier> image_classifier,
ImageClassifier::CreateFromOptions(options));
// Load image in a FrameBuffer.
ASSIGN_OR_RETURN(ImageData image,
DecodeImageFromFile(absl::GetFlag(FLAGS_image_path)));
std::unique_ptr<FrameBuffer> frame_buffer;
if (image.channels == 3) {
frame_buffer =
CreateFromRgbRawBuffer(image.pixel_data, {image.width, image.height});
} else if (image.channels == 4) {
frame_buffer =
CreateFromRgbaRawBuffer(image.pixel_data, {image.width, image.height});
} else {
return absl::InvalidArgumentError(absl::StrFormat(
"Expected image with 3 (RGB) or 4 (RGBA) channels, found %d",
image.channels));
}
// Run classification and display results.
auto start_classify = steady_clock::now();
ASSIGN_OR_RETURN(ClassificationResult result,
image_classifier->Classify(*frame_buffer));
auto end_classify = steady_clock::now();
std::string delegate =
absl::GetFlag(FLAGS_use_coral) ? "Coral Edge TPU" : "CPU";
std::cout << "Time cost to classify the input image on " << delegate << ": "
<< std::chrono::duration<float, std::milli>(end_classify -
start_classify)
.count()
<< " ms" << std::endl;
DisplayResult(result);
// Cleanup and return.
ImageDataFree(&image);
return absl::OkStatus();
}
} // namespace vision
} // namespace task
} // namespace tflite
int main(int argc, char** argv) {
// Parse command line arguments and perform sanity checks.
absl::ParseCommandLine(argc, argv);
if (absl::GetFlag(FLAGS_model_path).empty()) {
std::cerr << "Missing mandatory 'model_path' argument.\n";
return 1;
}
if (absl::GetFlag(FLAGS_image_path).empty()) {
std::cerr << "Missing mandatory 'image_path' argument.\n";
return 1;
}
if (!absl::GetFlag(FLAGS_class_name_whitelist).empty() &&
!absl::GetFlag(FLAGS_class_name_blacklist).empty()) {
std::cerr << "'class_name_whitelist' and 'class_name_blacklist' arguments "
"are mutually exclusive.\n";
return 1;
}
// Run classification.
absl::Status status = tflite::task::vision::Classify();
if (status.ok()) {
return 0;
} else {
std::cerr << "Classification failed: " << status.message() << "\n";
return 1;
}
}