Updated normalized rect calculation for some angles in MPPVisionTaskRunner

This commit is contained in:
Prianka Liz Kariat 2023-04-11 18:11:33 +05:30
parent 089361cd89
commit 27353310c3
5 changed files with 22 additions and 13 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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<mediapipe::NormalizedRect>)
normalizedRectFromRegionOfInterest:(CGRect)roi
imageSize:(CGSize)imageSize
imageOrientation:(UIImageOrientation)imageOrientation
ROIAllowed:(BOOL)ROIAllowed
error:(NSError **)error;

View File

@ -86,6 +86,7 @@ static const NSInteger kMPPOrientationDegreesLeft = -270;
}
- (std::optional<NormalizedRect>)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;
}

View File

@ -128,6 +128,7 @@ static NSString *const kTaskGraphName = @"mediapipe.tasks.vision.ObjectDetectorG
error:(NSError **)error {
std::optional<NormalizedRect> 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<NormalizedRect> rect =
[_visionTaskRunner normalizedRectFromRegionOfInterest:CGRectZero
imageSize:image.size
imageOrientation:image.orientation
ROIAllowed:YES
error:error];