-1

我有一个问题如何正确调用 CIDetector 我正在尝试实时运行人脸检测,这非常有效。然而,应用程序的内存消耗随时间线性增加,您可以在下图中看到我认为这是由于创建了对象但它们没有被释放,任何人都可以建议如何正确地做到这一点。

我已经将问题归结为这个函数,因为每次调用它时内存都会线性增加,当它终止时它会迅速下降到几乎 80 MB 而不是 11 GB 上升,还检查内存泄漏,但没有找到。

我的目标开发平台是 Mac OS,我试图从 CA 检测器中提取嘴巴位置,然后用它来计算游戏鼠标功能中的 Delta。

我也看过这篇文章,但是我尝试了他们的方法,但它对我 不起作用 CIDetector 没有释放内存

在此处输入图像描述

 fileprivate func faceDetection(){

    // setting up dispatchQueue
    dispatchQueue.async {

        //  checking if sample buffer  is equal to nil if not assign its value to sample
        if let sample = self.sampleBuffers {


            //   if allfeatures is not equal to nil. if yes assign allfeatures to features otherwise return
            guard let features = self.allFeatures(sample: sample) else { return }

            // loop to cycle through all features
            for  feature in features {

                // checks if the feature is a CIFaceFeature if yes assign feature to face feature and go on.
                if let faceFeature = feature as? CIFaceFeature {


                    if !self.hasEnded {

                        if self.calX.count > 30 {
                            self.sens.append((self.calX.max()! - self.calX.min()!))
                            self.sens.append((self.calY.max()! - self.calY.min()!))
                            print((self.calX.max()! - self.calX.min()!))
                            self.hasEnded = true
                        } else {
                            self.calX.append(faceFeature.mouthPosition.x)
                            self.calY.append(faceFeature.mouthPosition.y)

                        }

                    } else {
                        self.mouse(position:  CGPoint(x: (faceFeature.mouthPosition.x  - 300 ) * 2, y: (faceFeature.mouthPosition.y + 20 ) * 2), faceFeature: faceFeature)

                    }
                }
            }
        }

        if !self.faceTrackingEnds {
            self.faceDetection()
        }
    }
}
4

1 回答 1

1

这个问题是由于重复调用函数而不等待其完成导致修复是实现一个调度组,然后像这样在完成时调用函数现在 CIdetector 在 200 MB 内存下运行舒适

    fileprivate func faceDetection(){

       let group = DispatchGroup()
       group.enter()

       // setting up dispatchQueue
       dispatchQueue.async {

        //  checking if sample buffer  is equal to nil if not assign its value to sample
        if let sample = self.sampleBuffers {


            //   if allfeatures is not equal to nil. if yes assign allfeatures to features otherwise return
            guard let features = self.allFeatures(sample: sample) else { return }

            // loop to cycle through all features
            for  feature in features {

                // checks if the feature is a CIFaceFeature if yes assign feature to face feature and go on.
                if let faceFeature = feature as? CIFaceFeature {


                        self.mouse(position: faceFeature.mouthPosition, faceFeature: faceFeature)

                }
            }
        }
        group.leave()
    }
    group.notify(queue: .main) {
        if !self.faceTrackingEnds {
            self.faceDetection()
        }
    }

}
于 2018-06-21T12:25:50.163 回答