我正在使用MLKit尝试在照片中查找人脸,并且每次视觉图像返回时都没有人脸。
这是我当前正在使用的代码
//instantiate MLVision
lazy var vision = Vision.vision().faceDetector(options: faceDetectionOptions())
func faceDetectionOptions() -> VisionFaceDetectorOptions {
let options = VisionFaceDetectorOptions()
options.performanceMode = .accurate
options.landmarkMode = .all
options.classificationMode = .all
options.minFaceSize = CGFloat(0.1)
options.isTrackingEnabled = false
return options
}
然后我保存图像并在其中处理UIImage
func saveImage() {
let visionImage = VisionImage(image: self.canvasView!.asImage())
// begin scanning image or video
self.vision.process(visionImage, completion: { (faces, error) in
//no faces were detected
var detectedFaces = [VisionFace]()
var isSmiling = false
var smilingFaces = [Bool]()
guard error == nil, let faces = faces, !faces.isEmpty else {
return
}
for face in faces {
if face.hasSmilingProbability {
if face.smilingProbability < 0.3 {
isSmiling = false
} else {
isSmiling = true
}
}
//add smiling faces to array
if isSmiling == true {
smilingFaces.append(isSmiling)
}
//add face to faces array
detectedFaces.append(face)
}
self.contentScore.detectedFaces = detectedFaces.count
self.contentScore.smilingFaces = smilingFaces.count
// contentScore.eventAttendees = objEvent.event_users!.count
})
photoEditorDelegate?.imageEdited(image: canvasView!.asImage())
player?.pause()
self.dismiss(animated: true, completion: nil)
}
这是将UIView转换为图像的功能
extension UIView {
// Using a function since `var image` might conflict with an existing variable
// (like on `UIImageView`)
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
任何帮助将不胜感激