Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios Vision VNImageRequestHandler orientation issue

I am trying to detect faces via camera using VNImageRequestHandler (iOS Vision). When I point on the photo by the camera in landscape mode it detects faces but with opposite orientation mode.

  let detectFaceRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])

enter image description here

like image 783
Svitlana Avatar asked Jun 22 '17 01:06

Svitlana


3 Answers

Have you tried to play with the VNImageRequestHandler orientation property?

let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .right, options: [:])

I had to set it to .right while reading video input from back camera in portrait mode.

like image 67
amb Avatar answered Nov 13 '22 03:11

amb


Convert the image to CIImage and apply the orientation property like below and pass it to the imagerequest handler

let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
   let imageElement = ciImage.applyingOrientation(Int32(orientation.rawValue))

        // Show the image in the UI.
        originalImage.image = uiImage

also check https://github.com/gunapandianraj/iOS-Vision code for converting Vision rect to UIKit rect

like image 29
Guna pandian Avatar answered Nov 13 '22 03:11

Guna pandian


I always got flipped points (bounding boxes) vertically. I fixed this totally with helper method:

private static func translateVisionToNormalBoundingBox(bb: CGRect, imageFullRect: CGRect) -> CGRect
{
    let renormalized = VNImageRectForNormalizedRect(bb, Int(imageFullRect.width), Int(imageFullRect.height))
    // Vertically translate origin !!!
    // Vertically translate origin !!!
    // Vertically translate origin !!!
    return CGRect(
        origin: CGPoint(
            x: renormalized.origin.x,
            y: imageFullRect.maxY - renormalized.origin.y - renormalized.size.height
        ),
        size: renormalized.size
    )
}

For cgImage orientation I use this StackOverflow's extension:

extension UIImage {

var cgImagePropertyOrientation: CGImagePropertyOrientation {
    switch imageOrientation
    {
    case .down: return .down
    case .left: return .left
    case .right: return .right
    case .up: return .up
    case .downMirrored: return .downMirrored
    case .leftMirrored: return .leftMirrored
    case .rightMirrored: return .rightMirrored
    case .upMirrored: return .upMirrored
        
        // TWEAK FOR NEW CASES !!!
        // TWEAK FOR NEW CASES !!!
        // TWEAK FOR NEW CASES !!!
    @unknown default:
        return .down
    }
}

}

like image 1
sabiland Avatar answered Nov 13 '22 02:11

sabiland