目标检测代码的总体梳理

1、首先要给目标检测对象的模型推理分配推理的acl资源,内存。 

2、获取命令行参数中输入的路径下的所有图片,这里通过GetAllFiles函数获取到的是所有的图片文件的目录。

3、通过ReadImageFile来获取每张图片的宽、高、通道数。使用binFile函数以二进制格式打开图片文件。移动文件指针获取文件的len,将整个二进制文件拷贝到一个数组中,使用acldvpp接口读这个数组便可获取到图片的宽、高、通道数。

4、将获取的图片原始数据的宽高等属性拷贝到设备侧,并在设备侧使用Dvpp将图片先转化为yuv格式,再resize图片的大小到推理所需要的大小。此处需要将保存图片二进制数据的数组保存到一个共享指针中以供后续使用,还需要将其数组长度也保存下来。

5、将预处理的图片送入模型推理,获取推理结果。具体推理过程后续会更新在后续博客中。

6、最后推理结束之后,需要解析推理输出,并将推理得到的物体类别和位置标记到图片上。

int main(int argc, char *argv[]) {
    //检查应用程序执行时的输入,程序执行要求输入图片目录参数
    if((argc < 2) || (argv[1] == nullptr)){
        ERROR_LOG("Please input: ./main ");
        return FAILED;
    }
    //实例化目标检测对象,参数为分类模型路径,模型输入要求的宽和高
    ObjectDetect detect(kModelPath, kModelWidth, kModelHeight);
    //初始化分类推理的acl资源, 模型和内存
    Result ret = detect.Init();
    if (ret != SUCCESS) {
        ERROR_LOG("Classification Init resource failed");
        return FAILED;
    }

    //获取图片目录下所有的图片文件名
    string inputImageDir = string(argv[1]);
    vector fileVec;
    Utils::GetAllFiles(inputImageDir, fileVec);
    if (fileVec.empty()) {
        ERROR_LOG("Failed to deal all empty path=%s.", inputImageDir.c_str());
        return FAILED;
    }

    //逐张图片推理
    ImageData image;
    for (string imageFile : fileVec) {
        Utils::ReadImageFile(image, imageFile);                //read H/W/N of pic
        if (image.data == nullptr) {
            ERROR_LOG("Read image %s failed", imageFile.c_str());
            return FAILED;
        }

        //预处理图片:读取图片,讲图片缩放到模型输入要求的尺寸
        ImageData resizedImage;
        Result ret = detect.Preprocess(resizedImage, image);
        if (ret != SUCCESS) {
            ERROR_LOG("Read file %s failed, continue to read next",
                      imageFile.c_str());                
            continue;
        }
        //将预处理的图片送入模型推理,并获取推理结果
        aclmdlDataset* inferenceOutput = nullptr;
        ret = detect.Inference(inferenceOutput, resizedImage);
        if ((ret != SUCCESS) || (inferenceOutput == nullptr)) {
            ERROR_LOG("Inference model inference output data failed");
            return FAILED;
        }
        //解析推理输出,并将推理得到的物体类别和位置标记到图片上
        ret = detect.Postprocess(image, inferenceOutput, imageFile);
        if (ret != SUCCESS) {
            ERROR_LOG("Process model inference output data failed");
            return FAILED;
        }
    }

    INFO_LOG("Execute sample success");
    return SUCCESS;
}

 

 

你可能感兴趣的:(神经网络)