3

I'm trying to go through every photo in an iOS Device's photo library including camera roll, albums, etc..

From there, I want to check if the photo has a face in it, using core image, and if so, add it to an array called detectedFaceArray.

I understand I have to use ALAssetsLibrary to enumerate through all the groups and then photos in the photo gallery, but I have no idea how to implement core image on each photo. The code I am using is the code being asked in this question here:

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

OR

-(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");}];
}

I'm not even sure if this is the correct code to be using so any help would be appreciated. I have read through the ALAssetsLibrary and Core Image Documentation but still don't know how I could do it.

Any suggestions? Thank you!

Community
  • 1
  • 1
falky
  • 589
  • 2
  • 11
  • 27

2 Answers2

2

I exactly don't know how to detect a face from image. But you can read this link & download OpenCV to do that

Akshit Zaveri
  • 4,166
  • 6
  • 30
  • 59
0

For those who still looking for a possible answer to the question. I am using this code to enumerate assets and save those that have faces on them.

[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];
John Paul Manoza
  • 1,735
  • 25
  • 22