Tencent/ncnn

HarmonyOS next系统ncnn无法识别图片

Opened this issue · 0 comments

报错信息

使用ncnn识别图片不成功,返回结果如下:
struct pres{
float x_y[4][2] ; // 四个顶点x,y坐标
float w; // 目标宽
float h; // 目标高
int vertex_id[4] = {0, 1, 2, 3}; // 四个顶点顺时针排序的索引
float s; // 目标得分
};
x_y内容为nan,s为-1

运行环境

harmonyOS next,mate60手机

复现步骤

1.加载AI数据模型

int ret = Net->load_param_mem(modelParamData.get());//加载param数据,结果返回为0
RawFile *modelBinFile = OH_ResourceManager_OpenRawFile(mgr, model_bin.c_str());
RawFileDescriptor descriptor;
bool isSuccess =  OH_ResourceManager_GetRawFileDescriptor(modelBinFile, descriptor);
if (isSuccess) {
    FILE *fp = nullptr;
    fp = fdopen(descriptor.fd, "rw");
    if (!fp) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RES, TAG, "============================= fp filed");
    } else {
        int ret = Net->load_model(fp);//加载model数据,结果返回为0
    }
} else {
    OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RES, TAG, "============================= filed");
}

2.opencv预处理图片

NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, args[0]); 
if (native == nullptr) { 
    return error; 
} 
struct OhosPixelMapInfos pixelMapInfos; 
if (OH_PixelMap_GetImageInfo(native, &pixelMapInfos) != IMAGE_RESULT_SUCCESS) { 
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "Test", "hhhhh%{public}d", -1); 
    return error; 
}
void *pixel; 
OH_PixelMap_AccessPixels(native, &pixel);
cv::Mat originMat(pixelMapInfos.height, pixelMapInfos.width, CV_8UC4, pixel); 
if (!originMat.data) { 
    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, TAG_P, "hhhhh%{public}d", -1); 
    return error; 
}
cvtColor(originMat, originMat, cv::COLOR_RGBA2RGB);
cv::copyMakeBorder(originMat, first_pad_image, 0, pd_h, 0, pd_w, cv::BORDER_CONSTANT, 0);
first_pad_height = (float)first_pad_image.rows, first_pad_width = (float)first_pad_image.cols;
// 对图像进行缩放
cv:: Mat r_rgb;
cv::resize(first_pad_image, r_rgb, cv::Size(img_size-2*pad_num, img_size-2*pad_num));
resize_height = (float)r_rgb.rows, resize_width = (float)r_rgb.cols;
// dsnt对边缘点效果不好,补0防止关键点处于边缘
cv::copyMakeBorder(r_rgb, second_pad_image, pad_num, pad_num, pad_num, pad_num, cv::BORDER_CONSTANT, 0);

3.调用ncnn::Mat::from_pixels处理图片数据

ncnn::Mat in = ncnn::Mat::from_pixels(second_pad_image.data, ncnn::Mat::PIXEL_RGB, second_pad_image.cols, 
second_pad_image.rows);
in.substract_mean_normalize(_mean_val, norm_val);
ncnn::Extractor ex = Net->create_extractor();
// nncn推理结果
ex.input("input:0", in);
ncnn::Mat out;
ex.extract("heats_map_regression/pred_keypoints/BiasAdd:0", out);
for (int q=0; q<out.c; q++)
{
    const float* ptr = out.channel(q);
    for (int y=0; y<out.h; y++)
    {
        for (int x=0; x<out.w; x++)
        {
           OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RES, TAG, "%{public}f ", ptr[x]);
        }
        ptr += out.w;
        OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RES, TAG, "\n");
    }
    OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RES, TAG, "--------------------------------------\n");
}

log日志打印的值为nan