osx - How do I perform Face Detection in Swift -


im using following code detect face image.

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)") 

how can detect face , save nsimage?

xcode 9 • swift 4

extension nsimage {     var ciimage: ciimage? {         guard let data = tiffrepresentation else { return nil }         return ciimage(data: data)     }     var faces: [nsimage] {         guard let ciimage = ciimage else { return [] }         return (cidetector(oftype: cidetectortypeface, context: nil, options: [cidetectoraccuracy: cidetectoraccuracyhigh])?             .features(in: ciimage) as? [cifacefeature])?             .map {                 let ciimage = ciimage.cropped(to: $0.bounds)  // swift 3 use cropping(to:)                 let imagerep = nsciimagerep(ciimage: ciimage)                 let nsimage = nsimage(size: imagerep.size)                 nsimage.addrepresentation(imagerep)             return nsimage         }  ?? []     } } 

testing

let image = nsimage(contentsof: url(string: "https://i.stack.imgur.com/xs4rx.jpg")!)! let faces = image.faces 

enter image description here


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -