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

up主可以出一个ios demo吗 #24

Closed
humf opened this issue Jul 25, 2017 · 12 comments
Closed

up主可以出一个ios demo吗 #24

humf opened this issue Jul 25, 2017 · 12 comments

Comments

@humf
Copy link

humf commented Jul 25, 2017

文档较少,没有demo不知道如何使用

@nihui
Copy link
Member

nihui commented Jul 25, 2017

这个代码片段可以参考。。。有错误请指出

#include <algorithm>
#include <functional>
#include <vector>

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIImage.h>

// ncnn
#include "net.h"

static int print_topk(const std::vector<float>& cls_scores, int topk)
{
    // partial sort topk with index
    int size = cls_scores.size();
    std::vector< std::pair<float, int> > vec;
    vec.resize(size);
    for (int i=0; i<size; i++)
    {
        vec[i] = std::make_pair(cls_scores[i], i);
    }

    std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
                      std::greater< std::pair<float, int> >());

    // print topk and score
    for (int i=0; i<topk; i++)
    {
        float score = vec[i].first;
        int index = vec[i].second;
        fprintf(stderr, "%d = %f\n", index, score);
    }

    return 0;
}

int main(int argc, char** argv)
{
    const char* imagepath = argv[1];

    // load image
    UIImage* image = 0;
    {
        NSString* imagePath = [NSString stringWithUTF8String:imagepath];
        image = [UIImage imageWithContentsOfFile:imagePath];
    }

    // get rgba pixels from image
    int w = image.size.width;
    int h = image.size.height;
    fprintf(stderr, "%d x %d\n", w, h);
    unsigned char* rgba = new unsigned char[w*h*4];
    {
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
        CGContextRef contextRef = CGBitmapContextCreate(rgba, w, h, 8, w*4,
                                                        colorSpace,
                                                        kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault);

        CGContextDrawImage(contextRef, CGRectMake(0, 0, w, h), image.CGImage);
        CGContextRelease(contextRef);
    }

    // init net
    ncnn::Net net;
    {
        int r0 = net.load_param("squeezenet_v1.1.param");
        int r1 = net.load_model("squeezenet_v1.1.bin");
        fprintf(stderr, "net load %d %d\n", r0, r1);
    }

    // run forward
    ncnn::Mat out;
    {
        ncnn::Extractor ex = net.create_extractor();
        ex.set_light_mode(true);
        ex.set_num_threads(2);

        ncnn::Mat in = ncnn::Mat::from_pixels_resize(rgba, ncnn::Mat::PIXEL_RGBA2BGR, w, h, 227, 227);
        
        const float mean_vals[3] = {104.f, 117.f, 123.f};
        in.substract_mean_normalize(mean_vals, 0);

        ex.input("data", in);

        ex.extract("prob", out);
    }

    // get prob
    std::vector<float> cls_scores;
    {
        cls_scores.resize(out.c);
        for (int j=0; j<out.c; j++)
        {
            const float* prob = out.data + out.cstep * j;
            cls_scores[j] = prob[0];
        }
    }

    // print topk
    print_topk(cls_scores, 3);

    // clean
    delete[] rgba;

    return 0;
}

@SeanChense
Copy link

2017-07-25 4 07 53

我把 release 编译好的库拿进来,不是很懂这样导入相关头文件为什么会出错 @nihui

@nihui
Copy link
Member

nihui commented Jul 25, 2017

用 mm 文件

@humf
Copy link
Author

humf commented Jul 25, 2017

非常感谢 @nihui 已在ios上成功跑起来了

@humf humf closed this as completed Jul 25, 2017
@caochunyuan
Copy link

我在ios上跑,会出现这种错误,fopen squeezenet_v1.1.param failed
fopen squeezenet_v1.1.bin failed 什么原因呢

@humf
Copy link
Author

humf commented Jul 26, 2017

@caochunyuan 路径没有找到,首先要确定squeezenet_v1.1.bin文件引入到工程中
NSString *paramPath = [[NSBundle mainBundle] pathForResource:@"squeezenet_v1.1" ofType:@"param"];
NSString *binPath = [[NSBundle mainBundle] pathForResource:@"squeezenet_v1.1" ofType:@"bin"];

    int r0 = net.load_param([paramPath UTF8String]);
    int r1 = net.load_model([binPath UTF8String]);

@venus024
Copy link

稀世精品,真的太赞了,服(大写)x100

@nihui nihui mentioned this issue Jul 27, 2017
@brightzhengh
Copy link

@humf 我文件是可以读成功的,通过NSFileHandle check过,但是在net.load_parm这里挂了,你这里ncnn库是他们的release里那到的吗?这里有什么要注意的地方么?

@dangbo
Copy link

dangbo commented Jul 29, 2017

https://github.com/dangbo/ncnn-mobile use ncnn in Android and iOS

This was referenced Oct 15, 2018
@Joke-Wang
Copy link

我在接入的时候有报错,具体是const float* prob = out.data + out.cstep * j;报错信息是“Semantic Issue Group - Arithmetic on a pointer to void”。可以帮忙解答一下吗?

@nihui
Copy link
Member

nihui commented Mar 6, 2020

我在接入的时候有报错,具体是const float* prob = out.data + out.cstep * j;报错信息是“Semantic Issue Group - Arithmetic on a pointer to void”。可以帮忙解答一下吗?

        for (int j=0; j<out.c; j++)
        {
            cls_scores[j] = out[j];
        }

@hsl20130659
Copy link

大神你好,我在用ncnn做人脸检测,最近遇到一个问题,我用UIimage加载iPhone拍的图片,用你的这种方式获取图像数据
unsigned char* rgba = new unsigned char[wh4];
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGContextRef contextRef = CGBitmapContextCreate(rgba, w, h, 8, w*4,
colorSpace,
kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault);

    CGContextDrawImage(contextRef, CGRectMake(0, 0, w, h), image.CGImage);
    CGContextRelease(contextRef);
}

处理结果不对,人脸框是错的。
用cv::Mat imageMat;
UIImageToMat(image,imageMat);
cv::transpose(imageMat, imageMat);
cv::flip(imageMat,imageMat,1);
rgba = imageMat.data
处理结果才是对的,这是为什么呢?大神有遇到过这个问题吗?大神有空帮忙解答一下哈,我是不想借助opencv这种方式获取图像数据。

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

9 participants