Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 基于预训练caffe模型的图像分类_C++_Opencv_Visual C++_Caffe_Object Recognition - Fatal编程技术网

C++ 基于预训练caffe模型的图像分类

C++ 基于预训练caffe模型的图像分类,c++,opencv,visual-c++,caffe,object-recognition,C++,Opencv,Visual C++,Caffe,Object Recognition,我正在visual studio中做一个关于使用预先训练好的caffe模型进行图像分类的项目,openCV3.4.0,C++ 我面临着一些错误: readNet:Identifier未找到 blobFromImage:function不接受7个参数 我从你的电脑上复制了代码 请帮帮我,因为我是新手。提前谢谢 代码: const char* keys = "{ help h | | Print help message. }" "{ input i | | Path to inp

我正在visual studio中做一个关于使用预先训练好的caffe模型进行图像分类的项目,openCV3.4.0C++

我面临着一些错误:

  • readNet:Identifier
    未找到
  • blobFromImage:function
    不接受7个参数
  • 我从你的电脑上复制了代码

    请帮帮我,因为我是新手。提前谢谢

    代码:

    const char* keys =
    "{ help  h     | | Print help message. }"
    "{ input i     | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
    "{ model m     | | Path to a binary file of model contains trained weights. "
    "It could be a file with extensions .caffemodel (Caffe), "
    ".pb (TensorFlow), .t7 or .net (Torch), .weights (Darknet) }"
    "{ config c    | | Path to a text file of model contains network configuration. "
    "It could be a file with extensions .prototxt (Caffe), .pbtxt (TensorFlow), .cfg (Darknet) }"
    "{ framework f | | Optional name of an origin framework of the model. Detect it automatically if it does not set. }"
    "{ classes     | | Optional path to a text file with names of classes. }"
    "{ mean        | | Preprocess input image by subtracting mean values. Mean values should be in BGR order and delimited by spaces. }"
    "{ scale       | 1 | Preprocess input image by multiplying on a scale factor. }"
    "{ width       |   | Preprocess input image by resizing to a specific width. }"
    "{ height      |   | Preprocess input image by resizing to a specific height. }"
    "{ rgb         |   | Indicate that model works with RGB input images instead BGR ones. }"
    "{ backend     | 0 | Choose one of computation backends: "
    "0: default C++ backend, "
    "1: Halide language (http://halide-lang.org/), "
    "2: Intel's Deep Learning Inference Engine (https://software.seek.intel.com/deep-learning-deployment)}"
    "{ target      | 0 | Choose one of target computation devices: "
    "0: CPU target (by default),"
    "1: OpenCL }";
    
    
    using namespace cv;
    using namespace dnn;
    std::vector<std::string> classes;
    
    
    int main(int argc, char** argv)
    {
        CommandLineParser parser(argc, argv, keys);
        parser.about("Use this script to run classification deep learning networks using OpenCV.");
        if (argc == 1 || parser.has("help"))
        {
            parser.printMessage();
            return 0;
        }
        float scale = parser.get<float>("scale");
        Scalar mean = parser.get<Scalar>("mean");
        bool swapRB = parser.get<bool>("rgb");
        CV_Assert(parser.has("width"), parser.has("height"));
        int inpWidth = parser.get<int>("width");
        int inpHeight = parser.get<int>("height");
        String model = parser.get<String>("model");
        String config = parser.get<String>("config");
        String framework = parser.get<String>("framework");
        int backendId = parser.get<int>("backend");
        int targetId = parser.get<int>("target");
        // Open file with classes names.
        if (parser.has("classes"))
        {
            std::string file = parser.get<String>("classes");
            std::ifstream ifs(file.c_str());
            if (!ifs.is_open())
                CV_Error(Error::StsError, "File " + file + " not found");
            std::string line;
            while (std::getline(ifs, line))
            {
                classes.push_back(line);
            }
        }
        CV_Assert(parser.has("model"));
        Net net = readNet(model, config, framework);
        net.setPreferableBackend(backendId);
        net.setPreferableTarget(targetId);
        // Create a window
        static const std::string kWinName = "Deep learning image classification in OpenCV";
        namedWindow(kWinName, WINDOW_NORMAL);
        VideoCapture cap;
        if (parser.has("input"))
            cap.open(parser.get<String>("input"));
        else
            cap.open(0);
        // Process frames.
        Mat frame, blob;
        while (waitKey(1) < 0)
        {
            cap >> frame;
            if (frame.empty())
            {
                waitKey();
                break;
            }
            blobFromImage(frame, blob, scale, Size(inpWidth, inpHeight), mean, swapRB, false);
            net.setInput(blob);
            Mat prob = net.forward();
            Point classIdPoint;
            double confidence;
            minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
            int classId = classIdPoint.x;
            // Put efficiency information.
            std::vector<double> layersTimes;
            double freq = getTickFrequency() / 1000;
            double t = net.getPerfProfile(layersTimes) / freq;
            std::string label = format("Inference time: %.2f ms", t);
            putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
            // Print predicted class.
            label = format("%s: %.4f", (classes.empty() ? format("Class #%d", classId).c_str() :
                classes[classId].c_str()),
                confidence);
            putText(frame, label, Point(0, 40), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
            imshow(kWinName, frame);
        }
        return 0;
    }
    
    const char*键=
    “{help h | |打印帮助消息。}”
    “{input i | |输入图像或视频文件的路径。跳过此参数可从相机捕获帧。}”
    {model m | |模型二进制文件的路径包含经过训练的权重
    它可能是一个扩展名为.caffemodel(Caffe)的文件
    .pb(TensorFlow)、.t7或.net(Torch)、.weights(Darknet)}
    {config c | |模型的文本文件的路径包含网络配置
    它可以是扩展名为.prototxt(Caffe)、.pbtxt(TensorFlow)、.cfg(Darknet)}的文件
    “{framework f | |模型的原始框架的可选名称。如果未设置,则自动检测它。}”
    “{classes | |具有类名称的文本文件的可选路径。}”
    “{mean | |通过减去平均值预处理输入图像。平均值应按BGR顺序并用空格分隔。}”
    “{scale | 1 |通过乘以比例因子对输入图像进行预处理。}”
    “{width | |通过调整到特定宽度来预处理输入图像。}”
    “{height | |通过调整到特定高度来预处理输入图像。}”
    “{rgb | |表示模型使用rgb输入图像而不是BGR图像。}”
    {后端| 0 |选择一个计算后端:
    “0:默认C++后端”
    “1:卤化物语言(http://halide-lang.org/), "
    “2:英特尔的深度学习推理机(https://software.seek.intel.com/deep-learning-deployment)}"
    {target | 0 |选择一个目标计算设备:
    0:CPU目标(默认情况下)
    “1:OpenCL}”;
    使用名称空间cv;
    使用名称空间dnn;
    std::向量类;
    int main(int argc,字符**argv)
    {
    CommandLineParser解析器(argc、argv、键);
    about(“使用此脚本使用OpenCV运行分类深度学习网络”);
    if(argc==1 | | parser.has(“help”))
    {
    parser.printMessage();
    返回0;
    }
    float scale=parser.get(“scale”);
    标量平均值=parser.get(“平均值”);
    boolswaprb=parser.get(“rgb”);
    CV_断言(parser.has(“宽度”)、parser.has(“高度”);
    int inpWidth=parser.get(“宽度”);
    int inpHeight=parser.get(“高度”);
    字符串模型=parser.get(“模型”);
    String config=parser.get(“config”);
    stringframework=parser.get(“framework”);
    int backendId=parser.get(“backend”);
    int targetId=parser.get(“target”);
    //打开具有类名称的文件。
    if(parser.has(“类”))
    {
    std::string file=parser.get(“类”);
    std::ifstream ifs(file.c_str());
    如果(!ifs.is_open())
    CV_错误(错误::stror,“文件”+文件+“未找到”);
    std::字符串行;
    while(std::getline(ifs,line))
    {
    类。推回(行);
    }
    }
    CV_断言(parser.has(“model”);
    Net=readNet(模型、配置、框架);
    net.setPreferableBackend(backendId);
    net.setPreferableTarget(targetId);
    //创建一个窗口
    static const std::string kWinName=“OpenCV中的深度学习图像分类”;
    namedWindow(kWinName,窗口正常);
    视频捕捉帽;
    if(parser.has(“输入”))
    cap.open(parser.get(“input”);
    其他的
    上限开放(0);
    //处理框架。
    垫架,水滴;
    while(等待键(1)<0)
    {
    cap>>框架;
    if(frame.empty())
    {
    waitKey();
    打破
    }
    blobFromImage(帧、blob、比例、大小(inpWidth、inpHeight)、平均值、swapRB、false);
    net.setInput(blob);
    Mat prob=net.forward();
    点分类点;
    双重信心;
    minMaxLoc(概率重塑(1,1),0和置信度,0和分类点);
    int classId=classIdPoint.x;
    //把效率信息放进去。
    std::向量分层时间;
    double freq=getTickFrequency()/1000;
    double t=net.getPerfProfile(layersTimes)/freq;
    std::string label=格式(“推断时间:%.2f ms”,t);
    putText(帧、标签、点(0,15)、字体(HERSHEY)单纯形、0.5、标量(0,255,0));
    //打印预测类。
    label=format(“%s:%.4f)”,(classes.empty()?format(“Class#%d”,classId)。c#u str():
    类[classId].c_str()),
    信心);
    putText(帧、标签、点(0,40)、字体(HERSHEY)单纯形、0.5、标量(0,255,0));
    imshow(kWinName,frame);
    }
    返回0;
    }
    
    您复制的代码指的是开发分支3.4.1-dev,与您正在使用的版本(3.4.0)相比,该分支有相当大的差异

    根据文档,只有一次方法readNet不可用(因此出现错误)


    升级到branch 3.4.1-dev或使用为您的版本提供的示例。

    您复制的代码指的是开发分支3.4.1-dev,与您使用的版本(3.4.0)相比,该分支有相当大的差异

    根据文档,只有一次方法readNet不可用(因此出现错误)


    升级到branch 3.4.1-dev或使用为您的版本提供的示例。

    在机器学习中,使用经过训练的模型通常称为推理。在机器学习中,使用经过训练的模型通常称为推理。