3

我正在尝试浏览 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 Documentation,但仍然不知道我该怎么做。

有什么建议么?谢谢!

4

2 回答 2

2

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

于 2013-10-12T09:06:26.797 回答
0

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

[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];
于 2014-10-30T17:02:13.897 回答