Updated Image classifier result to return empty results if packet can't be validated.

This commit is contained in:
Prianka Liz Kariat 2023-05-23 20:54:42 +05:30
parent fda001b666
commit 3eb97ae1ff

View File

@ -29,13 +29,20 @@ using ::mediapipe::Packet;
+ (nullable MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket: + (nullable MPPImageClassifierResult *)imageClassifierResultWithClassificationsPacket:
(const Packet &)packet { (const Packet &)packet {
MPPClassificationResult *classificationResult; // Even if packet does not validate a the expected type, you can safely access the timestamp.
NSInteger timestampInMilliSeconds =
(NSInteger)(packet.Timestamp().Value() / kMicroSecondsPerMilliSecond);
if (!packet.ValidateAsType<ClassificationResultProto>().ok()) { if (!packet.ValidateAsType<ClassificationResultProto>().ok()) {
return nil; // MPPClassificationResult's timestamp is populated from timestamp `ClassificationResultProto`'s
// timestamp_ms(). It is 0 since the packet can't be validated as a `ClassificationResultProto`.
return [[MPPImageClassifierResult alloc]
initWithClassificationResult:[[MPPClassificationResult alloc] initWithClassifications:@[]
timestampInMilliseconds:0]
timestampInMilliseconds:timestampInMilliSeconds];
} }
classificationResult = [MPPClassificationResult MPPClassificationResult *classificationResult = [MPPClassificationResult
classificationResultWithProto:packet.Get<ClassificationResultProto>()]; classificationResultWithProto:packet.Get<ClassificationResultProto>()];
return [[MPPImageClassifierResult alloc] return [[MPPImageClassifierResult alloc]