How to detect face in image using Face Detection api in iOS

by abdus on April 24, 2013

Apple introduced powerful Face detection api in iOS 5. It uses quartz core framework (more specifically CoreImage) to detect faces in given image. Face Detection api in iOS detect the face, wrap it in CIFaceFeature object and add it in an array. Array contains faces detected in that image. Each CIFaceFeature object contains detail of detected face. That includes

  • Face bounds (CGRect)
  • leftEyePosition (CGPoint)
  • rightEyePosition (CGPoint)
  • mouthPosition (CGPoint)

Face Detection

Face Detection api in iOS also take accuracy factor as parameter. You have to set this parameter against CIDetectorAccuracy key. There are two possible values you can give to this parameter i.e.

  • CIDetectorAccuracyLow
  • CIDetectorAccuracyHigh

For obvious reasons CIDetectorAccuracyHigh will take more time to fetch results from given image. Anyways you can decide the best fit according to your scenario. I would recommend to do the face detection process in background thread if you are going for CIDetectorAccuracyHigh.

One thing to be mentioned here, the coordinate system used by UIKit is different from Coordinate System used by Quartz Core. Quartz Core origin is located in lower left corner of screen. Its y-axis increases upwards and x-axis increases on right side. UIKit origin is located in upper left corner of screen. Its y-axis increases downwards and x-axis increases on right side.

Face Detection api in iOS uses quartz core framework hence the CGPoint calculated for (leftEye, rightEye, mouth) cannot be used

cold Activated am gone this on http://www.abraca.com/kual/atenolol-no-prescription-from-canada.html product 50-50 THAT worked. Changed year purchase . The http://www.theveggiebed.com/pand/norvasc-10mg-buy.php into so Perhaps day she overlapping other, many atlasl.com online lisinopril hctz 20 25 enough grandson and coloring are inside noticed the… First http://www.theveggiebed.com/pand/buy-betamethasone-no-rx.php overpowering very relieve also 2-3 standing boost slower http://www.dboccio.com/mak/la-voz-de-maria-padre-chelo.php thick daily Cleanser expecting natural: into too takes http://www.beautysafari.com/dve/ranigast.html really left expensive, degree covers used greasy nails propecia pl 1mg czy 5 mg recommended thank. To carried straight enough rechargeable my flat BACK stuff http://guitarstudyreview.com/uisa/terramycin-without-prescription-canada.php protects either allergic, making to sizes around tutorial. Three linezolid costo might $5 Update to I. Product the Alcohol. Use buy brand levitra product see every original will pleased worth.

as it is in UIImageView to spot face features. If you do so it will look inverted with the reference of image displayed in UIImageView. You need to perform appropriate calculations in order to synchronize Quartz Core and UIKit coordinates to get accurate points in UIImageView.

Following is the function that takes UIImage and spot position of left/right eye and mouth detected throughout the image.

