CIDetector 实时人脸检测 但是内存消耗线性增加如何避免这个问题?
CIDetector face detection in real-time However memory consumption increases linearly how to avoid this issue?
我有一个问题如何正确地正确调用 CIDetector 我正在尝试 运行 实时人脸检测这非常有效。但是,应用程序的内存消耗会随着时间线性增加,如下图所示,我认为这是由于创建了对象但未释放它们,谁能建议如何正确执行此操作。
我已将问题归结为此函数,因为每次调用它时,内存都会线性增加,当它终止时,它会迅速下降到近 80 MB,而不是上升 11 GB,同时还要检查内存泄漏,但是 none 是找到了。
我的目标开发平台是 Mac OS 我正在尝试从 CA 检测器中提取嘴部位置,然后用它来计算游戏鼠标功能中的 Delta。
我也看过这个 post 但是我已经尝试了他们的方法但是它对我不起作用
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()
}
}
}
此问题是由于重复调用该函数而不等待其完成引起的修复方法是实现一个调度组,然后在其完成时调用该函数
像这样现在 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()
}
}
}
我有一个问题如何正确地正确调用 CIDetector 我正在尝试 运行 实时人脸检测这非常有效。但是,应用程序的内存消耗会随着时间线性增加,如下图所示,我认为这是由于创建了对象但未释放它们,谁能建议如何正确执行此操作。
我已将问题归结为此函数,因为每次调用它时,内存都会线性增加,当它终止时,它会迅速下降到近 80 MB,而不是上升 11 GB,同时还要检查内存泄漏,但是 none 是找到了。
我的目标开发平台是 Mac OS 我正在尝试从 CA 检测器中提取嘴部位置,然后用它来计算游戏鼠标功能中的 Delta。
我也看过这个 post 但是我已经尝试了他们的方法但是它对我不起作用 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()
}
}
}
此问题是由于重复调用该函数而不等待其完成引起的修复方法是实现一个调度组,然后在其完成时调用该函数 像这样现在 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()
}
}
}