title: Convert Vision boundingBox from VNFaceObservation to rect to draw on image
body: <p>I am trying to use <code>VNDetectFaceRectanglesRequest</code> from the new <code>Vision</code> API to detect faces on images. Then, I draw a red rectangle on each detected face. </p>
  
  <p>But I'm having an issue converting the <code>boundingBox</code> from <code>VNFaceObservation</code> into a <code>CGRect</code>. It seems that my only problem is a the <strong>y origin</strong>. </p>
  
  <hr>
  
  <p>Here's my code:</p>
  
  <pre><code>let request=VNDetectFaceRectanglesRequest{request, error in
      var final_image=UIImage(ciImage: image)
      if let results=request.results as? [VNFaceObservation]{
          for face_obs in results{
            UIGraphicsBeginImageContextWithOptions(final_image.size, false, 1.0)
            final_image.draw(in: CGRect(x: 0, y: 0, width: final_image.size.width, height: final_image.size.height))
  
            var rect=face_obs.boundingBox
  /*/*/*/ RESULT 2 is when I uncomment this line to "flip" the y  /*/*/*/
            //rect.origin.y=1-rect.origin.y
            let conv_rect=CGRect(x: rect.origin.x*final_image.size.width, y: rect.origin.y*final_image.size.height, width: rect.width*final_image.size.width, height: rect.height*final_image.size.height)
  
            let c=UIGraphicsGetCurrentContext()!
            c.setStrokeColor(UIColor.red.cgColor)
            c.setLineWidth(0.01*final_image.size.width)
            c.stroke(conv_rect)
  
            let result=UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
  
            final_image=result!
          }
      }
      DispatchQueue.main.async{
          self.image_view.image=final_image
      }
  }
  
  
  let handler=VNImageRequestHandler(ciImage: image)
  DispatchQueue.global(qos: .userInteractive).async{
      do{
          try handler.perform([request])
      }catch{
          print(error)
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Here are the results so far. </p>
  
  <p>Result 1 (without flipping the y)
  <a href="https://i.stack.imgur.com/TA7do.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TA7do.png" alt="Result 1"></a></p>
  
  <p>Result 2 (flipping the y)
  <a href="https://i.stack.imgur.com/4GsJh.png" rel="noreferrer"><img src="https://i.stack.imgur.com/4GsJh.png" alt="Result 2"></a></p>
  
  <hr>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>I found a solution on my own for the rect.</p>
  
  <pre><code>let rect=face_obs.boundingBox
  let x=rect.origin.x*final_image.size.width
  let w=rect.width*final_image.size.width
  let h=rect.height*final_image.size.height
  let y=final_image.size.height*(1-rect.origin.y)-h
  let conv_rect=CGRect(x: x, y: y, width: w, height: h)
  </code></pre>
  
  <p>However, I marked @wei-jay's answer as the good one since it's more classy.</p>