UrbanPro
true

Learn Mobile App Development from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Microsoft Cognitive Face API (iOS) - Identifying Faces From an Image and Extracting Facial Features Like Age, Gender, Emotions.

Abhishek Dwivedi
22/05/2018 1 0

Microsoft Cognitive Services is a set of APIs, SDKs and services available to developers to make their applications more intelligent, engaging and discoverable. You can basically do a lot of cool stuffs using the platform and we are going to see one of those cool stuffs today. 

We are going to integrate Microsoft Cognitive Face API in an iOS application. We will extract faces from a given image and also facial features like age, gender, emotions like smile, anger etc. Basically you can click a photo from your iPhone app, and detect how many people are present in the photo, their age and gender, and their emotions. Sounds exciting ? Let dive in.

 

I am expecting a basic understanding of iOS application development for this tutorial.

Step 1. Create a single view application.

 

Step 2. Install cocoapods if you don't already have it. Its basically a dependency manager and will help us    install third party SDKs / libraries in our applications.

 

Step 3. Open terminal, and go inside the project folder.

 

Step 4. Type in, pod init. It creates a pod file in your project folder. Open the podfile using xcode.

 

Step 5. Edit the podfile with the below contents / code. Save and close.

# Uncomment the next line to define a global platform for your project

platform :ios, '9.0'

 

target 'MyCoolProject' do

# Comment the next line if you're not using Swift and don't want to use dynamic frameworks

use_frameworks!

 

# Pods for MyCoolProject

pod 'ProjectOxfordFace', :git => 'https://github.com/Microsoft/Cognitive-Face-iOS'

end

 

Step 6. Open the xcworkspace created in your project folder. 

 

Step 7. Integrate an image picker to input an image from your camera. (Its pretty straight forward and you will find many tutorials for this.)

 

Step 8. In your ViewController, import the following.

#import <Photos/Photos.h>

#import "UIImage+FixOrientation.h"

#import "UIImage+Crop.h"

#import "ImageHelper.h"

#import <ProjectOxfordFace/MPOFaceServiceClient.h>

 

Step 9. Add a 'Analyse' button in the UI and add the below action code for your analyse button.

- (IBAction)analyseClicked:(id)sender {

    

MPOFaceServiceClient *client = [[MPOFaceServiceClient alloc] initWithEndpointAndSubscriptionKey:ProjectOxfordFaceEndpoint key:ProjectOxfordFaceSubscriptionKey];

    

NSData *data = UIImageJPEGRepresentation(selectedImage, 0.8);

    

[client detectWithData:data returnFaceId:YES returnFaceLandmarks:YES returnFaceAttributes:@[@(MPOFaceAttributeTypeGender), @(MPOFaceAttributeTypeAge),@(MPOFaceAttributeTypeEmotion), @(MPOFaceAttributeTypeSmile)] completionBlock:^(NSArray *collection, NSError *error) {

        if (error) {

            NSLog(@"Detection failed with error %@",[error localizedDescription]);

            return;

        }

        [self->detectedFaces removeAllObjects]; //detectedFaces is a NSMutableArray

        for (MPOFace *face in collection) {

            UIImage *croppedImage = [self->selectedImage crop:CGRectMake(face.faceRectangle.left.floatValue, face.faceRectangle.top.floatValue, face.faceRectangle.width.floatValue, face.faceRectangle.height.floatValue)];

            MPODetectionFaceObject *obj = [[MPODetectionFaceObject alloc] init];

            obj.croppedFaceImage = croppedImage;

            obj.genderText = [NSString stringWithFormat:@"Gender: %@", face.attributes.gender];

            obj.ageText = [NSString stringWithFormat:@"Age: %@", face.attributes.age.stringValue];

            NSLog(@"Gender : %@", face.attributes.gender);

            NSLog(@"Age : %d", face.attributes.age.intValue);

            NSLog(@"Smile : %d", face.attributes.smile.intValue);

            NSLog(@"Happiness : %d", face.attributes.emotion.happiness.intValue);

            NSLog(@"Sadness : %d", face.attributes.emotion.sadness.intValue);

            [self->detectedFaces addObject:obj];

        }

 

    }];

}

 

1 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You

Programing Languages Learning Tricks
You want to learn that new language or library or framework as soon as possible, right? That’s understandable. Fortunately, there are a handful of tips that can help you to better retain all of that...

Harshal G.

0 0
0

Xamarin Evolve 2016
Xamarin is one of the best cross-platform frameworks I have ever seen in my life. That is the biggest sell, you can write 1 mobile app and then “tailor” it for 3 different platforms. Great....

What Is Swift?
1. It is a new programming language that was created by Apple. 2. It is entirely built from the ground up. 3. It was announced at the WWDC in June of 2014. 4. But it was in development for number of...
R

10 Ways My Life Changed When I Learned To Code
1. I have freedom to make my own schedule. 2. I earn more, but work fewer hours. 3. I can say “no” to job offers. 4. People come to me asking if I can work for them. 5. I never have to...

The Future of Mobile App Development
It is a not a big surprise to know that about 179 billion mobile applications are downloaded per year. This big figure indicates about the future of mobile apps .It is a fast growing sector. Like other...
X

Looking for Mobile App Development Classes?

The best tutors for Mobile App Development Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Mobile App Development with the Best Tutors

The best Tutors for Mobile App Development Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more