如何使简单的人脸检测正常工作?

时间:2019-03-31 20:21:40

标签: firebase tensorflow-lite firebase-mlkit

我正在尝试使用iOS上的ML Kit来获取人脸检测的简单示例。这是目标C代码的摘录:

    FIRVisionFaceDetectorOptions *faceDetectorOptions;
    FIRVision *vision;
    FIRVisionFaceDetector *faceDetector;

    faceDetectorOptions = [[FIRVisionFaceDetectorOptions alloc] init];
    faceDetectorOptions.performanceMode = FIRVisionFaceDetectorPerformanceModeAccurate;
    faceDetectorOptions.landmarkMode = FIRVisionFaceDetectorLandmarkModeAll;
    faceDetectorOptions.contourMode = FIRVisionFaceDetectorContourModeNone;
    faceDetectorOptions.classificationMode = FIRVisionFaceDetectorClassificationModeAll;
    faceDetectorOptions.minFaceSize = 0.1; // TODO: finalize this option value

    vision = [FIRVision vision];
    faceDetector = [vision faceDetectorWithOptions:faceDetectorOptions];

    UIImage *staticImg = [UIImage imageNamed:@"sample.jpg"];
    FIRVisionImage *visionImage = [[FIRVisionImage alloc] initWithImage:staticImg];

    NSError* error = Nil;
    NSArray<FIRVisionFace *> * faces = [faceDetector resultsInImage:visionImage error:&error];
    NSLog(@"Synchronous result. error = %@, face count = %lu", error, faces.count);

sample.jpg文件是下载的以下图像,并作为资源添加到我的Xcode项目中: http://chwb.org/wp-content/uploads/2014/01/Theo_Janssen-Face1.jpg

resultsInImage没有返回错误,但是也没有面孔。它记录:

Synchronous result. error = (null), face count = 0

我做错什么了吗?

1 个答案:

答案 0 :(得分:0)

我知道了。问题是我需要将图像元数据的方向设置为:

    FIRVisionImageMetadata *imageMetadata = [FIRVisionImageMetadata new];
    imageMetadata.orientation = [FcFaceDetector visionImageOrientationFromImageOrientation:uiImage.imageOrientation];
    visionImage.metadata = imageMetadata;
+ (FIRVisionDetectorImageOrientation) visionImageOrientationFromImageOrientation:(UIImageOrientation)imageOrientation {
    switch (imageOrientation) {
            case UIImageOrientationUp:
            return FIRVisionDetectorImageOrientationTopLeft;
            case UIImageOrientationDown:
            return FIRVisionDetectorImageOrientationBottomRight;
            case UIImageOrientationLeft:
            return FIRVisionDetectorImageOrientationLeftBottom;
            case UIImageOrientationRight:
            return FIRVisionDetectorImageOrientationRightTop;
            case UIImageOrientationUpMirrored:
            return FIRVisionDetectorImageOrientationTopRight;
            case UIImageOrientationDownMirrored:
            return FIRVisionDetectorImageOrientationBottomLeft;
            case UIImageOrientationLeftMirrored:
            return FIRVisionDetectorImageOrientationLeftTop;
            case UIImageOrientationRightMirrored:
            return FIRVisionDetectorImageOrientationRightBottom;
    }
}

文档似乎尚不清楚,因为它似乎建议不要设置它: https://firebase.google.com/docs/ml-kit/ios/detect-faces#2-run-the-face-detector