forked from google-ai-edge/mediapipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request google-ai-edge#5432 from priankakariatyml:ios-audi…
…o-classifier-utils PiperOrigin-RevId: 641291978
- Loading branch information
Showing
11 changed files
with
279 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2023 The MediaPipe Authors. | ||
# | ||
# 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 = "MPPAudioClassifierOptionsHelpers", | ||
srcs = ["sources/MPPAudioClassifierOptions+Helpers.mm"], | ||
hdrs = ["sources/MPPAudioClassifierOptions+Helpers.h"], | ||
copts = [ | ||
"-ObjC++", | ||
"-std=c++17", | ||
], | ||
deps = [ | ||
"//mediapipe/framework:calculator_options_cc_proto", | ||
"//mediapipe/tasks/cc/audio/audio_classifier/proto:audio_classifier_graph_options_cc_proto", | ||
"//mediapipe/tasks/cc/components/processors/proto:classifier_options_cc_proto", | ||
"//mediapipe/tasks/ios/audio/audio_classifier:MPPAudioClassifierOptions", | ||
"//mediapipe/tasks/ios/common/utils:NSStringHelpers", | ||
"//mediapipe/tasks/ios/core:MPPTaskOptionsProtocol", | ||
"//mediapipe/tasks/ios/core/utils:MPPBaseOptionsHelpers", | ||
], | ||
) | ||
|
||
objc_library( | ||
name = "MPPAudioClassifierResultHelpers", | ||
srcs = ["sources/MPPAudioClassifierResult+Helpers.mm"], | ||
hdrs = ["sources/MPPAudioClassifierResult+Helpers.h"], | ||
copts = [ | ||
"-ObjC++", | ||
"-std=c++17", | ||
], | ||
deps = [ | ||
"//mediapipe/framework:packet", | ||
"//mediapipe/tasks/cc/components/containers/proto:classifications_cc_proto", | ||
"//mediapipe/tasks/ios/audio/audio_classifier:MPPAudioClassifierResult", | ||
"//mediapipe/tasks/ios/components/containers/utils:MPPClassificationResultHelpers", | ||
], | ||
) |
32 changes: 32 additions & 0 deletions
32
mediapipe/tasks/ios/audio/audio_classifier/utils/sources/MPPAudioClassifierOptions+Helpers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 The MediaPipe Authors. | ||
// | ||
// 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. | ||
|
||
#include "mediapipe/framework/calculator_options.pb.h" | ||
#import "mediapipe/tasks/ios/audio/audio_classifier/sources/MPPAudioClassifierOptions.h" | ||
#import "mediapipe/tasks/ios/core/sources/MPPTaskOptionsProtocol.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface MPPAudioClassifierOptions (Helpers) <MPPTaskOptionsProtocol> | ||
|
||
/** | ||
* Populates the provided `CalculatorOptions` proto container with the current settings. | ||
* | ||
* @param optionsProto The `CalculatorOptions` proto object to copy the settings to. | ||
*/ | ||
- (void)copyToProto:(::mediapipe::CalculatorOptions *)optionsProto; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
58 changes: 58 additions & 0 deletions
58
...apipe/tasks/ios/audio/audio_classifier/utils/sources/MPPAudioClassifierOptions+Helpers.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2024 The MediaPipe Authors. | ||
// | ||
// 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/audio/audio_classifier/utils/sources/MPPAudioClassifierOptions+Helpers.h" | ||
|
||
#import "mediapipe/tasks/ios/common/utils/sources/NSString+Helpers.h" | ||
#import "mediapipe/tasks/ios/core/utils/sources/MPPBaseOptions+Helpers.h" | ||
|
||
#include "mediapipe/tasks/cc/audio/audio_classifier/proto/audio_classifier_graph_options.pb.h" | ||
#include "mediapipe/tasks/cc/components/processors/proto/classifier_options.pb.h" | ||
|
||
namespace { | ||
using CalculatorOptionsProto = mediapipe::CalculatorOptions; | ||
using AudioClassifierGraphOptionsProto = | ||
::mediapipe::tasks::audio::audio_classifier::proto::AudioClassifierGraphOptions; | ||
using ClassifierOptionsProto = ::mediapipe::tasks::components::processors::proto::ClassifierOptions; | ||
} // namespace | ||
|
||
@implementation MPPAudioClassifierOptions (Helpers) | ||
|
||
- (void)copyToProto:(CalculatorOptionsProto *)optionsProto { | ||
AudioClassifierGraphOptionsProto *graphOptions = | ||
optionsProto->MutableExtension(AudioClassifierGraphOptionsProto::ext); | ||
|
||
[self.baseOptions copyToProto:graphOptions->mutable_base_options() | ||
withUseStreamMode:self.runningMode != MPPAudioRunningModeAudioStream]; | ||
|
||
ClassifierOptionsProto *classifierOptionsProto = graphOptions->mutable_classifier_options(); | ||
classifierOptionsProto->Clear(); | ||
|
||
if (self.displayNamesLocale) { | ||
classifierOptionsProto->set_display_names_locale(self.displayNamesLocale.cppString); | ||
} | ||
|
||
classifierOptionsProto->set_max_results((int)self.maxResults); | ||
classifierOptionsProto->set_score_threshold(self.scoreThreshold); | ||
|
||
for (NSString *category in self.categoryAllowlist) { | ||
classifierOptionsProto->add_category_allowlist(category.cppString); | ||
} | ||
|
||
for (NSString *category in self.categoryDenylist) { | ||
classifierOptionsProto->add_category_denylist(category.cppString); | ||
} | ||
} | ||
|
||
@end |
36 changes: 36 additions & 0 deletions
36
mediapipe/tasks/ios/audio/audio_classifier/utils/sources/MPPAudioClassifierResult+Helpers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2024 The MediaPipe Authors. | ||
// | ||
// 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/audio/audio_classifier/sources/MPPAudioClassifierResult.h" | ||
|
||
#include "mediapipe/framework/packet.h" | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface MPPAudioClassifierResult (Helpers) | ||
|
||
/** | ||
* Creates an `MPPAudioClassifierResult` from a MediaPipe packet containing a | ||
* std::vector<ClassificationResult>. | ||
* | ||
* @param packet a MediaPipe packet wrapping a ClassificationResultProto. | ||
* | ||
* @return An `MPPAudioClassifierResult` object that contains a list of audio classifications. | ||
*/ | ||
+ (nullable MPPAudioClassifierResult *)audioClassifierResultWithClassificationsPacket: | ||
(const mediapipe::Packet &)packet; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
57 changes: 57 additions & 0 deletions
57
mediapipe/tasks/ios/audio/audio_classifier/utils/sources/MPPAudioClassifierResult+Helpers.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 The MediaPipe Authors. | ||
// | ||
// 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/audio/audio_classifier/utils/sources/MPPAudioClassifierResult+Helpers.h" | ||
#import "mediapipe/tasks/ios/components/containers/utils/sources/MPPClassificationResult+Helpers.h" | ||
|
||
#include "mediapipe/tasks/cc/components/containers/proto/classifications.pb.h" | ||
|
||
static const int kMicrosecondsPerMillisecond = 1000; | ||
|
||
namespace { | ||
using ClassificationResultProto = | ||
::mediapipe::tasks::components::containers::proto::ClassificationResult; | ||
using ::mediapipe::Packet; | ||
} // namespace | ||
|
||
@implementation MPPAudioClassifierResult (Helpers) | ||
|
||
+ (nullable MPPAudioClassifierResult *)audioClassifierResultWithClassificationsPacket: | ||
(const Packet &)packet { | ||
// Even if packet does not validate as the expected type, you can safely access the timestamp. | ||
NSInteger timestampInMilliseconds = | ||
(NSInteger)(packet.Timestamp().Value() / kMicrosecondsPerMillisecond); | ||
|
||
if (!packet.ValidateAsType<std::vector<ClassificationResultProto>>().ok()) { | ||
return [[MPPAudioClassifierResult alloc] initWithClassificationResults:@[] | ||
timestampInMilliseconds:timestampInMilliseconds]; | ||
} | ||
|
||
std::vector<ClassificationResultProto> cppClassificationResults = | ||
packet.Get<std::vector<ClassificationResultProto>>(); | ||
|
||
NSMutableArray<MPPClassificationResult *> *classificationResults = | ||
[NSMutableArray arrayWithCapacity:cppClassificationResults.size()]; | ||
|
||
for (const auto &cppClassificationResult : cppClassificationResults) { | ||
MPPClassificationResult *classificationResult = | ||
[MPPClassificationResult classificationResultWithProto:cppClassificationResult]; | ||
[classificationResults addObject:classificationResult]; | ||
} | ||
|
||
return [[MPPAudioClassifierResult alloc] initWithClassificationResults:classificationResults | ||
timestampInMilliseconds:timestampInMilliseconds]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters