TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Hot Prospect example to Halloween Mask... running into a positioning problem

Forums > SwiftUI

Hi there,

I've hijacked the Hot Prospect example's code scanner library to use the MetaDataObject to recognize a face and draw a mask over it. I'm so close, I'm successfully having the bounds get passed back, but there position seems a bit wonky. The X and Y coordinates seems flipped? The rectangle doesn't quite make it all the way through the whole range? Testing on an 5th gen iPad.

https://github.com/carlynorama/Masquerade

Any thoughts on how to get the rectangles to line up better? Thanks all in advanced.

//
//  CameraFeedView.swift
//  Masquerade
//
//
import SwiftUI

struct CameraFeedView: View {
    @State var foundFace:CGRect?
    @State var geometryRect:CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
    //var testRect:CGRect = CGRect(x: 0.44112000039964916, y: 0.1979580322805941, width: 0.3337599992007017, height: 0.5941303606941507)

    var body: some View {
        GeometryReader(content: { geometry in
            ZStack {
                CameraFeed(codeTypes: [.face], completion: handleCameraReturn)
                if (foundFace != nil) {
                    Rectangle()
                        .stroke()
                        .foregroundColor(.orange)
                        .frame(width: 100, height: 100, alignment: .topLeading)
                        .position(x: geometry.size.width * foundFace!.origin.x, y: geometry.size.height * foundFace!.origin.y)

                    FoundObject(frameRect: geometryRect, boundsRect: foundFace!)
                        .stroke()
                        .foregroundColor(.blue)
                }
            }
            .onAppear(perform: {
                let frame = geometry.frame(in: .global)
                geometryRect = CGRect(origin: CGPoint(x: frame.minX, y: frame.minY), size: geometry.size)
            })
        })

    }

    func handleCameraReturn(result: Result<CGRect, CameraFeed.CameraError>) {
        switch result {
        case .success(let bounds):
            print(bounds)
            foundFace = bounds
            //TODO: Add a timer so if not updated in time foundFace = nil / opcity of rectangle?
        case .failure(let error):
            print("Scanning failed: \(error)")
            foundFace = nil
        }
    }

}

struct FoundObject: Shape {
    func reMapBoundries(frameRect:CGRect, boundsRect:CGRect) -> CGRect {
        //Y bounded to width? Really?
        let newY = (frameRect.width * boundsRect.origin.x) + (1.0-frameRect.origin.x)
        //X bounded to height? Really?
        let newX = (frameRect.height * boundsRect.origin.y) + (1.0-frameRect.origin.y)
        let newWidth = 100//(frameRect.width * boundsRect.width)
        let newHeight = 100//(frameRect.height * boundsRect.height)
        let newRect = CGRect(origin: CGPoint(x: newX, y: newY), size: CGSize(width: newWidth, height: newHeight))
        return newRect
    }

    let frameRect:CGRect
    let boundsRect:CGRect
    func path(in rect: CGRect) -> Path {
        var path = Path()
        path.addRect(reMapBoundries(frameRect: frameRect, boundsRect: boundsRect))
        return path
    }
}

struct CameraFeedView_Previews: PreviewProvider {
    static var previews: some View {
        CameraFeedView()
    }
}

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.