文档较少,没有demo不知道如何使用
这个代码片段可以参考。。。有错误请指出
#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;
}

我把 release 编译好的库拿进来,不是很懂这样导入相关头文件为什么会出错
@nihui
用 mm 文件
非常感谢 @nihui 已在ios上成功跑起来了
我在ios上跑,会出现这种错误,fopen squeezenet_v1.1.param failed
fopen squeezenet_v1.1.bin failed 什么原因呢
@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]);
稀世精品,真的太赞了,服(大写)x100
@humf 我文件是可以读成功的,通过NSFileHandle check过,但是在net.load_parm这里挂了,你这里ncnn库是他们的release里那到的吗?这里有什么要注意的地方么?
https://github.com/dangbo/ncnn-mobile use ncnn in Android and iOS
我在接入的时候有报错,具体是const float* prob = out.data + out.cstep * j;报错信息是“Semantic Issue Group - Arithmetic on a pointer to void”。可以帮忙解答一下吗?
我在接入的时候有报错,具体是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];
}
大神你好,我在用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这种方式获取图像数据。
Most helpful comment
@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"];