【问题标题】:Get array of all photos in iOS Photo Gallery with faces in them获取 iOS Photo Gallery 中包含人脸的所有照片的数组
【发布时间】:2013-10-12 08:28:05
【问题描述】:

我正在尝试浏览 iOS 设备照片库中的每张照片,包括相机胶卷、相册等。

从那里,我想使用核心图像检查照片中是否有人脸,如果有,则将其添加到名为 detectedFaceArray 的数组中。

我知道我必须使用 ALAssetsLibrary 来枚举所有组,然后是照片库中的照片,但我不知道如何在每张照片上实现核心图像。我正在使用的代码是这里问题中提出的代码:

https://stackoverflow.com/questions/18658783/enumerategroupswithtypes-alassetsgroupall-retrieves-only-the-number-of-photos-i

或

-(void)getAllPictures
{
self.galleryImages=[[NSMutableArray alloc] init];
mutableArray =[[NSMutableArray alloc]init];
NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];  
library = [[ALAssetsLibrary alloc] init];

void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{

if(result != nil)
{
    if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
    {
        [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

        NSURL *url= (NSURL*) [[result defaultRepresentation]url];

        [library assetForURL:url
                 resultBlock:^(ALAsset *asset) {
                     [mutableArray addObject:asset];
                     if ([mutableArray count]==count)
                     {
                         self.galleryImages=[[NSMutableArray alloc] initWithArray:mutableArray];
                     }
                 }
                failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ];
    }
}
};

NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
    if(group != nil) {
        [group enumerateAssetsUsingBlock:assetEnumerator];
        [assetGroups addObject:group];
        count=[group numberOfAssets];
    }
};

assetGroups = [[NSMutableArray alloc] init];

[library enumerateGroupsWithTypes: ALAssetsGroupAll
                       usingBlock:assetGroupEnumerator
                     failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}

我什至不确定这是否是要使用的正确代码,因此我们将不胜感激。我已经阅读了 ALAssetsLibrary 和 Core Image 文档,但仍然不知道该怎么做。

有什么建议吗?谢谢!

【问题讨论】:

    标签: ios objective-c core-image alassetslibrary photo-gallery


    【解决方案1】:

    我完全不知道如何从图像中检测人脸。但是您可以阅读 this link 并下载 OpenCV 来做到这一点

    【讨论】:

    • 我知道如何使用Core Image来做,我只是不知道把代码放在哪里做......?感谢您的链接
    • @falky 你有想过这个吗?我需要做类似的事情
    • @michael 不,我没有抱歉
    【解决方案2】:

    对于那些仍在寻找问题的可能答案的人。我正在使用这段代码来枚举资产并保存那些有面孔的资产。

    [self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                    [group setAssetsFilter:[ALAssetsFilter allPhotos]];
                    [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                        if(result && self.galleryAssets.count <= 20) {
                           CIImage *ciImage = [CIImage imageWithCGImage:result.thumbnail];
                            NSNumber *orientation = [NSNumber numberWithInt:[[UIImage imageWithCIImage:ciImage] imageOrientation]+1];
                            NSDictionary *fOptions = [NSDictionary dictionaryWithObject:orientation forKey: CIDetectorImageOrientation];
                            NSArray *features = [detector featuresInImage:ciImage options:fOptions];
    
                            if(features.count != 0) {
                                [self.galleryAssets addObject:result];
                            }
                            *stop = NO;
                        } else {
                            *stop = YES; return;
                        }
                    }];
    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if(self.galleryAssets.count == 0) {
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Photos" message:@"You have no photos in your gallery." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                            [alert show];
                        } 
                    });
                } failureBlock:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 2014-07-04
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多