Skip to content

Added Utils for iOS Task Library Backed by C++ APIs #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion tensorflow_lite_support/ios/task/audio/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ objc_library(
module_name = "TFLAudioClassifier",
deps = [
"//tensorflow_lite_support/c/task/audio:audio_classifier",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils",
"//tensorflow_lite_support/ios/task/audio/core/audio_tensor:TFLAudioTensor",
"//tensorflow_lite_support/ios/task/audio/core/audio_tensor/utils:TFLAudioTensorUtils",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptions",
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_lite_support/ios/task/audio/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ objc_library(
deps = [
":TFLFloatBuffer",
"//tensorflow_lite_support/ios:TFLCommon",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ objc_library(
module_name = "TFLAudioRecord",
sdk_frameworks = ["AVFoundation"],
deps = [
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
"//tensorflow_lite_support/ios/task/audio/core:TFLAudioFormat",
"//tensorflow_lite_support/ios/task/audio/core:TFLFloatBuffer",
"//tensorflow_lite_support/ios/task/audio/core:TFLRingBuffer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ objc_library(
sdk_frameworks = ["AVFoundation"],
deps = [
"//tensorflow_lite_support/ios:TFLCommon",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
"//tensorflow_lite_support/ios/task/audio/core:TFLFloatBuffer",
"//tensorflow_lite_support/ios/task/audio/core:TFLRingBuffer",
"//tensorflow_lite_support/ios/task/audio/core/audio_record:TFLAudioRecord",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
==============================================================================*/
#import "tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.h"
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h"
#import "tensorflow_lite_support/ios/task/audio/core/audio_tensor/utils/sources/TFLAudioTensor+Utils.h"
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h"
Expand Down Expand Up @@ -66,7 +66,7 @@ - (BOOL)populateRequiredBufferSizeWithError:(NSError **)error {
TfLiteAudioClassifierGetRequiredInputBufferSize(_audioClassifier, &requiredBufferSizeError);

// Populate iOS error if C Error is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:requiredBufferSizeError toError:error]) {
if (![TFLCommonCUtils checkCError:requiredBufferSizeError toError:error]) {
TfLiteSupportErrorDelete(requiredBufferSizeError);
}

Expand All @@ -78,7 +78,7 @@ - (BOOL)populateRequiredAudioFormatWithError:(NSError **)error {
_requiredCAudioFormat =
TfLiteAudioClassifierGetRequiredAudioFormat(_audioClassifier, &getAudioFormatError);

if (![TFLCommonUtils checkCError:getAudioFormatError toError:error]) {
if (![TFLCommonCUtils checkCError:getAudioFormatError toError:error]) {
TfLiteSupportErrorDelete(getAudioFormatError);
}

Expand Down Expand Up @@ -129,7 +129,7 @@ + (nullable instancetype)audioClassifierWithOptions:(TFLAudioClassifierOptions *
deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)];

// Populate iOS error if TfliteSupportError is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:cCreateClassifierError toError:error]) {
if (![TFLCommonCUtils checkCError:cCreateClassifierError toError:error]) {
TfLiteSupportErrorDelete(cCreateClassifierError);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ - (nullable TFLClassificationResult *)classifyWithAudioTensor:(TFLAudioTensor *)
TfLiteAudioClassifierClassify(_audioClassifier, &cAudioBuffer, &classifyError);

// Populate iOS error if C Error is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:classifyError toError:error]) {
if (![TFLCommonCUtils checkCError:classifyError toError:error]) {
TfLiteSupportErrorDelete(classifyError);
}

Expand Down
4 changes: 2 additions & 2 deletions tensorflow_lite_support/ios/task/processor/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ objc_library(
],
module_name = "TFLSegmentationResult",
deps = [
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
],
)

Expand All @@ -79,7 +79,7 @@ objc_library(
deps = [
"//tensorflow_lite_support/c/task/processor:classification_options",
"//tensorflow_lite_support/ios:TFLCommon",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
"//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions",
],
)
Expand Down
6 changes: 3 additions & 3 deletions tensorflow_lite_support/ios/task/vision/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ objc_library(
module_name = "TFLImageClassifier",
deps = [
"//tensorflow_lite_support/c/task/vision:image_classifier",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptions",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers",
"//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions",
Expand All @@ -39,7 +39,7 @@ objc_library(
module_name = "TFLObjectDetector",
deps = [
"//tensorflow_lite_support/c/task/vision:object_detector",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptions",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers",
"//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions",
Expand All @@ -63,7 +63,7 @@ objc_library(
module_name = "TFLImageSegmenter",
deps = [
"//tensorflow_lite_support/c/task/vision:image_segmenter",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptions",
"//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers",
"//tensorflow_lite_support/ios/task/processor:TFLSegmentationResult",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
==============================================================================*/
#import "tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.h"
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h"
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationResult+Helpers.h"
Expand Down Expand Up @@ -91,7 +91,7 @@ + (nullable instancetype)imageClassifierWithOptions:(TFLImageClassifierOptions *
deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)];

// Populate iOS error if TfliteSupportError is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:cCreateClassifierError toError:error]) {
if (![TFLCommonCUtils checkCError:cCreateClassifierError toError:error]) {
TfLiteSupportErrorDelete(cCreateClassifierError);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ - (nullable TFLClassificationResult *)classifyWithGMLImage:(GMLImage *)image
cFrameBuffer = NULL;

// Populate iOS error if C Error is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:classifyError toError:error]) {
if (![TFLCommonCUtils checkCError:classifyError toError:error]) {
TfLiteSupportErrorDelete(classifyError);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
==============================================================================*/
#import "tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.h"
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h"
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLSegmentationResult+Helpers.h"
#import "tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+Utils.h"
Expand Down Expand Up @@ -90,7 +90,7 @@ + (nullable instancetype)imageSegmenterWithOptions:(nonnull TFLImageSegmenterOpt
// Freeing memory of allocated string.
free(cOptions.display_names_locale);

if (![TFLCommonUtils checkCError:cCreateImageSegmenterError toError:error]) {
if (![TFLCommonCUtils checkCError:cCreateImageSegmenterError toError:error]) {
TfLiteSupportErrorDelete(cCreateImageSegmenterError);
}

Expand Down Expand Up @@ -128,7 +128,7 @@ - (nullable TFLSegmentationResult *)segmentWithGMLImage:(GMLImage *)image
cFrameBuffer = nil;

// Populate iOS error if C Error is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:cSegmentError toError:error]) {
if (![TFLCommonCUtils checkCError:cSegmentError toError:error]) {
TfLiteSupportErrorDelete(cSegmentError);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
==============================================================================*/
#import "tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.h"
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h"
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h"
#import "tensorflow_lite_support/ios/task/processor/sources/TFLDetectionResult+Helpers.h"
Expand Down Expand Up @@ -91,7 +91,7 @@ + (nullable instancetype)objectDetectorWithOptions:(TFLObjectDetectorOptions *)o
deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)];

// Populate iOS error if TfliteSupportError is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:cCreateObjectDetectorError toError:error]) {
if (![TFLCommonCUtils checkCError:cCreateObjectDetectorError toError:error]) {
TfLiteSupportErrorDelete(cCreateObjectDetectorError);
}

Expand Down Expand Up @@ -130,7 +130,7 @@ - (nullable TFLDetectionResult *)detectWithGMLImage:(GMLImage *)image
cFrameBuffer = nil;

// Populate iOS error if C Error is not null and afterwards delete it.
if (![TFLCommonUtils checkCError:cDetectError toError:error]) {
if (![TFLCommonCUtils checkCError:cDetectError toError:error]) {
TfLiteSupportErrorDelete(cDetectError);
}

Expand Down
2 changes: 1 addition & 1 deletion tensorflow_lite_support/ios/task/vision/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ objc_library(
sdk_frameworks = ["Accelerate"],
deps = [
"//tensorflow_lite_support/c/task/vision/core:frame_buffer",
"//tensorflow_lite_support/ios:TFLCommonUtils",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
"//tensorflow_lite_support/odml/ios/image:MLImage",
],
)
18 changes: 16 additions & 2 deletions tensorflow_lite_support/ios/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ package(
objc_library(
name = "TFLStringUtil",
srcs = [
"Sources/TFLStringUtil.mm",
"sources/TFLStringUtil.mm",
],
hdrs = [
"Sources/TFLStringUtil.h",
"sources/TFLStringUtil.h",
],
module_name = "TFLStringUtil",
)

objc_library(
name = "TFLCommonUtils",
srcs = [
"sources/TFLCommonUtils.m",
],
hdrs = [
"sources/TFLCommonUtils.h",
],
module_name = "TFLCommonUtils",
deps = [
"//tensorflow_lite_support/ios:TFLCommon",
],
)
19 changes: 19 additions & 0 deletions tensorflow_lite_support/ios/utils/c/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package(
default_visibility = ["//tensorflow_lite_support:internal"],
licenses = ["notice"], # Apache 2.0
)

objc_library(
name = "TFLCommonCUtils",
srcs = [
"sources/TFLCommonCUtils.m",
],
hdrs = [
"sources/TFLCommonCUtils.h",
],
module_name = "TFLCommonCUtils",
deps = [
"//tensorflow_lite_support/c:common",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
],
)
37 changes: 37 additions & 0 deletions tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#import <Foundation/Foundation.h>
#import "tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h"

#include "tensorflow_lite_support/c/common.h"

NS_ASSUME_NONNULL_BEGIN

/** Helper utility for the all tasks which encapsulates common functionality of the iOS task library
* backed by C APIs. */
@interface TFLCommonCUtils : TFLCommonUtils

/**
* Converts a C library error, TfLiteSupportError to an NSError.
*
* @param supportError C library error.
* @param error Pointer to the memory location where the created error should be saved. If `nil`,
* no error will be saved.
*/
+ (BOOL)checkCError:(TfLiteSupportError *)supportError toError:(NSError **)error;

@end

NS_ASSUME_NONNULL_END
29 changes: 29 additions & 0 deletions tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2021 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h"

@implementation TFLCommonCUtils

+ (BOOL)checkCError:(TfLiteSupportError *)supportError toError:(NSError **)error {
if (!supportError) {
return YES;
}
NSString *description = [NSString stringWithCString:supportError->message
encoding:NSUTF8StringEncoding];
[TFLCommonUtils createCustomError:error withCode:supportError->code description:description];
return NO;
}

@end
17 changes: 17 additions & 0 deletions tensorflow_lite_support/ios/utils/cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package(
default_visibility = ["//tensorflow_lite_support:internal"],
licenses = ["notice"], # Apache 2.0
)

objc_library(
name = "TFLCommonCppUtils",
hdrs = [
"sources/TFLCommonCppUtils.h",
],
module_name = "TFLCommonCppUtils",
deps = [
"//tensorflow_lite_support/cc:common",
"@com_google_absl//absl/status",
"//tensorflow_lite_support/ios/utils:TFLCommonUtils",
],
)
37 changes: 37 additions & 0 deletions tensorflow_lite_support/ios/utils/cpp/sources/TFLCommonCppUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#import <Foundation/Foundation.h>
#import "tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h"

#include "absl/status/status.h"

NS_ASSUME_NONNULL_BEGIN

/** Helper utility for the all tasks which encapsulates common functionality of the iOS task library
* backed by C++ APIs. */
@interface TFLCommonCppUtils : TFLCommonUtils

/**
* Converts an absl status to an NSError.
*
* @param status absl status.
* @param error Pointer to the memory location where the created error should be saved. If `nil`,
* no error will be saved.
*/
+ (BOOL)checkCppError:(const absl::Status &)status toError:(NSError **)error;

@end

NS_ASSUME_NONNULL_END
Loading