如何在实时帧捕获中使用GoogleMobileVision?

缺口

我正在尝试实时检测微笑概率。使用GoogleMobileVision,但我在GMVDetector中传递的选项参数导致应用程序崩溃

由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[_ SwiftValue intValue]

另外,当我将选项传递为nil时,它会出现内存问题。

我的代码:

import UIKit
import GoogleMobileVision

class ViewController: UIViewController, FrameExtractorDelegate {

@IBOutlet weak var lblSmiling: UILabel!
var frameExtractor: FrameExtractor!
var faceDetector = GMVDetector()

@IBOutlet weak var imageView: UIImageView!

@IBAction func flipButton(_ sender: UIButton) {
    frameExtractor.flipCamera()
}

override func viewDidLoad() {
    super.viewDidLoad()
    frameExtractor = FrameExtractor()
    frameExtractor.delegate = self
    let options: NSDictionary = [GMVDetectorFaceLandmarkType: GMVDetectorFaceLandmark.all, GMVDetectorFaceClassificationType: GMVDetectorFaceClassification.all, GMVDetectorFaceTrackingEnabled: true]

    self.faceDetector = GMVDetector(ofType: GMVDetectorTypeFace, options: options as! [AnyHashable : Any])
}

// Getting individual frame image here
func captured(image: UIImage) {
    processImage(image: image)
    imageView.image = image
}

func processImage(image: UIImage) {

    let faces : [GMVFaceFeature] = faceDetector.features(in: image, options: nil) as! [GMVFaceFeature]

    for face in faces {

        if face.hasSmilingProbability && face.smilingProbability > 0.4 {
            lblSmiling.text = String(describing: face.smilingProbability)
        }
    }
}

}

缺口

经过大量搜索并使用标志后,我自己解决了它,
这是我的工作代码:

import UIKit
import GoogleMobileVision

class ViewController: UIViewController, FrameExtractorDelegate {

@IBOutlet weak var lblSmiling: UILabel!
@IBOutlet weak var imageView: UIImageView!

var newView = UIView()
private let ssQ = DispatchQueue(label: "process queue")
var frameExtractor: FrameExtractor!
var faceDetector: GMVDetector?
var faces = [GMVFaceFeature]()
var imgIsProcessing = false
var sessionCountToClr = 0

override func viewDidLoad() {
    super.viewDidLoad()
    frameExtractor = FrameExtractor()
    frameExtractor.delegate = self
    self.faceDetector = GMVDetector(ofType: GMVDetectorTypeFace, options: [GMVDetectorFaceLandmarkType: GMVDetectorFaceLandmark.all.rawValue,
                                                                           GMVDetectorFaceClassificationType: GMVDetectorFaceClassification.all.rawValue,
                                                                           GMVDetectorFaceMinSize: 0.3,
                                                                           GMVDetectorFaceTrackingEnabled: true])
}

@IBAction func flipButton(_ sender: UIButton) {
    frameExtractor.flipCamera()
}

func captured(image: UIImage) {
    DispatchQueue.main.async {
        self.processImage(image: image)
        self.imageView.image = image
    }
}

func processImage(image: UIImage) {
    if imgIsProcessing {
        return
    }

    imgIsProcessing = true
    ssQ.async { [unowned self] in
           self.faces = self.faceDetector!.features(in: image, options: nil) as! [GMVFaceFeature]
            DispatchQueue.main.async {
                if self.faces.count > 0 {
                    for face in self.faces {
                        let rect = CGRect(x: face.bounds.minX, y: face.bounds.minY+100, width: face.bounds.size.width, height: face.bounds.size.height)

                        self.drawFaceIndicator(rect: rect)
                        self.lblSmiling.text = String(format: "%.3f", face.smilingProbability)
                    }
                    self.sessionCountToClr = 0
                }
                else {
                    if self.sessionCountToClr == 30 {
                        self.newView.removeFromSuperview()
                        self.lblSmiling.text = "0.0"
                        self.sessionCountToClr = 0
                    } else {
                        self.sessionCountToClr+=1
                    }
                }
                self.imgIsProcessing = false
            }
        self.faces = []
    }
}

func drawFaceIndicator(rect: CGRect) {
        newView.removeFromSuperview()
        newView = UIView(frame: rect)
        newView.layer.cornerRadius = 10;
        newView.alpha = 0.3
        newView.layer.borderColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
        newView.layer.borderWidth = 4
        self.view.addSubview(newView)
}
}

我已经将我的整个项目上传到github上了,随时可以使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在TeeCharts中使用实时图表

如何在反应中使用实时图表?

如何捕获从Android的视频录制的实时帧图像帧

如何在CVImageBuffer内的每个捕获帧上叠加视图,实时而不是后期处理

如何在Android中的相机捕获图像上设置实时帧

如何使用Opencv捕获帧

PyCharm:如何在文件模板中使用实时模板

如何在输入标签中使用javascript显示实时输入?

如何在 PhpStorm 的实时模板中使用 at 符号 (@)?

如何在C中使用GSubprocess检索实时输出?

如何在swiftui中使用实时摄像机流?

如何使用ruby实时捕获输出?

如何在熊猫数据帧中使用 re.sub

如何在R中使用sqldf操纵本地数据帧?

如何在python数据帧中使用熊猫移动值?

如何在JavaScript中使用CSS关键帧动画

如何在gulp-replace中使用捕获组?

如何在Android中使用坐标捕获图像

如何在Java中使用Fiddler捕获https

如何在Java中使用鼠标指针捕获屏幕图像

如何在Go中使用/访问捕获组?

如何在Swift中使用AVCaptureSession捕获图片?

如何在C ++中使用OpenCV以所需的fps捕获视频

如何在Python脚本中使用picamera捕获RAW JPG

如何在多个捕获组中使用先行和后行

如何在Spark结构化流中使用流数据帧更新静态数据帧

pyshark - 如何在实时捕获期间打印目标 IP?

如何在节点中使用exec输出实时live console.log

如何在实时站点上的 Django 中使用 Google 授权应用程序