我有一个问题如何正确地正确调用CIDetector我正在尝试实时运行面部检测这非常有效。然而,应用程序的内存消耗随着时间线性增加,你可以在下面的图像中看到我认为这是由于对象被创建但是它们没有被释放,任何人都可以建议如何正确地做到这一点。
我已经确定了这个函数的问题,因为每当它被调用的内存在它终止时线性增加它会迅速下降到几乎80 MB而不是11 GB上升也检查内存泄漏但是没有找到。
我的目标开发平台是Mac OS我正试图从CA探测器中提取嘴部位置,然后用它来计算游戏鼠标功能的Delta。
我也看过这篇文章,但我尝试了他们的方法,但它对我不起作用 CIDetector isn't releasing memory
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()
}
}
}
答案 0 :(得分:0)
此问题是由以下原因引起的:反复调用该函数而不等待其完成,该修补程序正在实现调度组,然后在其完成时调用该函数 这样,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()
}
}
}