Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ - (nullable TFLSearchResult *)searchWithGMLImage:(GMLImage *)image error:(NSErro
return nil;
}

std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithError:error];
uint8_t *buffer = nil;
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithUnderlyingBuffer:&buffer
error:error];

if (!cppFrameBuffer) {
free(buffer);
return nil;
}

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

// Free the underlying buffer
free(buffer);

return [TFLSearchResult searchResultWithCppResult:cppSearchResultStatus error:error];
}

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

std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithError:error];
uint8_t *buffer = nil;
std::unique_ptr<FrameBufferCpp> cppFrameBuffer = [image cppFrameBufferWithUnderlyingBuffer:&buffer
error:error];

if (!cppFrameBuffer) {
free(buffer);
return nil;
}

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

// Free the underlying buffer
free(buffer);

return [TFLSearchResult searchResultWithCppResult:cppSearchResultStatus error:error];
}
@end
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ NS_ASSUME_NONNULL_BEGIN
* tflite::task::vision::FrameBuffer is used by the TFLite Task Vision C++
* library to hold the backing buffer of any image.
*
* @param buffer Pointer to the memory location where underlying pixel buffer
* of the image should be saved.
*
* @param error Pointer to the memory location where errors if any should be
* saved. If @c NULL, no error will be saved.
*
* @return The FrameBuffer created from the gmlImage which can be used with the
* TF Lite Task Vision C++ library. @c NULL in case of an error.
*/
- (std::unique_ptr<tflite::task::vision::FrameBuffer>)cppFrameBufferWithError:
(NSError *_Nullable *)error;
- (std::unique_ptr<tflite::task::vision::FrameBuffer>)
cppFrameBufferWithUnderlyingBuffer:(uint8_t **)buffer
error:(NSError *_Nullable *)error;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@

@implementation GMLImage (CppUtils)

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

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

StatusOr<std::unique_ptr<FrameBufferCpp>> frameBuffer =
CreateFromRawBuffer(buffer, {(int)bitmapSize.width, (int)bitmapSize.height},
CreateFromRawBuffer(*buffer, {(int)bitmapSize.width, (int)bitmapSize.height},
frame_buffer_format, FrameBufferCpp::Orientation::kTopLeft);

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