Search code examples
swiftcgrect

Value of type 'CGRect' has no member 'scaled'


I'm trying to use detectLandmarks in swift4 but appear an error at let faceBoundingBox = boundingBox.scaled(to: self.view.bounds.size)

Anyone know how to fix it

func detectLandmarks(on image: CIImage) {
    try? faceLandmarksDetectionRequest.perform([faceLandmarks], on: image)
    if let landmarksResults = faceLandmarks.results as? [VNFaceObservation] {
        for observation in landmarksResults {
            DispatchQueue.main.async {
                if let boundingBox = self.faceLandmarks.inputFaceObservations?.first?.boundingBox {
                    let faceBoundingBox = boundingBox.scaled(to: self.view.bounds.size)

                    //different types of landmarks
                    let faceContour = observation.landmarks?.faceContour
                    self.convertPointsForFace(faceContour, faceBoundingBox)    
                }
            }
        }
    }
}

Solution

  • Try adding in this extension into your code and things should build nicely:

    //
    //  CGRectExtension.swift
    //  Vision Face Detection
    //
    //  Created by Pawel Chmiel on 23.06.2017.
    //  Copyright © 2017 Droids On Roids. All rights reserved.
    //
    import Foundation
    import UIKit
    
    extension CGRect {
        func scaled(to size: CGSize) -> CGRect {
            return CGRect(
                x: self.origin.x * size.width,
                y: self.origin.y * size.height,
                width: self.size.width * size.width,
                height: self.size.height * size.height
            )
        }
    }