Added validation of C++ image classification result packet in MPPImageClassifierResult+Helpers.mm

This commit is contained in:
Prianka Liz Kariat 2023-05-04 16:52:58 +05:30
parent ab4b07646c
commit e47bb16544
2 changed files with 12 additions and 5 deletions

View File

@ -18,6 +18,8 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
static const int kMicroSecondsPerMilliSecond = 1000;
@interface MPPImageClassifierResult (Helpers) @interface MPPImageClassifierResult (Helpers)
/** /**
@ -28,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
* *
* @return An `MPPImageClassifierResult` object that contains a list of image classifications. * @return An `MPPImageClassifierResult` object that contains a list of image classifications.
*/ */
+ (MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket: + (nullable MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket:
(const mediapipe::Packet &)packet; (const mediapipe::Packet &)packet;
@end @end

View File

@ -17,8 +17,6 @@
#include "mediapipe/tasks/cc/components/containers/proto/classifications.pb.h" #include "mediapipe/tasks/cc/components/containers/proto/classifications.pb.h"
static const int kMicroSecondsPerMilliSecond = 1000;
namespace { namespace {
using ClassificationResultProto = using ClassificationResultProto =
::mediapipe::tasks::components::containers::proto::ClassificationResult; ::mediapipe::tasks::components::containers::proto::ClassificationResult;
@ -27,9 +25,16 @@ using ::mediapipe::Packet;
@implementation MPPImageClassifierResult (Helpers) @implementation MPPImageClassifierResult (Helpers)
+ (MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket: + (nullable MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket:
(const Packet &)packet { (const Packet &)packet {
MPPClassificationResult *classificationResult = [MPPClassificationResult MPPClassificationResult *classificationResult;
MPPImageClassifierResult *imageClassifierResult;
if (!packet.ValidateAsType<ClassificationResultProto>().ok()) {
return nil;
}
classificationResult = [MPPClassificationResult
classificationResultWithProto:packet.Get<ClassificationResultProto>()]; classificationResultWithProto:packet.Get<ClassificationResultProto>()];
return [[MPPImageClassifierResult alloc] return [[MPPImageClassifierResult alloc]