CIDetector没有使用UIImagePickerController(Swift)检测面部

时间:2015-09-25 15:33:25

标签: swift uiimagepickercontroller cidetector

我正在尝试用相机拍照,然后检测其中的脸部。但它不起作用...... results数组返回零计数。我用一张来自互联网的人的照片测试了这段代码,它返回了1张找到的脸。这是我的代码:

// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        Idea.CurrentIdea.idea.mockups.append(PFFile(data: UIImageJPEGRepresentation(pickedImage, 0.5)!))

        //Face Detection
        let cid:CIDetector = CIDetector(ofType:CIDetectorTypeFace, context:nil, options:[CIDetectorAccuracy: CIDetectorAccuracyHigh]);

        let cii = CIImage(CGImage: pickedImage.CGImage!)

        let results:NSArray = cid.featuresInImage(cii)

        print(results.count)
        for r in results {
            let face:CIFaceFeature = r as! CIFaceFeature;
            NSLog("Face found at (%f,%f) of dimensions %fx%f", face.bounds.origin.x, face.bounds.origin.y, face.bounds.width, face.bounds.height);
        }


    }
    dismissViewControllerAnimated(true, completion: nil)
}

有什么想法吗?谢谢!最近网上关于它的内容并不多。

1 个答案:

答案 0 :(得分:1)

确保检测器的图像方向和预期图像方向相同。有关详细信息,请参阅此答案:https://stackoverflow.com/a/17019107/919790