在一张图片里查找人脸

作者: 轻云绿原 | 来源:发表于2017-04-13 14:57 被阅读544次

    来源

    https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_detect_faces/ci_detect_faces.html#//apple_ref/doc/uid/TP30001185-CH8-SW1

    Core Image可以在一张图片里分析并查找出人脸.它执行的是人脸查找,而不是识别.人脸detection是把人脸用方框标识出来的功能,而人脸recognition是认出一张特殊的人脸(知道这脸是张三,李四等等).在Core Image查找出人脸后,它可以提供这张脸的信息,比如,眼睛和嘴的位置.它也能在视频里跟踪已识别的人脸的位置.

    Core Image identifies face bounds in an imageCore Image identifies face bounds in an image

    知道一张图片里的人脸的位置,可以让你执行其它的操作,比如裁剪或调整人脸图片的质量(色调平衡,红眼矫正等).你也可以在脸部做其它更有趣的操作:比如:

    人脸查找只支持iOS 5.0 和 OS X 10.7以上.


    查找人脸

    使用CIDetector类在一张图片上查找人脸

    CIContext *context = [CIContext context];                    // 1
    NSDictionary *opts = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh };      // 2
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:context
                                              options:opts];                    // 3
     
    opts = @{ CIDetectorImageOrientation :
              [[myImage properties] valueForKey:kCGImagePropertyOrientation] }; // 4
    NSArray *features = [detector featuresInImage:myImage options:opts];        // 5
    

    解释一下以上代码:

    1. 用默认选项创建一个context.(当你要创建detector时,你也可以nil来代替context)
    2. 创建一个选项dictionary来指明detector的精度.你可以要底精度或高精度.底精度就查找快.高精度,全面但是慢.
    3. 创建一个人脸查找.这里的查找的唯一类型就是人脸(还有其它类型,看下面.)
    4. 给查找人脸设置一下选项.让Core Image知道图片的方向是很重要的,要让检测器知道在哪里可以找到直立的面孔,大多数时候,你可以从图片本身知道图片的方向,并提供给options dictionary.
    5. 用detector在一张图片上查找特征.你提供的图片必须是CIImage对象.图片返回了一个CIFeature对象的数组,每一个CIFeature对象都代表了图里的一张脸.

    在你得到一个脸的数组时,你很可能要找出它们的特征,比如眼睛和嘴的位置.下一节会解释

    // Types to be used for +[CIDetector detectorOfType:context:options:]
    
    /* Specifies a detector type for face recognition. */
    @available(iOS 5.0, *)
    public let CIDetectorTypeFace: String
    
    /* Specifies a detector type for rectangle detection. */
    @available(iOS 8.0, *)
    public let CIDetectorTypeRectangle: String
    
    /* Specifies a detector type for barcode detection. */
    @available(iOS 8.0, *)
    public let CIDetectorTypeQRCode: String
    
    /* Specifies a detector type for text detection. */
    
    @available(iOS 9.0, *)
    public let CIDetectorTypeText: String
    

    获得脸和脸部特征

    脸部特征包括:

    • 左右眼的位置
    • 嘴的位置
    • 在一个视频片断里跟踪一张脸的ID和帧数

    你在得到一组脸部特征时,你可循环查找每张脸的界限和在这张脸的每个特征,如下:

    for (CIFaceFeature *f in features) {
        NSLog(@"%@", NSStringFromRect(f.bounds));
     
        if (f.hasLeftEyePosition) {
            NSLog(@"Left eye %g %g", f.leftEyePosition.x, f.leftEyePosition.y);
        }
        if (f.hasRightEyePosition) {
            NSLog(@"Right eye %g %g", f.rightEyePosition.x, f.rightEyePosition.y);
        }
        if (f.hasMouthPosition) {
            NSLog(@"Mouth %g %g", f.mouthPosition.x, f.mouthPosition.y);
        }
    }
    

    视频里跟踪人脸

    open class CIFaceFeature : CIFeature
    
    var trackingID: Int32 { get }
    Description 
    The tracking identifier of the face object.
    Core Image provides a tracking identifier for faces it detects in a video stream, which you can use to identify when a CIFaceFeature objects detected in one video frame is the same face detected in a previous video frame.
    This identifier persists only as long as a face is in the frame and is not associated with a specific face. In other words, if a face moves out of the video frame and comes back into the frame later, another ID is assigned. (Core Image detects faces, but does not recognize specific faces.)
    Availability    iOS (8.0 and later), macOS (10.10 and later), tvOS (9.0 and later)
    Declared In Core Image
    More    Property Reference
    

    trackingID是用来在标识一张人脸对象.
    在一个视频流里,Core Image为它找到的人脸提供一个跟踪标识,它被用来区分CIFaceFeature在前一帧和当前帧找到的人脸是否是同一张脸.
    只要这张在脸还在帧里,这个标识就会保持,但它并不和特定的脸相关联.换句话说,如果脸移出了视频帧并且再回来,另一个标识就会被赋于(Core Image只查找人脸,并不认出人脸).

    这个trackingID可以被用来当活体检测.

    相关文章

      网友评论

      • GavinKang:写的不错哦,希望楼主继续更新

      本文标题:在一张图片里查找人脸

      本文链接:https://www.haomeiwen.com/subject/nhcbattx.html