2

Swift 5, Xcode 11, iOS 13.0.

In the code below, we will get the face from the image 'test', recognize the left eye, and display 'num' at its coordinates. However, the coordinates of the nose are not displayed at the correct position. I'm in trouble because I don't know the solution. I would be grateful if you could tell me.

import Vision
import Foundation
import SwiftUI

struct ContentView: View {
    @ObservedObject var faceGet = test()
    @State var uiimage : UIImage? = nil
    var body: some View {
        VStack{
            if uiimage != nil {
                Image(uiImage: uiimage!).resizable().scaledToFit()
            }
            Divider()
            Button(action: {
                self.uiimage = self.faceGet.faceCheck()
            }){
                Text("Tap image to see result")
            }
        }
    }
}


class test :ObservableObject{
    private var originalImage = UIImage(named: "test3")
    func faceCheck() -> UIImage?{
        var drawnImage : UIImage? = originalImage

        let request = VNDetectFaceLandmarksRequest { (request, error) in
            for observation in request.results as! [VNFaceObservation] {
                if let landmark = observation.landmarks?.nose{
                    for i in 0...landmark.pointCount - 1 {
                        drawnImage = self.drawText(
                            image: drawnImage!,
                            point: landmark.pointsInImage(imageSize: self.originalImage!.size) [i] ,
                            num: i)
                    }
                }
            }
        }

        if let cgImage = self.originalImage?.cgImage {
            let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
            try? handler.perform([request])
        }

        return drawnImage
    }


    func drawText(image :UIImage , point:CGPoint , num:Int ) ->UIImage
    {

        let text = num.description
        var newImage : UIImage? = nil

        let fontSize = image.size.height / image.size.width * 10

        let font = UIFont.boldSystemFont(ofSize: fontSize)

        let textWidth = CGFloat(round(text.widthOfString(usingFont: font)))
        let textHeight = CGFloat(round(text.heightOfString(usingFont: font)))

        let imageRect = CGRect(origin: .zero, size: image.size)
        UIGraphicsBeginImageContext(image.size);

        image.draw(in: imageRect)

        let rePoint :CGPoint = CGPoint(x:imageRect.maxX - CGFloat(round(point.x)),
                                       y:imageRect.maxY - CGFloat(round(point.y)))

        let textRect  = CGRect(origin: rePoint, size: CGSize(width: textWidth  , height: textHeight ))
        let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        let textFontAttributes = [
            NSAttributedString.Key.font: font,
            NSAttributedString.Key.foregroundColor: UIColor.red,
            NSAttributedString.Key.paragraphStyle: textStyle
        ]
        text.draw(in: textRect, withAttributes: textFontAttributes)

        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext()

        return newImage!
    }

}
extension String {
    public func widthOfString(usingFont font: UIFont) -> CGFloat {
        let attributes = [NSAttributedString.Key.font: font]
        let size = self.size(withAttributes: attributes)
        return size.width
    }

    public func heightOfString(usingFont font: UIFont) -> CGFloat {
        let attributes = [NSAttributedString.Key.font: font]
        let size = self.size(withAttributes: attributes)
        return size.height
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
notane
  • 31
  • 3

0 Answers0