2828import org .tensorflow .lite .annotations .UsedByReflection ;
2929import org .tensorflow .lite .support .image .MlImageAdapter ;
3030import org .tensorflow .lite .support .image .TensorImage ;
31+ import org .tensorflow .lite .task .core .BaseOptions ;
3132import org .tensorflow .lite .task .core .TaskJniUtils ;
3233import org .tensorflow .lite .task .core .TaskJniUtils .EmptyHandleProvider ;
3334import org .tensorflow .lite .task .core .TaskJniUtils .FdAndOptionsHandleProvider ;
@@ -151,7 +152,12 @@ public long createHandle(
151152 long fileDescriptorOffset ,
152153 ObjectDetectorOptions options ) {
153154 return initJniWithModelFdAndOptions (
154- fileDescriptor , fileDescriptorLength , fileDescriptorOffset , options );
155+ fileDescriptor ,
156+ fileDescriptorLength ,
157+ fileDescriptorOffset ,
158+ options ,
159+ TaskJniUtils .createProtoBaseOptionsHandleWithLegacyNumThreads (
160+ options .getBaseOptions (), options .getNumThreads ()));
155161 }
156162 },
157163 OBJECT_DETECTOR_NATIVE_LIB ,
@@ -180,7 +186,9 @@ public long createHandle() {
180186 descriptor .getFd (),
181187 /*fileDescriptorLength=*/ OPTIONAL_FD_LENGTH ,
182188 /*fileDescriptorOffset=*/ OPTIONAL_FD_OFFSET ,
183- options );
189+ options ,
190+ TaskJniUtils .createProtoBaseOptionsHandleWithLegacyNumThreads (
191+ options .getBaseOptions (), options .getNumThreads ()));
184192 }
185193 },
186194 OBJECT_DETECTOR_NATIVE_LIB ));
@@ -209,7 +217,11 @@ public static ObjectDetector createFromBufferAndOptions(
209217 new EmptyHandleProvider () {
210218 @ Override
211219 public long createHandle () {
212- return initJniWithByteBuffer (modelBuffer , options );
220+ return initJniWithByteBuffer (
221+ modelBuffer ,
222+ options ,
223+ TaskJniUtils .createProtoBaseOptionsHandleWithLegacyNumThreads (
224+ options .getBaseOptions (), options .getNumThreads ()));
213225 }
214226 },
215227 OBJECT_DETECTOR_NATIVE_LIB ));
@@ -233,6 +245,7 @@ public static class ObjectDetectorOptions {
233245 // 1. java.util.Optional require Java 8 while we need to support Java 7.
234246 // 2. The Guava library (com.google.common.base.Optional) is avoided in this project. See the
235247 // comments for labelAllowList.
248+ private final BaseOptions baseOptions ;
236249 private final String displayNamesLocale ;
237250 private final int maxResults ;
238251 private final float scoreThreshold ;
@@ -252,6 +265,7 @@ public static Builder builder() {
252265
253266 /** A builder that helps to configure an instance of ObjectDetectorOptions. */
254267 public static class Builder {
268+ private BaseOptions baseOptions = BaseOptions .builder ().build ();
255269 private String displayNamesLocale = "en" ;
256270 private int maxResults = -1 ;
257271 private float scoreThreshold ;
@@ -262,6 +276,12 @@ public static class Builder {
262276
263277 private Builder () {}
264278
279+ /** Sets the general options to configure Task APIs, such as accelerators. */
280+ public Builder setBaseOptions (BaseOptions baseOptions ) {
281+ this .baseOptions = baseOptions ;
282+ return this ;
283+ }
284+
265285 /**
266286 * Sets the locale to use for display names specified through the TFLite Model Metadata, if
267287 * any.
@@ -335,7 +355,11 @@ public Builder setLabelDenyList(List<String> labelDenyList) {
335355 *
336356 * <p>numThreads should be greater than 0 or equal to -1. Setting numThreads to -1 has the
337357 * effect to let TFLite runtime set the value.
358+ *
359+ * @deprecated use {@link BaseOptions} to configure number of threads instead. This method
360+ * will override the number of threads configured from {@link BaseOptions}.
338361 */
362+ @ Deprecated
339363 public Builder setNumThreads (int numThreads ) {
340364 this .numThreads = numThreads ;
341365 return this ;
@@ -381,6 +405,10 @@ public int getNumThreads() {
381405 return numThreads ;
382406 }
383407
408+ public BaseOptions getBaseOptions () {
409+ return baseOptions ;
410+ }
411+
384412 private ObjectDetectorOptions (Builder builder ) {
385413 displayNamesLocale = builder .displayNamesLocale ;
386414 maxResults = builder .maxResults ;
@@ -389,6 +417,7 @@ private ObjectDetectorOptions(Builder builder) {
389417 labelAllowList = builder .labelAllowList ;
390418 labelDenyList = builder .labelDenyList ;
391419 numThreads = builder .numThreads ;
420+ baseOptions = builder .baseOptions ;
392421 }
393422 }
394423
@@ -463,8 +492,7 @@ public List<Detection> detect(MlImage image) {
463492 }
464493
465494 /**
466- * Performs actual detection on the provided {@code MlImage} with {@link
467- * ImageProcessingOptions}.
495+ * Performs actual detection on the provided {@code MlImage} with {@link ImageProcessingOptions}.
468496 *
469497 * <p>{@link ObjectDetector} supports the following options:
470498 *
@@ -497,10 +525,11 @@ private static native long initJniWithModelFdAndOptions(
497525 int fileDescriptor ,
498526 long fileDescriptorLength ,
499527 long fileDescriptorOffset ,
500- ObjectDetectorOptions options );
528+ ObjectDetectorOptions options ,
529+ long baseOptionsHandle );
501530
502531 private static native long initJniWithByteBuffer (
503- ByteBuffer modelBuffer , ObjectDetectorOptions options );
532+ ByteBuffer modelBuffer , ObjectDetectorOptions options , long baseOptionsHandle );
504533
505534 private static native List <Detection > detectNative (long nativeHandle , long frameBufferHandle );
506535
0 commit comments