Search code examples
iosswiftapple-vision

X and Y-axis swapped in Vision Framework Swift


I'm using Vision Framework to detecting faces with iPhone's front camera. My code looks like

  func detect(_ cmSampleBuffer: CMSampleBuffer) {
    guard let pixelBuffer = CMSampleBufferGetImageBuffer(cmSampleBuffer) else {return}
    var requests: [VNRequest] = []
    
    let requestLandmarks = VNDetectFaceLandmarksRequest { request, _ in
      DispatchQueue.main.async {
        guard let results = request.results as? [VNFaceObservation],
        print(results)
      }
    }
    requests.append(requestLandmarks)
            
    let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .leftMirrored)
    do {
      try handler.perform(requests)
    } catch {
      print(error)
    }
  }

However, I noticed that when I move my face horizontally, the coordinates change vertically and vice versa. The image bellow can help to understand

enter image description here

If anyone can help me i'm going crazy about it


Solution

  • For some reason, remove

    let connectionVideo = videoDataOutput.connection(with: AVMediaType.video)
    connectionVideo?.videoOrientation = AVCaptureVideoOrientation.portrait
    

    from my AVCaptureVideoDataOutput solved the problem 🤡