人脸识别 iPhone X Swift

Face Recognition iPhone X Swift

我用 ARFaceAnchor 做了一些实验来识别一些情绪,比如眨眼等等。可以肯定的是,我正确设置了 FaceAnchor,因为在调试器上我能够看到坐标,但它似乎无法识别我设置的任何情绪...

您会在附件中找到 ViewController,在单独的 Class 中您会找到 Emotions。

有什么想法吗?谢谢!

//  ViewController.swift
//

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSessionDelegate {

    @IBOutlet var sceneView: ARSCNView!

    let session = ARSession()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.sceneView.scene = SCNScene()
        self.sceneView.rendersContinuously = true

        // Configure our ARKit tracking session for facial recognition
        let config = ARFaceTrackingConfiguration()
        config.worldAlignment = .gravity
        session.delegate = self
        session.run(config, options: [])
    }

    // AR Session

    var currentFaceAnchor: ARFaceAnchor?
    var currentFrame: ARFrame?

    func session(_ session: ARSession, didUpdate frame: ARFrame) {
        self.currentFrame = frame
        DispatchQueue.main.async {

        }
    }

    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {

    }

    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
        guard let faceAnchor = anchors.first as? ARFaceAnchor else { return }
        self.currentFaceAnchor = faceAnchor
        print("Face",faceAnchor)

    }

    func session(_ session: ARSession, didRemove anchors: [ARAnchor]) {

    }

    var expressionsToUse: [Expression] = [SmileExpression(), EyebrowsRaisedExpression(), EyeBlinkLeftExpression(), EyeBlinkRightExpression(), JawOpenExpression(), LookLeftExpression(), LookRightExpression()] //All the expressions
    var currentExpression: Expression? = nil {
        didSet {
            if currentExpression != nil {
                self.currentExpressionShownAt = Date()
            } else {
                self.currentExpressionShownAt = nil
            }
        }
    }
    var currentExpressionShownAt: Date? = nil



}

没有检测到表达式的原因是除了将它们添加到 expressionsToUse 数组之外,您实际上没有对它们做任何事情。

每个 Expression 都有三个您目前没有使用的函数:

 func name() -> String {}
 func isExpressing(from: ARFaceAnchor) -> Bool {}
 func isDoingWrongExpression(from: ARFaceAnchor) -> Bool {}

由于您想要检测情绪,因此您需要将这些函数挂接到以下委托方法中:

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) { }

因此,像这样的内容会为您指明正确的方向:

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

    //1. Check To See We Have A Valid ARFaceAnchor
    guard let faceAnchor = anchors.first as? ARFaceAnchor else { return }

    self.currentFaceAnchor = faceAnchor

    //2. Loop Through Each Of The Expression & Determine Which One Is Being Used
    expressionsToUse.forEach { (possibleExpression) in

        //a. If The The User Is Doing A Particular Expression Then Assign It To The currentExpression Variable
        if possibleExpression.isExpressing(from: faceAnchor){

            currentExpression = possibleExpression

            print("""
                  Current Detected Expression = \(possibleExpression.name())
                  It Was Detected On \(currentExpressionShownAt!)
                  """)

        }else if possibleExpression.isDoingWrongExpression(from: faceAnchor){

           print("Incorrect Detected Expression = \(possibleExpression.name())")
        }
    }

}

希望对您有所帮助...