Added common utils and string helpers
This commit is contained in:
parent
8c013647c8
commit
e997a19289
41
mediapipe/tasks/ios/common/utils/BUILD
Normal file
41
mediapipe/tasks/ios/common/utils/BUILD
Normal file
|
@ -0,0 +1,41 @@
|
|||
# Copyright 2022 The MediaPipe 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.
|
||||
|
||||
package(default_visibility = ["//mediapipe/tasks:internal"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
objc_library(
|
||||
name = "MPPCommonUtils",
|
||||
srcs = ["sources/MPPCommonUtils.mm"],
|
||||
hdrs = ["sources/MPPCommonUtils.h"],
|
||||
deps = [
|
||||
"//mediapipe/tasks/cc:common",
|
||||
"//mediapipe/tasks/ios/common:MPPCommon",
|
||||
"@com_google_absl//absl/status",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@com_google_absl//absl/strings:cord",
|
||||
],
|
||||
)
|
||||
|
||||
objc_library(
|
||||
name = "NSStringHelpers",
|
||||
srcs = ["sources/NSString+Helpers.mm"],
|
||||
hdrs = ["sources/NSString+Helpers.h"],
|
||||
copts = [
|
||||
"-ObjC++",
|
||||
"-std=c++17",
|
||||
],
|
||||
)
|
||||
|
78
mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h
Normal file
78
mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2022 The MediaPipe 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>
|
||||
#include "mediapipe/tasks/cc/common.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/** Error domain of Mediapipe Task related errors. */
|
||||
extern NSString *const MPPTasksErrorDomain;
|
||||
|
||||
/** Helper utility for the all tasks which encapsulates common functionality. */
|
||||
@interface MPPCommonUtils : NSObject
|
||||
|
||||
/**
|
||||
* Creates and saves an NSError in the Mediapipe task library domain, with the given code and
|
||||
* description.
|
||||
*
|
||||
* @param code Error code.
|
||||
* @param description Error description.
|
||||
* @param error Pointer to the memory location where the created error should be saved. If `nil`,
|
||||
* no error will be saved.
|
||||
*/
|
||||
+ (void)createCustomError:(NSError **)error
|
||||
withCode:(NSUInteger)code
|
||||
description:(NSString *)description;
|
||||
|
||||
/**
|
||||
* Creates and saves an NSError with the given domain, code and description.
|
||||
*
|
||||
* @param error Pointer to the memory location where the created error should be saved. If `nil`,
|
||||
* no error will be saved.
|
||||
* @param domain Error domain.
|
||||
* @param code Error code.
|
||||
* @param description Error description.
|
||||
*/
|
||||
+ (void)createCustomError:(NSError **)error
|
||||
withDomain:(NSString *)domain
|
||||
code:(NSUInteger)code
|
||||
description:(NSString *)description;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Allocates a block of memory with the specified size and returns a pointer to it. If memory
|
||||
* cannot be allocated because of an invalid memSize, it saves an error. In other cases, it
|
||||
* terminates program execution.
|
||||
*
|
||||
* @param memSize size of memory to be allocated
|
||||
* @param error Pointer to the memory location where errors if any should be saved. If `nil`, no
|
||||
* error will be saved.
|
||||
*
|
||||
* @return Pointer to the allocated block of memory on successfull allocation. nil in case as
|
||||
* error is encountered because of invalid memSize. If failure is due to any other reason, method
|
||||
* terminates program execution.
|
||||
*/
|
||||
+ (void *)mallocWithSize:(size_t)memSize error:(NSError **)error;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
137
mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm
Normal file
137
mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.mm
Normal file
|
@ -0,0 +1,137 @@
|
|||
// Copyright 2022 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 "mediapipe/tasks/ios/common/utils/sources/MPPCommonUtils.h"
|
||||
|
||||
#import "mediapipe/tasks/ios/common/sources/MPPCommon.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "absl/status/status.h" // from @com_google_absl
|
||||
#include "absl/strings/cord.h" // from @com_google_absl
|
||||
|
||||
#include "mediapipe/tasks/cc/common.h"
|
||||
|
||||
/** Error domain of MediaPipe task library errors. */
|
||||
NSString *const MPPTasksErrorDomain = @"org.mediapipe.tasks";
|
||||
|
||||
@implementation MPPCommonUtils
|
||||
|
||||
+ (void)createCustomError:(NSError **)error
|
||||
withCode:(NSUInteger)code
|
||||
description:(NSString *)description {
|
||||
[MPPCommonUtils createCustomError:error
|
||||
withDomain:MPPTasksErrorDomain
|
||||
code:code
|
||||
description:description];
|
||||
}
|
||||
|
||||
+ (void)createCustomError:(NSError **)error
|
||||
withDomain:(NSString *)domain
|
||||
code:(NSUInteger)code
|
||||
description:(NSString *)description {
|
||||
if (error) {
|
||||
*error = [NSError errorWithDomain:domain
|
||||
code:code
|
||||
userInfo:@{NSLocalizedDescriptionKey : description}];
|
||||
}
|
||||
}
|
||||
|
||||
+ (void *)mallocWithSize:(size_t)memSize error:(NSError **)error {
|
||||
if (!memSize) {
|
||||
[MPPCommonUtils createCustomError:error
|
||||
withCode:MPPTasksErrorCodeInvalidArgumentError
|
||||
description:@"memSize cannot be zero."];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *allocedMemory = malloc(memSize);
|
||||
if (!allocedMemory) {
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
return allocedMemory;
|
||||
}
|
||||
|
||||
+ (BOOL)checkCppError:(const absl::Status &)status toError:(NSError *_Nullable *)error {
|
||||
if (status.ok()) {
|
||||
return YES;
|
||||
}
|
||||
// Payload of absl::Status created by the Media Pipe task library stores an appropriate value of
|
||||
// the enum MediaPipeTasksStatus. The integer value corresponding to the MediaPipeTasksStatus enum
|
||||
// stored in the payload is extracted here to later map to the appropriate error code to be
|
||||
// returned. In cases where the enum is not stored in (payload is NULL or the payload string
|
||||
// cannot be converted to an integer), we set the error code value to be 1
|
||||
// (MPPTasksErrorCodeError of MPPTasksErrorCode used in the iOS library to signify
|
||||
// any errors not falling into other categories.) Since payload is of type absl::Cord that can be
|
||||
// type cast into an absl::optional<std::string>, we use the std::stoi function to convert it into
|
||||
// an integer code if possible.
|
||||
NSUInteger genericErrorCode = MPPTasksErrorCodeError;
|
||||
NSUInteger errorCode;
|
||||
try {
|
||||
// Try converting payload to integer if payload is not empty. Otherwise convert a string
|
||||
// signifying generic error code MPPTasksErrorCodeError to integer.
|
||||
errorCode =
|
||||
(NSUInteger)std::stoi(static_cast<absl::optional<std::string>>(
|
||||
status.GetPayload(mediapipe::tasks::kMediaPipeTasksPayload))
|
||||
.value_or(std::to_string(genericErrorCode)));
|
||||
} catch (std::invalid_argument &e) {
|
||||
// If non empty payload string cannot be converted to an integer. Set error code to 1(kError).
|
||||
errorCode = MPPTasksErrorCodeError;
|
||||
}
|
||||
|
||||
// If errorCode is outside the range of enum values possible or is
|
||||
// MPPTasksErrorCodeError, we try to map the absl::Status::code() to assign
|
||||
// appropriate MPPTasksErrorCode in default cases. Note:
|
||||
// The mapping to absl::Status::code() is done to generate a more specific error code than
|
||||
// MPPTasksErrorCodeError in cases when the payload can't be mapped to
|
||||
// MPPTasksErrorCode. This can happen when absl::Status returned by TFLite library are in turn returned
|
||||
// without modification by Mediapipe cc library methods.
|
||||
if (errorCode > MPPTasksErrorCodeLast || errorCode <= MPPTasksErrorCodeFirst) {
|
||||
switch (status.code()) {
|
||||
case absl::StatusCode::kInternal:
|
||||
errorCode = MPPTasksErrorCodeError;
|
||||
break;
|
||||
case absl::StatusCode::kInvalidArgument:
|
||||
errorCode = MPPTasksErrorCodeInvalidArgumentError;
|
||||
break;
|
||||
case absl::StatusCode::kNotFound:
|
||||
errorCode = MPPTasksErrorCodeError;
|
||||
break;
|
||||
default:
|
||||
errorCode = MPPTasksErrorCodeError;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Creates the NSEror with the appropriate error
|
||||
// MPPTasksErrorCode and message. MPPTasksErrorCode has a one to one
|
||||
// mapping with MediaPipeTasksStatus starting from the value 1(MPPTasksErrorCodeError)
|
||||
// and hence will be correctly initialized if directly cast from the integer code derived from
|
||||
// MediaPipeTasksStatus stored in its payload. MPPTasksErrorCode omits kOk = 0 of
|
||||
// MediaPipeTasksStatusx.
|
||||
//
|
||||
// Stores a string including absl status code and message(if non empty) as the
|
||||
// error message See
|
||||
// https://github.com/abseil/abseil-cpp/blob/master/absl/status/status.h#L514
|
||||
// for explanation. absl::Status::message() can also be used but not always
|
||||
// guaranteed to be non empty.
|
||||
NSString *description = [NSString
|
||||
stringWithCString:status.ToString(absl::StatusToStringMode::kWithNoExtraData).c_str()
|
||||
encoding:NSUTF8StringEncoding];
|
||||
[MPPCommonUtils createCustomError:error withCode:errorCode description:description];
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
28
mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h
Normal file
28
mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2022 The MediaPipe 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>
|
||||
#include <string>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface NSString (Helpers)
|
||||
|
||||
@property(readonly) std::string cppString;
|
||||
|
||||
+ (NSString *)stringWithCppString:(std::string)text;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
27
mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.mm
Normal file
27
mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.mm
Normal file
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2022 The MediaPipe 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 "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h"
|
||||
|
||||
@implementation NSString (Helpers)
|
||||
|
||||
- (std::string)cppString {
|
||||
return std::string(self.UTF8String, [self lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
|
||||
}
|
||||
|
||||
+ (NSString *)stringWithCppString:(std::string)text {
|
||||
return [NSString stringWithCString:text.c_str() encoding:[NSString defaultCStringEncoding]];
|
||||
}
|
||||
|
||||
@end
|
Loading…
Reference in New Issue
Block a user