Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔥🔥🔥 [Guide] How to use this lib in iOS #90

Closed
Yak0xff opened this issue Mar 15, 2019 · 10 comments
Closed

🔥🔥🔥 [Guide] How to use this lib in iOS #90

Yak0xff opened this issue Mar 15, 2019 · 10 comments

Comments

@Yak0xff
Copy link

Yak0xff commented Mar 15, 2019

This lib is very useful! I try it in iOS and successful run.

  1. download or clone this lib in your computer;
  2. create New Xcode project;
  3. add this lib's src to your project;
  4. add the system lib libc++.tbd and other you need framework(eg. ACFoundation.framework etc.);
  5. download opencv2.framework and add it in your project;
  6. follow the example file to write the code.

!!!

  1. modify facedetectcnn.h
//#define _ENABLE_AVX2 //Please enable it if X64 CPU
#define _ENABLE_NEON //Please enable it if ARM CPU
  1. modify .m to .mm
  2. import lib in your .mm
#import <opencv2/opencv.hpp>
#import <opencv2/imgcodecs/ios.h>
#import "ViewController.h"
#import "facedetectcnn.h"

you must import the opencv2/opencv.hpp first !!!

MyCode:

#import <opencv2/opencv.hpp>
#import <opencv2/imgcodecs/ios.h>
#import "ViewController.h"
#import "facedetectcnn.h"

//define the buffer size. Do not change the size!
#define DETECT_BUFFER_SIZE 0x20000
using namespace cv;

@implementation ViewController

- (UIImage *)loadImageAndDectect:(const char *)image_file{
    Mat img = imread(image_file);
    if (img.empty()) {
        fprintf(stderr, "Can not load the image file %s.\n", image_file);
        return nil;
    }
    
    int *pResults = NULL;
    unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE);
    if (!pBuffer) {
        fprintf(stderr, "Can not alloc buffer.\n");
        return nil;
    }
    pResults = facedetect_cnn(pBuffer, (unsigned char *)(img.ptr(0)), img.cols, img.rows, (int)img.step);
    printf("%d faces detected.\n", (pResults ? *pResults : 0));
    Mat result_cnn = img.clone();;
    //print the detection results
    for(int i = 0; i < (pResults ? *pResults : 0); i++)
    {
        short * p = ((short*)(pResults+1))+142*i;
        int x = p[0];
        int y = p[1];
        int w = p[2];
        int h = p[3];
        int neighbors = p[4];
        int angle = p[5];
        
        printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
        rectangle(result_cnn, cv::Rect(x, y, w, h), Scalar(0, 255, 0), 2);
    }
    
    free(pBuffer);
    
    return MatToUIImage(result_cnn);
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:imageView];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@".jpg"];
    
    imageView.image = [self loadImageAndDectect:[path UTF8String]];
}


@end

IMG_0428
IMG_0429
IMG_0430

@Yak0xff Yak0xff changed the title How to use this lib in iOS 🙈[Guide] How to use this lib in iOS Mar 15, 2019
@Yak0xff Yak0xff changed the title 🙈[Guide] How to use this lib in iOS 🔥🔥🔥 [Guide] How to use this lib in iOS Mar 15, 2019
@ShiqiYu
Copy link
Owner

ShiqiYu commented Mar 15, 2019 via email

@Yak0xff Yak0xff closed this as completed Mar 21, 2019
@ewen1024
Copy link

@RobinChao Do you have the running time on IOS. I tested it on iphone 6s and iphone x, much slower than the raspberry platform

@Yak0xff
Copy link
Author

Yak0xff commented Apr 25, 2019

@ewen1024 Sorry, i didn't have the running time. Did you input huge image to NN, if you resize the image or use small size image, maybe running faster a little. Thanks.

@ewen1024
Copy link

Thx for your reply.
By enabling neon int8 and optimization -o3, I already got the running time in 100ms.
But I failed to enable openmp on ios which said the library only supports x86. Do you have any idea on this?\

@ShiqiYu
Copy link
Owner

ShiqiYu commented Apr 25, 2019 via email

@ShiqiYu
Copy link
Owner

ShiqiYu commented Apr 25, 2019 via email

@ewen1024
Copy link

@ShiqiYu On ios platform, it seems the compiler clang doesn't support openmp.
So I can't compile the code with -fopenmp. Maybe I need to find a way to add support of openmp for the clang

@ShiqiYu
Copy link
Owner

ShiqiYu commented Apr 25, 2019 via email

@ewen1024
Copy link

@ShiqiYu appreciate for your help!

@zpfate
Copy link

zpfate commented Jun 3, 2019

请问一下为什么在ios上图片变色了, 或者使用opencv的ios的函数UIImageToMat, 无法识别出人脸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants