Skip to content

Commit f08a570

Browse files
Merge pull request #896 from khanhlvg:ios-image-searcher-bug-fix
PiperOrigin-RevId: 484363050
2 parents 1407175 + 3e5c2ea commit f08a570

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,20 @@ - (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image error:(NSErro
108108
return nil;
109109
}
110110

111-
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithError:error];
111+
uint8_t *buffer = nil;
112+
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithUnderlyingBuffer:&buffer
113+
error:error];
112114

113115
if (!cppFrameBuffer) {
116+
free(buffer);
114117
return nil;
115118
}
116119

117120
StatusOr<SearchResultCpp> cppSearchResultStatus = _cppImageSearcher->Search(*cppFrameBuffer);
118121

122+
// Free the underlying buffer
123+
free(buffer);
124+
119125
return [TFLSearchResult searchResultWithCppResult:cppSearchResultStatus error:error];
120126
}
121127

@@ -129,9 +135,12 @@ - (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image
129135
return nil;
130136
}
131137

132-
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithError:error];
138+
uint8_t *buffer = nil;
139+
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithUnderlyingBuffer:&buffer
140+
error:error];
133141

134142
if (!cppFrameBuffer) {
143+
free(buffer);
135144
return nil;
136145
}
137146

@@ -144,6 +153,9 @@ - (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image
144153
StatusOr<SearchResultCpp> cppSearchResultStatus =
145154
_cppImageSearcher->Search(*cppFrameBuffer, regionOfInterest);
146155

156+
// Free the underlying buffer
157+
free(buffer);
158+
147159
return [TFLSearchResult searchResultWithCppResult:cppSearchResultStatus error:error];
148160
}
149161
@end

tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ NS_ASSUME_NONNULL_BEGIN
3131
* tflite::task::vision::FrameBuffer is used by the TFLite Task Vision C++
3232
* library to hold the backing buffer of any image.
3333
*
34+
* @param buffer Pointer to the memory location where underlying pixel buffer
35+
* of the image should be saved.
36+
*
3437
* @param error Pointer to the memory location where errors if any should be
3538
* saved. If @c NULL, no error will be saved.
3639
*
3740
* @return The FrameBuffer created from the gmlImage which can be used with the
3841
* TF Lite Task Vision C++ library. @c NULL in case of an error.
3942
*/
40-
- (std::unique_ptr<tflite::task::vision::FrameBuffer>)cppFrameBufferWithError:
41-
(NSError *_Nullable *)error;
43+
- (std::unique_ptr<tflite::task::vision::FrameBuffer>)
44+
cppFrameBufferWithUnderlyingBuffer:(uint8_t **)buffer
45+
error:(NSError *_Nullable *)error;
4246

4347
@end
4448

tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+CppUtils.mm

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727
@implementation GMLImage (CppUtils)
2828

29-
- (std::unique_ptr<FrameBufferCpp>)cppFrameBufferWithError:(NSError **)error {
30-
uint8_t *buffer = [self bufferWithError:error];
29+
- (std::unique_ptr<tflite::task::vision::FrameBuffer>)
30+
cppFrameBufferWithUnderlyingBuffer:(uint8_t **)buffer
31+
error:(NSError *_Nullable *)error {
32+
*buffer = [self bufferWithError:error];
3133

3234
if (!buffer) {
3335
return NULL;
@@ -37,7 +39,7 @@ @implementation GMLImage (CppUtils)
3739
FrameBufferCpp::Format frame_buffer_format = FrameBufferCpp::Format::kRGB;
3840

3941
StatusOr<std::unique_ptr<FrameBufferCpp>> frameBuffer =
40-
CreateFromRawBuffer(buffer, {(int)bitmapSize.width, (int)bitmapSize.height},
42+
CreateFromRawBuffer(*buffer, {(int)bitmapSize.width, (int)bitmapSize.height},
4143
frame_buffer_format, FrameBufferCpp::Orientation::kTopLeft);
4244

4345
if (![TFLCommonCppUtils checkCppError:frameBuffer.status() toError:error]) {

0 commit comments

Comments
 (0)