Skip to content

Commit f36b70b

Browse files
committed
Added Object Detector
1 parent e3dbfa9 commit f36b70b

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

tensorflow_lite_support/c/task/vision/BUILD

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,20 @@ cc_library_with_tflite(
3131
"//tensorflow_lite_support/cc/task/vision/proto:image_classifier_options_proto_inc",
3232
],
3333
)
34+
35+
cc_library_with_tflite(
36+
name = "object_detector",
37+
hdrs = [
38+
"object_detector.h",
39+
],
40+
tflite_deps = [
41+
"//tensorflow_lite_support/cc/task/vision:object_detector",
42+
],
43+
deps = [
44+
"//tensorflow_lite_support/c:common",
45+
"//tensorflow_lite_support/c/task/core:base_options",
46+
"//tensorflow_lite_support/c/task/processor:classification_options",
47+
"//tensorflow_lite_support/c/task/processor:detection_result",
48+
"//tensorflow_lite_support/c/task/vision/core:frame_buffer",
49+
],
50+
)
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#ifndef TENSORFLOW_LITE_SUPPORT_C_TASK_VISION_IMAGE_CLASSIFIER_H_
16+
#define TENSORFLOW_LITE_SUPPORT_C_TASK_VISION_IMAGE_CLASSIFIER_H_
17+
18+
#include <stdint.h>
19+
20+
#include "tensorflow_lite_support/c/common.h"
21+
#include "tensorflow_lite_support/c/task/core/base_options.h"
22+
#include "tensorflow_lite_support/c/task/processor/classification_options.h"
23+
#include "tensorflow_lite_support/c/task/processor/detection_result.h"
24+
#include "tensorflow_lite_support/c/task/vision/core/frame_buffer.h"
25+
26+
// --------------------------------------------------------------------------
27+
/// C API for Object Detector.
28+
///
29+
/// The API leans towards simplicity and uniformity instead of convenience, as
30+
/// most usage will be by language-specific wrappers. It provides largely the
31+
/// same set of functionality as that of the C++ TensorFlow Lite
32+
/// `ObjectDetector` API, but is useful for shared libraries where having
33+
/// a stable ABI boundary is important.
34+
///
35+
/// Usage:
36+
/// <pre><code>
37+
/// // Create the model
38+
/// Using the options initialized with default values returned by
39+
/// TfLiteObjectDetectorOptionsCreate() makes sure that there will be no
40+
/// undefined behaviour due to garbage values in unitialized members.
41+
/// TfLiteObjectDetectorOptions options = TfLiteObjectDetectorOptionsCreate();
42+
///
43+
/// Set the model file path in options
44+
/// options.base_options.model_file.file_path = "/path/to/model.tflite";
45+
///
46+
/// If need be, set values for any options to customize behaviour.
47+
/// options.base_options.compute_settings.cpu_settings.num_threads = 3
48+
///
49+
/// Create TfLiteObjectDetector using the options:
50+
/// If error information is not nedded in case of failure:
51+
/// TfLiteObjectDetector* object_detector =
52+
/// TfLiteObjectDetectorFromOptions(&options, NULL);
53+
///
54+
/// If error information is nedded in case of failure:
55+
/// TfLiteSupportError* create_error = NULL;
56+
/// TfLiteObjectDetector* object_detector =
57+
/// TfLiteObjectDetectorFromOptions(&options, &create_error);
58+
///
59+
/// if (!object_detector) {
60+
/// Handle failure.
61+
/// Do something with `create_error`, if requested as illustrated above.
62+
/// }
63+
///
64+
/// Dispose of the create_error object.
65+
/// TfLiteSupportErrorDelete(create_error);
66+
///
67+
/// Classify an image
68+
/// TfLiteFrameBuffer frame_buffer = { Initialize with image data }
69+
///
70+
/// If error information is not nedded in case of failure:
71+
/// TfLiteDetectionResult* detection_result =
72+
/// TfLiteObjectDetectorClassify(object_detector, &frame_buffer, NULL);
73+
///
74+
/// If error information is needed in case of failure:
75+
/// TfLiteSupportError* detect_error = NULL;
76+
/// TfLiteDetectionResult* detection_result =
77+
/// TfLiteObjectDetectorDetect(object_detector, &frame_buffer,
78+
/// &detect_error);
79+
///
80+
/// if (!detection_result) {
81+
/// Handle failure.
82+
/// Do something with `detection_error`, if requested as illustrated above.
83+
/// }
84+
///
85+
/// Dispose of the detection_error object.
86+
/// TfLiteSupportErrorDelete(detection_error);
87+
///
88+
/// Dispose of the API object.
89+
/// TfLiteObjectDetectorOptionsDelete(object_detector);
90+
91+
#ifdef __cplusplus
92+
extern "C" {
93+
#endif // __cplusplus
94+
95+
typedef struct TfLiteObjectDetector TfLiteObjectDetector;
96+
97+
typedef struct TfLiteObjectDetectorOptions {
98+
TfLiteClassificationOptions classification_options;
99+
TfLiteBaseOptions base_options;
100+
} TfLiteObjectDetectorOptions;
101+
102+
// Creates and returns TfLiteObjectDetectorOptions initialized with default
103+
// values. Default values are as follows:
104+
// 1. .classification_options.max_results = -1, which returns all classification
105+
// categories by default.
106+
// 2. .base_options.compute_settings.tflite_settings.cpu_settings.num_threads =
107+
// -1, which makes the TFLite runtime choose the value.
108+
// 3. .classification_options.score_threshold = 0
109+
// 4. All pointers like .base_options.model_file.file_path,
110+
// .base_options.classification_options.display_names_local,
111+
// .classification_options.label_allowlist.list,
112+
// options.classification_options.label_denylist.list are NULL.
113+
// 5. All other integer values are initialized to 0.
114+
TfLiteObjectDetectorOptions TfLiteObjectDetectorOptionsCreate();
115+
116+
// Creates TfLiteObjectDetector from options.
117+
// .base_options.model_file.file_path in TfLiteObjectDetectorOptions should be
118+
// set to the path of the tflite model you wish to create the
119+
// TfLiteObjectDetector with.
120+
// Create TfLiteObjectDetectorOptions using
121+
// TfLiteObjectDetectorOptionsCreate(). If need be, you can change the default
122+
// values of options for customizing classification, If options are not created
123+
// in the aforementioned way, you have to make sure that all members are
124+
// initialized to respective default values and all pointer members are zero
125+
// initialized to avoid any undefined behaviour.
126+
//
127+
// Returns the created object detector in case of success.
128+
// Returns nullptr on failure which happens commonly due to one of the following
129+
// issues:
130+
// 1. file doesn't exist or is not a well formatted.
131+
// 2. options is nullptr.
132+
// 3. Both options.classification_options.label_denylist and
133+
// options.classification_options.label_allowlist are non empty. These
134+
// fields are mutually exclusive.
135+
//
136+
// The caller can check if an error was encountered by testing if the returned
137+
// value of the function is null. If the caller doesn't want the reason for
138+
// failure, they can simply pass a NULL for the address of the error pointer as
139+
// shown below:
140+
//
141+
// TfLiteObjectDetector* detector = TfLiteObjectDetectorFromOptions(options,
142+
// NULL);
143+
//
144+
// If the caller wants to be informed of the reason for failure, they must pass
145+
// the adress of a pointer of type TfLiteSupportError to the `error` param as
146+
// shown below:
147+
//
148+
// TfLiteSupport *error = NULL:
149+
// TfLiteObjectDetector* classifier = TfLiteObjectDetectorFromOptions(options,
150+
// &error);
151+
//
152+
// In case of unsuccessful execution, Once the function returns, the error
153+
// pointer will point to a struct containing the error information. If error
154+
// info is passed back to the caller, it is the responsibility of the caller to
155+
// free the error struct by calling the following method defined in common.h:
156+
//
157+
// TfLiteSupportErrorDelete(error)
158+
//
159+
TfLiteObjectDetector* TfLiteObjectDetectorFromOptions(
160+
const TfLiteObjectDetectorOptions* options, TfLiteSupportError** error);
161+
162+
// Invokes the encapsulated TFLite model and performs object detection on the
163+
// frame_buffer. Returns a pointer to the created object detection result result
164+
// in case of success or NULL in case of failure. The caller must test the
165+
// return value to identify success or failure. If the caller doesn't want the
166+
// reason for failure, they can simply pass a NULL for the address of the error
167+
// pointer as shown below:
168+
//
169+
// TfLiteDetectionResult* detection_result =
170+
// TfLiteObjectDetectorDetect(&options, NULL);
171+
//
172+
// If the caller wants to be informed of the reason for failure, they must pass
173+
// the adress of a pointer of type TfLiteSupportError to the `error` param as
174+
// shown below:
175+
//
176+
// TfLiteSupport *error = NULL:
177+
// TfLiteObjectDetector* detector = TfLiteObjectDetectorFromOptions(options,
178+
// &error);
179+
//
180+
// In case of unsuccessful execution, Once the function returns, the error
181+
// pointer will point to a struct containing the error information. If error
182+
// info is passed back to the caller, it is the responsibility of the caller to
183+
// free the error struct by calling the following method defined in common.h:
184+
//
185+
// TfLiteSupportErrorDelete(error)
186+
//
187+
TfLiteDetectionResult* TfLiteObjectDetectorDetect(
188+
const TfLiteObjectDetector* detector, const TfLiteFrameBuffer* frame_buffer,
189+
TfLiteSupportError** error);
190+
191+
// Disposes off the object detector.
192+
void TfLiteObjectDetectorDelete(TfLiteObjectDetector* detector);
193+
194+
#ifdef __cplusplus
195+
} // extern "C"
196+
#endif // __cplusplus
197+
198+
#endif // TENSORFLOW_LITE_SUPPORT_C_TASK_VISION_IMAGE_CLASSIFIER_H_

0 commit comments

Comments
 (0)