Skip to content

Commit 949e3e4

Browse files
Merge pull request #891 from khanhlvg:ios-image-searcher-api-impl
PiperOrigin-RevId: 482325865
2 parents 5c1a046 + 82cb0db commit 949e3e4

File tree

7 files changed

+279
-41
lines changed

7 files changed

+279
-41
lines changed

tensorflow_lite_support/ios/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ objc_library(
153153
hdrs = [
154154
"sources/TFLCommonCppUtils.h",
155155
],
156+
copts = [
157+
"-ObjC++",
158+
"-std=c++17",
159+
],
156160
module_name = "TFLCommonCppUtils",
157161
deps = [
158162
":TFLCommon",

tensorflow_lite_support/ios/task/processor/sources/TFLSearchOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ - (instancetype)init {
2222
// maxResults will be 0 at the time of initialization. Setting it to 5 since max_results
2323
// defaults to 5 in search_options.proto.
2424
_maxResults = 5;
25+
_indexFile = [[TFLExternalFile alloc] init];
2526
}
2627
return self;
2728
}

tensorflow_lite_support/ios/task/vision/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ objc_library(
8989
module_name = "TFLImageSearcher",
9090
deps = [
9191
"//tensorflow_lite_support/cc/task/vision:image_searcher",
92+
"//tensorflow_lite_support/ios:TFLCommonCppUtils",
9293
"//tensorflow_lite_support/ios:TFLCommonUtils",
9394
"//tensorflow_lite_support/ios/task/core:TFLBaseOptionsCppHelpers",
9495
"//tensorflow_lite_support/ios/task/processor:TFLEmbeddingOptionsHelpers",

tensorflow_lite_support/ios/task/vision/sources/TFLImageSearcher.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,41 @@ NS_SWIFT_NAME(ImageSearcher)
9696
* camera and get the frames for inference, you must request for this format
9797
* from AVCaptureVideoDataOutput. Otherwise your inference results will be wrong.
9898
*
99-
* @param image An image on which embedding extraction is to be performed, followed by
100-
* nearest-neighbor search in the index, represented as a `GMLImage`.
101-
*
99+
* @param image An image on which to perform embedding extraction, followed by a nearest-neighbor
100+
* search in the index, represented as a `GMLImage`.
101+
102102
* @return A `TFLSearchResult`. `nil` if there is an error encountered during embedding extraction
103103
* and nearest neighbor search. Please see `TFLSearchResult` for more details.
104104
*/
105105
- (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image
106106
error:(NSError **)error NS_SWIFT_NAME(search(mlImage:));
107107

108+
/**
109+
* Performs embedding extraction on the given GMLImage, followed by nearest-neighbor search in the
110+
* index on the pixels within the specified region of interest of the given
111+
* `GMLImage`.
112+
*
113+
* @discussion This method currently supports inference on only following type of images:
114+
* 1. RGB and RGBA images for `GMLImageSourceTypeImage`.
115+
* 2. kCVPixelFormatType_32BGRA for `GMLImageSourceTypePixelBuffer` and
116+
* `GMLImageSourceTypeSampleBuffer`. If you are using `AVCaptureSession` to setup
117+
* camera and get the frames for inference, you must request for this format
118+
* from AVCaptureVideoDataOutput. Otherwise your classification
119+
* results will be wrong.
120+
*
121+
* @param image An image on which to perform embedding extraction, followed by a nearest-neighbor
122+
* search in the index, represented as a `GMLImage`.
123+
*
124+
* @param roi A CGRect specifying the region of interest within the given `GMLImage`.
125+
*
126+
* @return A TFLClassificationResult with one set of results per image classifier head. `nil` if
127+
* there is an error encountered during classification.
128+
*/
129+
- (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image
130+
regionOfInterest:(CGRect)roi
131+
error:(NSError **)error
132+
NS_SWIFT_NAME(search(mlImage:regionOfInterest:));
133+
108134
- (instancetype)init NS_UNAVAILABLE;
109135

110136
@end

tensorflow_lite_support/ios/task/vision/sources/TFLImageSearcher.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
==============================================================================*/
1515
#import "tensorflow_lite_support/ios/task/vision/sources/TFLImageSearcher.h"
1616
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
17-
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
17+
#import "tensorflow_lite_support/ios/sources/TFLCommonCppUtils.h"
1818
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+CppHelpers.h"
1919
#import "tensorflow_lite_support/ios/task/processor/sources/TFLEmbeddingOptions+Helpers.h"
2020
#import "tensorflow_lite_support/ios/task/processor/sources/TFLSearchOptions+Helpers.h"
@@ -71,16 +71,17 @@ - (ImageSearcherOptionsCpp)cppOptions {
7171

7272
@implementation TFLImageSearcher
7373

74-
- (nullable instancetype)initWithCppImageSearcherOptions:(ImageSearcherOptionsCpp)cppOptions {
74+
- (nullable instancetype)initWithCppImageSearcherOptions:(ImageSearcherOptionsCpp)cppOptions
75+
error:(NSError **)error {
7576
self = [super init];
7677
if (self) {
7778
StatusOr<std::unique_ptr<ImageSearcherCpp>> cppImageSearcher =
7879
ImageSearcherCpp::CreateFromOptions(cppOptions);
79-
if (cppImageSearcher.ok()) {
80-
_cppImageSearcher = std::move(cppImageSearcher.value());
81-
} else {
80+
if (![TFLCommonCppUtils checkCppError:cppImageSearcher.status() toError:error]) {
8281
return nil;
8382
}
83+
84+
_cppImageSearcher = std::move(cppImageSearcher.value());
8485
}
8586
return self;
8687
}
@@ -96,7 +97,7 @@ + (nullable instancetype)imageSearcherWithOptions:(TFLImageSearcherOptions *)opt
9697

9798
ImageSearcherOptionsCpp cppOptions = [options cppOptions];
9899

99-
return [[TFLImageSearcher alloc] initWithCppImageSearcherOptions:cppOptions];
100+
return [[TFLImageSearcher alloc] initWithCppImageSearcherOptions:cppOptions error:error];
100101
}
101102

102103
- (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image error:(NSError **)error {

tensorflow_lite_support/ios/test/task/vision/image_searcher/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ objc_library(
1313
srcs = ["TFLImageSearcherTests.m"],
1414
data = [
1515
"//tensorflow_lite_support/cc/test/testdata/task/vision:test_images",
16+
"//tensorflow_lite_support/cc/test/testdata/task/vision:test_indices",
1617
"//tensorflow_lite_support/cc/test/testdata/task/vision:test_models",
1718
],
1819
tags = TFL_DEFAULT_TAGS,
1920
deps = [
21+
"//tensorflow_lite_support/ios:TFLCommon",
2022
"//tensorflow_lite_support/ios/task/vision:TFLImageSearcher",
2123
"//tensorflow_lite_support/ios/task/vision/utils:GMLImageUtils",
2224
],

0 commit comments

Comments
 (0)