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) NS_SWIFT_NAME(MPImage)
@interface MPPImage : NSObject @interface MPPImage : NSObject
/** Width of the image in pixels. */ /** Size of the image in pixels. */
@property(nonatomic, readonly) CGFloat width; @property(nonatomic, readonly) CGSize size;
/** Height of the image in pixels. */
@property(nonatomic, readonly) CGFloat height;
/** /**
* The display orientation of the image. If `imageSourceType` is `MPPImageSourceTypeImage`, the * The display orientation of the image. If `imageSourceType` is `MPPImageSourceTypeImage`, the

View File

@ -46,8 +46,7 @@ NS_ASSUME_NONNULL_BEGIN
_imageSourceType = MPPImageSourceTypeImage; _imageSourceType = MPPImageSourceTypeImage;
_orientation = orientation; _orientation = orientation;
_image = image; _image = image;
_width = image.size.width * image.scale; _size = CGSizeMake(image.size.width * image.scale, image.size.height * image.scale);
_height = image.size.height * image.scale;
} }
return self; return self;
} }
@ -72,8 +71,7 @@ NS_ASSUME_NONNULL_BEGIN
_orientation = orientation; _orientation = orientation;
CVPixelBufferRetain(pixelBuffer); CVPixelBufferRetain(pixelBuffer);
_pixelBuffer = pixelBuffer; _pixelBuffer = pixelBuffer;
_width = CVPixelBufferGetWidth(pixelBuffer); _size = CGSizeMake(CVPixelBufferGetWidth(pixelBuffer), CVPixelBufferGetHeight(pixelBuffer));
_height = CVPixelBufferGetHeight(pixelBuffer);
} }
return self; return self;
} }
@ -105,8 +103,7 @@ NS_ASSUME_NONNULL_BEGIN
_orientation = orientation; _orientation = orientation;
CFRetain(sampleBuffer); CFRetain(sampleBuffer);
_sampleBuffer = sampleBuffer; _sampleBuffer = sampleBuffer;
_width = CVPixelBufferGetWidth(imageBuffer); _size = CGSizeMake(CVPixelBufferGetWidth(imageBuffer), CVPixelBufferGetHeight(imageBuffer));
_height = CVPixelBufferGetHeight(imageBuffer);
} }
return self; 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 * @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`, the returned `NormalizedRect` covers the whole image. Make sure that `roi` equals
* `CGRectZero` if `ROIAllowed` is NO. Otherwise, an error will be returned. * `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 * @param imageOrientation A `UIImageOrientation` indicating the rotation to be applied to the
* image. The resulting `NormalizedRect` will convert the `imageOrientation` to degrees clockwise. * image. The resulting `NormalizedRect` will convert the `imageOrientation` to degrees clockwise.
* Mirrored orientations (`UIImageOrientationUpMirrored`, `UIImageOrientationDownMirrored`, * Mirrored orientations (`UIImageOrientationUpMirrored`, `UIImageOrientationDownMirrored`,
@ -83,6 +85,7 @@ NS_ASSUME_NONNULL_BEGIN
*/ */
- (std::optional<mediapipe::NormalizedRect>) - (std::optional<mediapipe::NormalizedRect>)
normalizedRectFromRegionOfInterest:(CGRect)roi normalizedRectFromRegionOfInterest:(CGRect)roi
imageSize:(CGSize)imageSize
imageOrientation:(UIImageOrientation)imageOrientation imageOrientation:(UIImageOrientation)imageOrientation
ROIAllowed:(BOOL)ROIAllowed ROIAllowed:(BOOL)ROIAllowed
error:(NSError **)error; error:(NSError **)error;

View File

@ -86,6 +86,7 @@ static const NSInteger kMPPOrientationDegreesLeft = -270;
} }
- (std::optional<NormalizedRect>)normalizedRectFromRegionOfInterest:(CGRect)roi - (std::optional<NormalizedRect>)normalizedRectFromRegionOfInterest:(CGRect)roi
imageSize:(CGSize)imageSize
imageOrientation: imageOrientation:
(UIImageOrientation)imageOrientation (UIImageOrientation)imageOrientation
ROIAllowed:(BOOL)ROIAllowed ROIAllowed:(BOOL)ROIAllowed
@ -102,8 +103,6 @@ static const NSInteger kMPPOrientationDegreesLeft = -270;
NormalizedRect normalizedRect; NormalizedRect normalizedRect;
normalizedRect.set_x_center(CGRectGetMidX(calculatedRoi)); normalizedRect.set_x_center(CGRectGetMidX(calculatedRoi));
normalizedRect.set_y_center(CGRectGetMidY(calculatedRoi)); normalizedRect.set_y_center(CGRectGetMidY(calculatedRoi));
normalizedRect.set_width(CGRectGetWidth(calculatedRoi));
normalizedRect.set_height(CGRectGetHeight(calculatedRoi));
int rotationDegrees = 0; int rotationDegrees = 0;
switch (imageOrientation) { switch (imageOrientation) {
@ -134,6 +133,17 @@ static const NSInteger kMPPOrientationDegreesLeft = -270;
normalizedRect.set_rotation(rotationDegrees * M_PI / kMPPOrientationDegreesDown); 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; return normalizedRect;
} }

View File

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