10

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Svitlana
  • 2,938
  • 1
  • 29
  • 38

3 Answers3

6

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.

amb
  • 4,798
  • 6
  • 41
  • 68
1

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

Guna pandian
  • 83
  • 1
  • 9
  • on trying to work with the above code for a I get `Use of unresolved identifier 'uiImage'`as soon as I attempt to declare the variable `orientation` in the first line. Is there a specific library that needs to be imported for this to work? – Veejay Mar 22 '18 at 20:35
  • sorry for the naming convention . uiImage is the local variable of type UIImage which will be assigned with image i picked from photo library or taking photo or downloaded image ... – Guna pandian Mar 24 '18 at 14:30
  • what is imageElement? and where is used? – Roberto Ferraz Jun 25 '18 at 13:29
1

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

}

sabiland
  • 2,526
  • 1
  • 25
  • 24