diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h index deffc97e2..db865b0e5 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPImage.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.h @@ -33,11 +33,8 @@ static const MPPImageSourceType MPPImageSourceTypeSampleBuffer = 2; NS_SWIFT_NAME(MPImage) @interface MPPImage : NSObject -/** Width of the image in pixels. */ -@property(nonatomic, readonly) CGFloat width; - -/** Height of the image in pixels. */ -@property(nonatomic, readonly) CGFloat height; +/** Size of the image in pixels. */ +@property(nonatomic, readonly) CGSize size; /** * The display orientation of the image. If `imageSourceType` is `MPPImageSourceTypeImage`, the diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPImage.m b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m index 1f5104ef7..c8bba9ace 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPImage.m +++ b/mediapipe/tasks/ios/vision/core/sources/MPPImage.m @@ -46,8 +46,7 @@ NS_ASSUME_NONNULL_BEGIN _imageSourceType = MPPImageSourceTypeImage; _orientation = orientation; _image = image; - _width = image.size.width * image.scale; - _height = image.size.height * image.scale; + _size = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale); } return self; } @@ -72,8 +71,7 @@ NS_ASSUME_NONNULL_BEGIN _orientation = orientation; CVPixelBufferRetain(pixelBuffer); _pixelBuffer = pixelBuffer; - _width = CVPixelBufferGetWidth(pixelBuffer); - _height = CVPixelBufferGetHeight(pixelBuffer); + _size = CGSizeMake(CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer)); } return self; } @@ -105,8 +103,7 @@ NS_ASSUME_NONNULL_BEGIN _orientation = orientation; CFRetain(sampleBuffer); _sampleBuffer = sampleBuffer; - _width = CVPixelBufferGetWidth(imageBuffer); - _height = CVPixelBufferGetHeight(imageBuffer); + _size = CGSizeMake(CVPixelBufferGetWidth(imageBuffer), CVPixelBufferGetHeight(imageBuffer)); } return self; } diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.h b/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.h index a7216840c..92b5563ef 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.h +++ b/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.h @@ -70,6 +70,8 @@ NS_ASSUME_NONNULL_BEGIN * @param roi A `CGRect` specifying the region of interest. If the input region of interest equals * `CGRectZero`, the returned `NormalizedRect` covers the whole image. Make sure that `roi` equals * `CGRectZero` if `ROIAllowed` is NO. Otherwise, an error will be returned. + * @param imageSize A `CGSize` specifying the size of the image within which normalized rect is + * calculated. * @param imageOrientation A `UIImageOrientation` indicating the rotation to be applied to the * image. The resulting `NormalizedRect` will convert the `imageOrientation` to degrees clockwise. * Mirrored orientations (`UIImageOrientationUpMirrored`, `UIImageOrientationDownMirrored`, @@ -83,6 +85,7 @@ NS_ASSUME_NONNULL_BEGIN */ - (std::optional) normalizedRectFromRegionOfInterest:(CGRect)roi + imageSize:(CGSize)imageSize imageOrientation:(UIImageOrientation)imageOrientation ROIAllowed:(BOOL)ROIAllowed error:(NSError **)error; diff --git a/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.mm b/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.mm index 964cd38c7..893470481 100644 --- a/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.mm +++ b/mediapipe/tasks/ios/vision/core/sources/MPPVisionTaskRunner.mm @@ -86,6 +86,7 @@ static const NSInteger kMPPOrientationDegreesLeft = -270; } - (std::optional)normalizedRectFromRegionOfInterest:(CGRect)roi + imageSize:(CGSize)imageSize imageOrientation: (UIImageOrientation)imageOrientation ROIAllowed:(BOOL)ROIAllowed @@ -102,8 +103,6 @@ static const NSInteger kMPPOrientationDegreesLeft = -270; NormalizedRect normalizedRect; normalizedRect.set_x_center(CGRectGetMidX(calculatedRoi)); normalizedRect.set_y_center(CGRectGetMidY(calculatedRoi)); - normalizedRect.set_width(CGRectGetWidth(calculatedRoi)); - normalizedRect.set_height(CGRectGetHeight(calculatedRoi)); int rotationDegrees = 0; switch (imageOrientation) { @@ -134,6 +133,17 @@ static const NSInteger kMPPOrientationDegreesLeft = -270; normalizedRect.set_rotation(rotationDegrees * M_PI / kMPPOrientationDegreesDown); + if (rotationDegrees % 180 == 0) { + normalizedRect.set_width(CGRectGetWidth(calculatedRoi)); + normalizedRect.set_height(CGRectGetHeight(calculatedRoi)); + } else { + const float width = CGRectGetHeight(calculatedRoi) * imageSize.height / imageSize.width; + const float height = CGRectGetWidth(calculatedRoi) * imageSize.width / imageSize.height; + + normalizedRect.set_width(width); + normalizedRect.set_height(height); + } + return normalizedRect; } diff --git a/mediapipe/tasks/ios/vision/object_detector/sources/MPPObjectDetector.mm b/mediapipe/tasks/ios/vision/object_detector/sources/MPPObjectDetector.mm index 499353558..16bcf5d7b 100644 --- a/mediapipe/tasks/ios/vision/object_detector/sources/MPPObjectDetector.mm +++ b/mediapipe/tasks/ios/vision/object_detector/sources/MPPObjectDetector.mm @@ -128,6 +128,7 @@ static NSString *const kTaskGraphName = @"mediapipe.tasks.vision.ObjectDetectorG error:(NSError **)error { std::optional rect = [_visionTaskRunner normalizedRectFromRegionOfInterest:CGRectZero + imageSize:image.size imageOrientation:image.orientation ROIAllowed:NO error:error]; @@ -152,6 +153,7 @@ static NSString *const kTaskGraphName = @"mediapipe.tasks.vision.ObjectDetectorG - (nullable MPPObjectDetectionResult *)detectInImage:(MPPImage *)image error:(NSError **)error { std::optional rect = [_visionTaskRunner normalizedRectFromRegionOfInterest:CGRectZero + imageSize:image.size imageOrientation:image.orientation ROIAllowed:YES error:error];