Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Face detection with OpenCV in iOS

    • 0
    • 0
    • 0
    • 0
    • 6
    • 0
    • 2
    • 0
    • 4.29k
    Comment on it

    Hello Readers!
    If you want set face detection in iOS (iPhone) follow the steps given below.

    First of all we need to add opencv framework to our project. There are severel ways to do that. But the most simple is as follows:

    Make framework via CMake

    install home-brew first-
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    Then install cmake with home-brew with following:
    brew install cmake

    After installing cmake fire following command:
    => git clone https://github.com/Itseez/opencv.git
    => sudo ln -s /Applications/Xcode.app/Contents/Developer Developer
    => python opencv/platforms/ios/build_framework.py ios

    After few minutes directory in which terminal is heading, you will get /ios/opencv2.framework

    Now you need to clone opencv master for your convenience. Follow URL :
    https://github.com/Itseez/opencv

    Now In the the downloaded folder you will find CvEffects xcode project which need to be included in your project.

    Now create a new Xcode project and name it as Sample_Face_Detection and add opencv2.framework framework to your project.

    In the main.storyboard at first ViewController add one UIImageView and two button named as start capture and stop capture respectively as per your convenience.


    Now add attached files in this blog to your Xcode project
    1. glasses.png
    2. mustache.png
    3. lbpcascade_frontalface.xml
    4. haarcascade_mcs_eyepair_big.xml
    5. haarcascade_mcs_mouth.xml



    Its time to add code for ViewController
    In your viewController add following steps:
    1. import these in viewcontroller.h

    #import "ios.h"
    #import "CvEffects/RetroFilter.hpp"
    #import "CvEffects/FaceAnimator.hpp"
    



    2. add videoCamera delegate like follow:

    @interface ViewController : UIViewController<CvVideoCameraDelegate>
    



    3. Set data types for controller

    CvVideoCamera* videoCamera;
    FaceAnimator::Parameters parameters;
    cv::Ptr<FaceAnimator> faceAnimator;
    



    4. Add following properties:

    @property (nonatomic, strong) CvVideoCamera* videoCamera;
    @property (nonatomic, strong) IBOutlet UIImageView *imageView;
    

    Map this imageView to you view controller Xib.


    5. Add following actions

    -(IBAction)startCaptureButtonPressed:(id)sender;
    -(IBAction)stopCaptureButtonPressed:(id)sender;
    

    Map these action to start and stop capture in view controller Xib in storyboard.

    In .m file of viewController add following code in Viewdidload:

       self.videoCamera = [[CvVideoCamera alloc]
                            initWithParentView:imageView];
        self.videoCamera.delegate = self;
        self.videoCamera.defaultAVCaptureDevicePosition =
                                AVCaptureDevicePositionFront;
        self.videoCamera.defaultAVCaptureSessionPreset =
                                AVCaptureSessionPreset352x288;
        self.videoCamera.defaultAVCaptureVideoOrientation =
                                AVCaptureVideoOrientationPortrait;
        self.videoCamera.defaultFPS = 30;
    

    This above code will initiate CvVideoCamera with properties set to it.

    // Load images
        UIImage* resImage = [UIImage imageNamed:@"glasses.png"];
        UIImageToMat(resImage, parameters.glasses, true);
        cvtColor(parameters.glasses, parameters.glasses, CV_BGRA2RGBA);
    
        resImage = [UIImage imageNamed:@"mustache.png"];
        UIImageToMat(resImage, parameters.mustache, true);
        cvtColor(parameters.mustache, parameters.mustache, CV_BGRA2RGBA);
    
        // Load Cascade Classisiers
        NSString* filename = [[NSBundle mainBundle]
                              pathForResource:@"lbpcascade_frontalface"
                                       ofType:@"xml"];
        parameters.faceCascade.load([filename UTF8String]);
    
        filename = [[NSBundle mainBundle]
                    pathForResource:@"haarcascade_mcs_eyepair_big"
                             ofType:@"xml"];
        parameters.eyesCascade.load([filename UTF8String]);
    
        filename = [[NSBundle mainBundle]
                    pathForResource:@"haarcascade_mcs_mouth"
                             ofType:@"xml"];
        parameters.mouthCascade.load([filename UTF8String]);
    

    These XMLs are used to identify different parts of your face as in our example lbpcascade_frontalface.xml, haarcascade_mcs_eyepair_big.xml and haarcascade_mcs_mouth.xml is used to identify front face, both eyes and mouth during detecting your whole face respectively.

    Now add following functions in your file
    To start capture :

    -(IBAction)startCaptureButtonPressed:(id)sender
    {
        [videoCamera start];
        isCapturing = YES;
    
        faceAnimator = new FaceAnimator(parameters);
    }
    

    To stop capture :

    -(IBAction)stopCaptureButtonPressed:(id)sender
    {
        [videoCamera stop];
        isCapturing = NO;
    }
    

    CvVideoCameraDelegate function which run once face animator runs:

    - (void)processImage:(cv::Mat&)image
    {
        faceAnimator->detectAndAnimateFaces(image);
    }
    

    Now run the code and you will find glasses on your eye pair and mustache on your upper lips. Since with the help of these XMLs app will detect you eyes and lips.

    Face detection with OpenCV in iOS

 6 Comment(s)

  • Is it possible to apply this code to a single image rather than to a video stream? I tried removing the video camera and adding the following code the the view controller:

    // Load image and xml files for facial features
    UIImage* resImage = [UIImage imageNamed:@"glasses.png"];
    UIImageToMat(resImage, parameters.glasses, true);
    cvtColor(parameters.glasses, parameters.glasses, CV&#95;BGRA2RGBA);
    resImage = [UIImage imageNamed:@"mustache.png"];
    UIImageToMat(resImage, parameters.mustache, true);
    cvtColor(parameters.mustache, parameters.mustache, CV&#95;BGRA2RGBA);
    // Load Cascade Classisiers
    NSString* filename = [[NSBundle mainBundle]
                          pathForResource:@"lbpcascade&#95;frontalface"
                          ofType:@"xml"];
    parameters.faceCascade.load([filename UTF8String]);
    filename = [[NSBundle mainBundle]
                pathForResource:@"haarcascade&#95;mcs&#95;eyepair&#95;big"
                ofType:@"xml"];
    parameters.eyesCascade.load([filename UTF8String]);
    filename = [[NSBundle mainBundle]
                pathForResource:@"haarcascade&#95;mcs&#95;mouth"
                ofType:@"xml"];
    parameters.mouthCascade.load([filename UTF8String]);
    
    
    
    // Single image feature detection:
    // Steps -- create CV image
    UIImage* faceImage = [UIImage imageNamed:@"Clark.png"]; // The picture that was taken (should have a face)
    cv::Mat faceMat;
    UIImageToMat(faceImage, faceMat, false);  // Convert image to opencv matrix
    
    // Detect and draw with face animator
    faceAnimator = new FaceAnimator(parameters);
    faceAnimator->detectAndAnimateFaces(faceMat); // How do I get the result of this?? Shouldn't this set the faceMat to an image that has the features drawn on?
    
    // Print features
    faceImage = MatToUIImage(faceMat);
    _imageView.image = faceImage;
    

    When running the following code I get a log saying that a face has been detected, yet the glasses are not drawn on the image. I assumed that if I passed a reference of the image to detectAndAnimateFaces that the image would come back with the features drawn on it. This isn't the case though. Am I missing something? Thanks!

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: