Giter VIP home page Giter VIP logo

Comments (12)

humf avatar humf commented on May 12, 2024 2

@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]);

from ncnn.

nihui avatar nihui commented on May 12, 2024 1

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

#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;
}

from ncnn.

brightzhengh avatar brightzhengh commented on May 12, 2024 1

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

from ncnn.

SeanChense avatar SeanChense commented on May 12, 2024

2017-07-25 4 07 53

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

from ncnn.

nihui avatar nihui commented on May 12, 2024

用 mm 文件

from ncnn.

humf avatar humf commented on May 12, 2024

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

from ncnn.

caochunyuan avatar caochunyuan commented on May 12, 2024

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

from ncnn.

venus024 avatar venus024 commented on May 12, 2024

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

from ncnn.

dangbo avatar dangbo commented on May 12, 2024

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

from ncnn.

Joke-Wang avatar Joke-Wang commented on May 12, 2024

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

from ncnn.

nihui avatar nihui commented on May 12, 2024

我在接入的时候有报错,具体是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];
        }

from ncnn.

hsl20130659 avatar hsl20130659 commented on May 12, 2024

大神你好,我在用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这种方式获取图像数据。

from ncnn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.