Skip to content

Commit c1d709a

Browse files
lu-wang-gtflite-support-robot
authored andcommitted
Throw RuntimeExceptions instead of AssertionError in ObjectDetector
PiperOrigin-RevId: 400353926
1 parent 0bd82be commit c1d709a

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/vision/detector/ObjectDetector.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ public final class ObjectDetector extends BaseVisionTaskApi {
9696
*
9797
* @param modelPath path to the detection model with metadata in the assets
9898
* @throws IOException if an I/O error occurs when loading the tflite model
99-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
100-
* code
99+
* @throws IllegalArgumentException if an argument is invalid
100+
* @throws IllegalStateException if there is an internal error
101+
* @throws RuntimeException if there is an otherwise unspecified error
101102
*/
102103
public static ObjectDetector createFromFile(Context context, String modelPath)
103104
throws IOException {
@@ -109,8 +110,9 @@ public static ObjectDetector createFromFile(Context context, String modelPath)
109110
*
110111
* @param modelFile the detection model {@link File} instance
111112
* @throws IOException if an I/O error occurs when loading the tflite model
112-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
113-
* code
113+
* @throws IllegalArgumentException if an argument is invalid
114+
* @throws IllegalStateException if there is an internal error
115+
* @throws RuntimeException if there is an otherwise unspecified error
114116
*/
115117
public static ObjectDetector createFromFile(File modelFile) throws IOException {
116118
return createFromFileAndOptions(modelFile, ObjectDetectorOptions.builder().build());
@@ -122,10 +124,9 @@ public static ObjectDetector createFromFile(File modelFile) throws IOException {
122124
*
123125
* @param modelBuffer a direct {@link ByteBuffer} or a {@link MappedByteBuffer} of the detection
124126
* model
125-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
126-
* code
127127
* @throws IllegalArgumentException if the model buffer is not a direct {@link ByteBuffer} or a
128-
* {@link MappedByteBuffer}
128+
* {@link MappedByteBuffer} * @throws IllegalStateException if there is an internal error
129+
* @throws RuntimeException if there is an otherwise unspecified error
129130
*/
130131
public static ObjectDetector createFromBuffer(final ByteBuffer modelBuffer) {
131132
return createFromBufferAndOptions(modelBuffer, ObjectDetectorOptions.builder().build());
@@ -136,8 +137,9 @@ public static ObjectDetector createFromBuffer(final ByteBuffer modelBuffer) {
136137
*
137138
* @param modelPath path to the detection model with metadata in the assets
138139
* @throws IOException if an I/O error occurs when loading the tflite model
139-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
140-
* code
140+
* @throws IllegalArgumentException if an argument is invalid
141+
* @throws IllegalStateException if there is an internal error
142+
* @throws RuntimeException if there is an otherwise unspecified error
141143
*/
142144
public static ObjectDetector createFromFileAndOptions(
143145
Context context, String modelPath, ObjectDetectorOptions options) throws IOException {
@@ -170,8 +172,9 @@ public long createHandle(
170172
*
171173
* @param modelFile the detection model {@link File} instance
172174
* @throws IOException if an I/O error occurs when loading the tflite model
173-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
174-
* code
175+
* @throws IllegalArgumentException if an argument is invalid
176+
* @throws IllegalStateException if there is an internal error
177+
* @throws RuntimeException if there is an otherwise unspecified error
175178
*/
176179
public static ObjectDetector createFromFileAndOptions(
177180
File modelFile, final ObjectDetectorOptions options) throws IOException {
@@ -201,10 +204,10 @@ public long createHandle() {
201204
*
202205
* @param modelBuffer a direct {@link ByteBuffer} or a {@link MappedByteBuffer} of the detection
203206
* model
204-
* @throws AssertionError if error occurs when creating {@link ObjectDetector} from the native
205-
* code
206207
* @throws IllegalArgumentException if the model buffer is not a direct {@link ByteBuffer} or a
207208
* {@link MappedByteBuffer}
209+
* @throws IllegalStateException if there is an internal error
210+
* @throws RuntimeException if there is an otherwise unspecified error
208211
*/
209212
public static ObjectDetector createFromBufferAndOptions(
210213
final ByteBuffer modelBuffer, final ObjectDetectorOptions options) {
@@ -328,8 +331,8 @@ public Builder setScoreThreshold(float scoreThreshold) {
328331
*
329332
* <p>If non-empty, detection results whose label is not in this set will be filtered out.
330333
* Duplicate or unknown labels are ignored. Mutually exclusive with {@code labelDenyList}. It
331-
* will cause {@link AssertionError} when calling {@link #createFromFileAndOptions}, if both
332-
* {@code labelDenyList} and {@code labelAllowList} are set.
334+
* will cause {@link IllegalStateException} when calling {@link #createFromFileAndOptions}, if
335+
* both {@code labelDenyList} and {@code labelAllowList} are set.
333336
*/
334337
public Builder setLabelAllowList(List<String> labelAllowList) {
335338
this.labelAllowList = Collections.unmodifiableList(new ArrayList<>(labelAllowList));
@@ -341,8 +344,8 @@ public Builder setLabelAllowList(List<String> labelAllowList) {
341344
*
342345
* <p>If non-empty, detection results whose label is in this set will be filtered out.
343346
* Duplicate or unknown labels are ignored. Mutually exclusive with {@code labelAllowList}. It
344-
* will cause {@link AssertionError} when calling {@link #createFromFileAndOptions}, if both
345-
* {@code labelDenyList} and {@code labelAllowList} are set.
347+
* will cause {@link IllegalStateException} when calling {@link #createFromFileAndOptions}, if
348+
* both {@code labelDenyList} and {@code labelAllowList} are set.
346349
*/
347350
public Builder setLabelDenyList(List<String> labelDenyList) {
348351
this.labelDenyList = Collections.unmodifiableList(new ArrayList<>(labelDenyList));
@@ -435,7 +438,8 @@ private ObjectDetectorOptions(Builder builder) {
435438
* </ul>
436439
*
437440
* @param image a UINT8 {@link TensorImage} object that represents an RGB or YUV image
438-
* @throws AssertionError if error occurs when processing the image from the native code
441+
* @throws IllegalStateException if there is an internal error
442+
* @throws RuntimeException if there is an otherwise unspecified error
439443
* @throws IllegalArgumentException if the color space type of image is unsupported
440444
*/
441445
public List<Detection> detect(TensorImage image) {
@@ -464,7 +468,8 @@ public List<Detection> detect(TensorImage image) {
464468
*
465469
* @param image a UINT8 {@link TensorImage} object that represents an RGB or YUV image
466470
* @param options the options to configure how to preprocess the image
467-
* @throws AssertionError if error occurs when processing the image from the native code
471+
* @throws IllegalStateException if there is an internal error
472+
* @throws RuntimeException if there is an otherwise unspecified error
468473
* @throws IllegalArgumentException if the color space type of image is unsupported
469474
*/
470475
public List<Detection> detect(TensorImage image, ImageProcessingOptions options) {
@@ -484,7 +489,8 @@ public List<Detection> run(
484489
* Performs actual detection on the provided {@code MlImage}.
485490
*
486491
* @param image an {@code MlImage} object that represents an image
487-
* @throws AssertionError if error occurs when processing the image from the native code
492+
* @throws IllegalStateException if there is an internal error
493+
* @throws RuntimeException if there is an otherwise unspecified error
488494
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
489495
*/
490496
public List<Detection> detect(MlImage image) {
@@ -504,7 +510,8 @@ public List<Detection> detect(MlImage image) {
504510
*
505511
* @param image an {@code MlImage} object that represents an image
506512
* @param options the options to configure how to preprocess the image
507-
* @throws AssertionError if error occurs when classifying the image from the native code
513+
* @throws IllegalStateException if there is an internal error
514+
* @throws RuntimeException if there is an otherwise unspecified error
508515
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
509516
*/
510517
public List<Detection> detect(MlImage image, ImageProcessingOptions options) {

tensorflow_lite_support/java/src/native/task/vision/detector/object_detector_jni.cc

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ limitations under the License.
3333
namespace {
3434

3535
using ::tflite::support::StatusOr;
36-
using ::tflite::support::utils::kAssertionError;
36+
using ::tflite::support::utils::GetExceptionClassNameForStatusCode;
3737
using ::tflite::support::utils::kInvalidPointer;
3838
using ::tflite::support::utils::StringListToVector;
3939
using ::tflite::support::utils::ThrowException;
@@ -42,7 +42,6 @@ using ::tflite::task::vision::BoundingBox;
4242
using ::tflite::task::vision::ConvertToCategory;
4343
using ::tflite::task::vision::DetectionResult;
4444
using ::tflite::task::vision::FrameBuffer;
45-
4645
using ::tflite::task::vision::ObjectDetector;
4746
using ::tflite::task::vision::ObjectDetectorOptions;
4847

@@ -162,13 +161,17 @@ jlong CreateObjectDetectorFromOptions(JNIEnv* env,
162161
if (object_detector_or.ok()) {
163162
return reinterpret_cast<jlong>(object_detector_or->release());
164163
} else {
165-
ThrowException(env, kAssertionError,
166-
"Error occurred when initializing ObjectDetector: %s",
167-
object_detector_or.status().message().data());
164+
ThrowException(
165+
env,
166+
GetExceptionClassNameForStatusCode(object_detector_or.status().code()),
167+
"Error occurred when initializing ObjectDetector: %s",
168+
object_detector_or.status().message().data());
168169
return kInvalidPointer;
169170
}
170171
}
171172

173+
} // namespace
174+
172175
extern "C" JNIEXPORT void JNICALL
173176
Java_org_tensorflow_lite_task_vision_detector_ObjectDetector_deinitJni(
174177
JNIEnv* env, jobject thiz, jlong native_handle) {
@@ -221,10 +224,10 @@ Java_org_tensorflow_lite_task_vision_detector_ObjectDetector_detectNative(
221224
if (results_or.ok()) {
222225
return ConvertToDetectionResults(env, results_or.value());
223226
} else {
224-
ThrowException(env, kAssertionError,
225-
"Error occurred when detecting the image: %s",
226-
results_or.status().message().data());
227+
ThrowException(
228+
env, GetExceptionClassNameForStatusCode(results_or.status().code()),
229+
"Error occurred when detecting the image: %s",
230+
results_or.status().message().data());
227231
return nullptr;
228232
}
229233
}
230-
} // namespace

0 commit comments

Comments
 (0)