CJ KIM
CJ KIM

Reputation: 13

Output wrong eye position using CIfacefeature and SquareCam

I am trying to write code which put a sticker on eyes and the code is based on SquareCam.

It detects faces well, but when I tried to output my image on left eye, it always gives wrong position even though I used the same ways on finding face rect.

There are results on my phone.

Back Camera Result

Front Camera Result

And the code is here.

  for ff in features as! [CIFaceFeature] {
        // find the correct position for the square layer within the previewLayer
        // the feature box originates in the bottom left of the video frame.
        // (Bottom right if mirroring is turned on)
        var faceRect = ff.bounds

        let temp = faceRect.origin.x
        faceRect.origin.x = faceRect.origin.y
        faceRect.origin.y = temp
        // scale coordinates so they fit in the preview box, which may be scaled
        let widthScaleBy = previewBox.size.width / clap.size.height
        let heightScaleBy = previewBox.size.height / clap.size.width
        faceRect.size.width *= widthScaleBy
        faceRect.size.height *= heightScaleBy
        faceRect.origin.x *= widthScaleBy
        faceRect.origin.y *= heightScaleBy

        var eyeRect : CGRect
        eyeRect = CGRect()
        eyeRect.origin.x = ff.leftEyePosition.y
        eyeRect.origin.y = ff.leftEyePosition.x
        eyeRect.origin.x *= widthScaleBy
        eyeRect.origin.y *= heightScaleBy


        eyeRect.size.width = faceRect.size.width * 0.15
        eyeRect.size.height = eyeRect.size.width


        if isMirrored {
            faceRect = faceRect.offsetBy(dx: previewBox.origin.x + previewBox.size.width - faceRect.size.width - (faceRect.origin.x * 2), dy: previewBox.origin.y)
            eyeRect = eyeRect.offsetBy(dx:previewBox.origin.x + previewBox.size.width - eyeRect.size.width - (eyeRect.origin.x * 2),dy : previewBox.origin.y)

        } else {
            faceRect = faceRect.offsetBy(dx: previewBox.origin.x, dy: previewBox.origin.y)
            eyeRect = eyeRect.offsetBy(dx: previewBox.origin.x, dy:previewBox.origin.y)

        }

        print(eyeRect)
        print(faceRect)

        var featureLayer: CALayer? = nil
        var eyeLayer : CALayer? = nil
        // re-use an existing layer if possible
        while featureLayer == nil && (currentSublayer < sublayersCount) {
            let currentLayer = sublayers[currentSublayer];currentSublayer += 1
            if currentLayer.name == "FaceLayer" {
                featureLayer = currentLayer
                currentLayer.isHidden = false
                eyeLayer = featureLayer?.sublayers?[0]
                //eyeLayer?.isHidden = false
            }
        }

        // create a new one if necessary
        if featureLayer == nil {
            featureLayer = CALayer()
            featureLayer!.contents = square.cgImage
            featureLayer!.name = "FaceLayer"
            previewLayer?.addSublayer(featureLayer!)

            eyeLayer = CALayer()
            eyeLayer!.contents = eyes.cgImage
            eyeLayer!.name = "EyeLayer"
            featureLayer?.addSublayer(eyeLayer!)
        }


        featureLayer!.frame = faceRect
        eyeLayer!.frame = eyeRect

Upvotes: 1

Views: 452

Answers (1)

Cyberbeni
Cyberbeni

Reputation: 710

0,0 is at bottom left for the eyePositions, so you have to eyePosition.y = image.size.height - eyePosition.y to be in the same coordinate system as frames.

Upvotes: 0

Related Questions