Swift中的人脸检测
我是Swift编程的新手。我正在尝试使用核心图形来完成人脸检测。到目前为止,我已经写了以下内容Swift中的人脸检测,swift,macos,Swift,Macos,我是Swift编程的新手。我正在尝试使用核心图形来完成人脸检测。到目前为止,我已经写了以下内容 let ciimage=CIImage(contentsOf: "/Users/me/Desktop/test.jpg"); let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh] let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context:
let ciimage=CIImage(contentsOf: "/Users/me/Desktop/test.jpg");
let accuracy = [CIDetectorAccuracy: CIDetectorAccuracyHigh]
let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: accuracy)
for face in faces as! [CIFaceFeature] {
print("Found bounds are \(face.bounds)")
}
我不知道如何从本地文件创建cimage。似乎我需要输入一个URL。。
我做错了什么,请指教
更新:
我已根据提供的答案更新了代码
let userDirectory = FileManager.default.homeDirectoryForCurrentUser
let desktopDirectory = userDirectory.appendingPathComponent("Desktop")
let pictureUrl = desktopDirectory.appendingPathComponent("test").appendingPathExtension("jpg")
let image = CIImage(contentsOf: pictureUrl)
let faceDetector = CIDetector(ofType: CIDetectorTypeFace, context: nil, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
let faces = faceDetector?.features(in: image!) as! [CIFaceFeature]
print("Number of faces: \(faces.count)")
执行时,这不会产生任何结果。您应该使用
FileManager
检索系统URL
guard let desktopDirectory = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first else {return}
let pictureUrl = desktopDirectory.appendingPathComponent("test").appendingPathExtension("jpg")
let image = CIImage(contentsOf: pictureUrl)
@利奥·达布斯。。这个问题适用于iOS。。这是指OS X刚刚意识到您遇到的问题是什么?
ciimage
nil?还是有错误?@DávidPásztor有没有办法从本地文件创建ciimage?我为创建ciimage而编写的代码无效。它需要是URL。您应该使用FileManager.default.URL(用于:.desktopDirectory,位于:.userDomainMask中)。首先
获取用户桌面url@techno“这不提供任何结果”是什么意思?CIImage
nil
?实际上它可以工作。。我需要手动启用调试区域。Xcode的新功能。。已经使用VS一段时间了。因此预期调试控制台在默认情况下处于活动状态。if let faces=faceDetector?。功能(在:图像中)为?[CIFaceFeature]{
@LeoDabus我将提出一个新问题。