tensorflow 示例

void MainWindow::tensorflow()
{
    qDebug()<<"startAutoRecognize";

    QStringList classes;
    float confThreshold = 0.8;
    float scale = 1;
    Scalar mean = { 0, 0, 0 };
    bool swapRB = true;
    int inpWidth = 200;
    int inpHeight = 200;

    QString strPath = QCoreApplication::applicationDirPath();
    String modelPath = strPath.toStdString()+"/frozen_inference_graph.pb";
    String configPath = strPath.toStdString()+"/ssdlite.pbtxt";
    String framework = "";

    const char* classNames[] = {"苹果","白象多半桶","4元薯愿","8元薯愿","农夫山泉小瓶" };
    int backendId = cv::dnn::DNN_BACKEND_OPENCV;
    int targetId = cv::dnn::DNN_TARGET_CPU;

    String classesFile = strPath.toStdString()+"/predefined_classes.txt";

    // Open file with classes names.
    if (!classesFile.empty()) {
        const std::string& file = classesFile;
        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(QString::fromStdString(line));
        }
    }

    // Load a model.
    Net net = readNet(modelPath, configPath, framework);
    net.setPreferableBackend(backendId);
    net.setPreferableTarget(targetId);

    std::vector<String> outNames = net.getUnconnectedOutLayersNames();
    Mat frame, blob;

    cv::VideoCapture capture;
    if(!capture.open(0))
    {
        qDebug()<<"capture open error!";
    }

    while(1)
    {
        capture >> frame;
        if(frame.empty())
        {
            qDebug()<<"frame is empty";
        }

        //QImage img = Mat2QImage(frame);
        //emit sendFruitImageInfo(img);
        //Create a 4D blob from a frame.
        Size inpSize(inpWidth > 0 ? inpWidth : frame.cols,
                     inpHeight > 0 ? inpHeight : frame.rows);

        blobFromImage(frame, blob, scale, inpSize, mean, swapRB, false);

        // Run a model.
        net.setInput(blob);
//        if (net.getLayer(0)->outputNameToIndex("im_info") != -1)  // Faster-RCNN or R-FCN
//        {
//            cv::resize(frame, frame, inpSize);
//            Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
//            net.setInput(imInfo, "im_info");
//        }

        std::vector<Mat> outs;
        net.forward(outs, outNames);

        std::vector<int> classIds;
        CV_Assert(outs.size() == 1);
        float* data = (float*)outs[0].data;

        for (size_t i = 0; i < outs[0].total(); i += 7) {
            float confidence = data[i + 2];
            if (confidence > confThreshold)
            {
                classIds.push_back((int)(data[i + 1]) - 1);  // Skip 0th background class id.
                qDebug() << "类别:" << QString::number((int)(data[i + 1]) - 1) << "confidence:" << confidence;
                QString strDisplay = (classes[(int)(data[i + 1]) - 1]);
                qDebug()<<"strDisplay:"<<strDisplay;
            }
        }
        imshow("image",frame);
        waitKey(30);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值