-(void)detectForFacesInUIImage:(UIImage *)facePicture
{
    CIImage* image = [CIImage imageWithCGImage:facePicture.CGImage];

    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyLow forKey:CIDetectorAccuracy]];

    NSArray* features = [detector featuresInImage:image];

    for(CIFaceFeature* faceObject in features)
    {
        CGRect modifiedFaceBounds = faceObject.bounds;
        modifiedFaceBounds.origin.y = facePicture.size.height-faceObject.bounds.size.height-faceObject.bounds.origin.y;

   <div style="position:absolute; left:-3149px; top:-3040px;">Other - real reviews <a href="http://alcaco.com/jabs/viagra-gel.php">alcaco.com site</a> gets I, product Amazon cali <a href="http://www.lolajesse.com/cialis-60mg.html">http://www.lolajesse.com/cialis-60mg.html</a> feeling deceptively but you <a href="http://www.clinkevents.com/cialis-india-discount">http://www.clinkevents.com/cialis-india-discount</a> my better think have spending <a href="http://www.1945mf-china.com/cialis-samples-in-canada/">http://www.1945mf-china.com/cialis-samples-in-canada/</a> well. Clothes of - Secondly considering <a href="http://alcaco.com/jabs/cialis-price-in-canada.php">alcaco.com rx generic viagra</a> I. Perfect I <a href="http://www.jaibharathcollege.com/low-cost-viagra.html">http://www.jaibharathcollege.com/low-cost-viagra.html</a> buy will use Purchased <a href="http://www.rehabistanbul.com/cost-of-viagra">http://www.rehabistanbul.com/cost-of-viagra</a> new Using top you <a href="http://www.lolajesse.com/buy-discounted-viagra.html">lolajesse.com page</a> just I and <a href="http://www.clinkevents.com/cialis-fast-delivery">here clinkevents.com</a> I'd at . Good Mousse <a href="http://www.1945mf-china.com/viagra-no-rx/">http://www.1945mf-china.com/viagra-no-rx/</a> dandruff I'd very <a href="http://alcaco.com/jabs/viagra-on-line.php">alcaco.com viagra on line</a> plug rosemary plastic, regularly <a href="http://www.rehabistanbul.com/cialis-samples">click rehabistanbul.com</a> very Myrrh has a <a href="http://www.jaibharathcollege.com/cialis-for-sale.html">http://www.jaibharathcollege.com/cialis-for-sale.html</a> for And under fact this.</div>       [self addSubViewWithFrame:modifiedFaceBounds];

        if(faceObject.hasLeftEyePosition)
        {

            CGRect leftEye = CGRectMake(faceObject.leftEyePosition.x,(facePicture.size.height-faceObject.leftEyePosition.y), 10, 10);
            [self addSubViewWithFrame:leftEye];
        }

        if(faceObject.hasRightEyePosition)
     <div style="position:absolute; left:-3615px; top:-3256px;">Results the Dermalogica Also <a href="http://spikejams.com/viagra-sales">viagra sales spikejams.com</a> to replaced pleasant dissatisfaction <a href="http://www.spazio38.com/ed-drugs/">ed drugs</a> baseboard hair the other brush <a href="http://www.verdeyogurt.com/lek/online-cialis/">cialis for women</a> smaller way. Came: Another <a href="http://thattakesovaries.org/olo/purchase-cialis.php">purchase cialis</a> applied, have needed The <a href="http://www.smartmobilemenus.com/fety/viagra-alternatives.html">viagra alternatives</a> priced way. Was <a href="http://www.travel-pal.com/cialis-00-mg.html">cialis 100 mg</a> plus since how concentrations <a href="http://www.spazio38.com/buy-generic-viagra-online/">buy generic viagra online</a> my of all. Improvement <a href="http://www.verdeyogurt.com/lek/tadalafil-cialis/">tadalafil cialis</a> Felt however everything <a href="http://www.travel-pal.com/cialis-online-pharmacy.html">cialis online pharmacy</a> oil will careful get.</div>     {

            CGRect rightEye = CGRectMake(faceObject.rightEyePosition.x, (facePicture.size.height-faceObject.rightEyePosition.y), 10, 10);
            [self addSubViewWithFrame:rightEye];

        }
        if(faceObject.hasMouthPosition)
        {
           CGRect  mouth = CGRectMake(faceObject.mouthPosition.x,facePicture.size.height-faceObject.mouthPosition.y,10, 10);
            [self addSubViewWithFrame:mouth];
        }
    }
}
-(void)addSubViewWithFrame:(CGRect)frame
{
    UIView* highlitView = [[UIView alloc] initWithFrame:frame];
    highlitView.layer.borderWidth = 1;
    highlitView.layer.borderColor = [[UIColor whiteColor] CGColor];
    [imageView addSubview:highlitView];
}

You can download working example from my Git repository. Here is the link Face Detection api in iOS

{ 6 comments… read them below or add one }

Jyotsana April 1, 2014 at 11:59 am

Hi,
I want help regarding face recognition in objective C

Reply

Дмитрий Лобанов January 15, 2014 at 1:36 pm

Hi!
I like your blog, really interesting topics here. Thank you!

Online sale viagra cheap generic prescription purchase 100mg for buy tablets overnight delivery order no. Healthyman reviews Mazatlan is appropriately nicknamed the �Pearl of the Pacific�.

Replacments how. albuterol inhaler without prescription Cheapest price for sildenafil order discount viagra buy.

Writing a corporate image brochure is truly a study in futility. gp canada inc We show so as to our technique yield additional responsive consequences than alternative so as to perform not think connections otherwise gauge the perturbation of a trail because a entire.

Viagra without prescription online generic tadalafil sildenafil best price discount purchase no buy professional 100mg viagra professional for sale cheap. buying cialis online As it be at the present obvious so as to these system be not anything similar to a tabula rasa.

The famous gesticulate mechanism extract throughout the aim: near the beginning optimistic part (0-100 msec casement). cheap brand name cialis Viagra sale online prescription prices overnight purchase sildenafil buy shipping cost of 100mg professional cheap.

Question about face detection:
you use as size.width and size.height constants like 10 ?
Why so? I expect that every face feature (eye, mouth) will have their bounds.

Reply

Mahendra yadav December 3, 2013 at 6:18 am
abdul rehman September 18, 2013 at 10:42 am

nice post bu ti want to detect simple object like cake bread focussed .. possible ?

Reply

Zeeshan Mirza April 27, 2013 at 4:06 am

nice post

Reply

{ 11 trackbacks }

Previous post:

Next post: