我是Swift Programming的新手。我正在尝试使用核心图形来完成人脸检测。到目前为止,我写了以下内容。
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)")
执行时,这不会产生任何结果。
答案 0 :(得分:1)
您应该使用FileManager
来检索系统网址。
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)