QT5视频抽针并上传(百度windows图像识别)
#pragma once #include <QObject> #include <QEventLoop> #include <opencv2/opencv.hpp> #include <opencv2/core/core.hpp> #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> #include <QHttpPart> #include <opencv2/imgproc/types_c.h> #include <opencv2/imgproc/imgproc_c.h> #include <opencv2/imgcodecs/legacy/constants_c.h> struct baiduData { QString label; int prob; }; class baiduapi : public QObject { Q_OBJECT public: baiduapi(); baiduData run(cv::Mat img); private: QString m_strServerAddr = "http://192.168.1.54:24401?threshold=0.15"; }; #include "baiduapi.h" #include <QUrlQuery> #include <QFile> #include <QUuid> #include <QJsonParseError> #include <QJsonObject> #include <QJsonArray> #include <QJsonValue> #include <QJsonDocument> #include <QDebug> #include <vector> baiduapi::baiduapi() {} baiduData baiduapi::run(cv::Mat img) { QNetworkAccessManager* m_pNetWorkManager = new QNetworkAccessManager(); baiduData info; if (m_strServerAddr.isEmpty()) { return info; } std::vector<uchar> buff; std::vector<int> param = std::vector<int>(2); param[0] = CV_IMWRITE_JPEG_QUALITY; param[1] = 95; cv::imencode(".jpg", img, buff, param); unsigned char* tem = &*buff.begin(); const char* temp1 = reinterpret_cast<const char*>(tem); QByteArray sb = QByteArray(temp1, buff.size()); QNetworkRequest request; request.setUrl(m_strServerAddr); request.setRawHeader(QString("Content-Type").toLatin1(), QString("application/octet-stream").toLatin1()); QNetworkReply* reply = m_pNetWorkManager->post(request, sb); QEventLoop eventLoop; connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit); eventLoop.exec(); /// 错误处理 if (reply->error() == QNetworkReply::NoError) { //qDebug() << "request protobufHttp NoError"; } else { qDebug() << "request protobufHttp handle errors here"; QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); //statusCodeV是HTTP服务器的相应码,reply->error()是Qt定义的错误码,可以参考QT的文档 qDebug("request protobufHttp found error ....code: %d %d\n", statusCodeV.toInt(), (int)reply->error()); //qDebug(qPrintable(reply->errorString())); } /// 请求返回的结果 QByteArray responseByte = reply->readAll(); QJsonParseError jsonpe; QJsonDocument json = QJsonDocument::fromJson(responseByte, &jsonpe); if (jsonpe.error == QJsonParseError::NoError) { if (json.isObject()) { QJsonObject obj = json.object(); if (obj.contains("results")) { QJsonObject::Iterator it; QJsonArray results = obj["results"].toArray(); if (results.count() > 0) { //qDebug() << obj["results"].toString(); qDebug() << QByteArray::fromPercentEncoding(responseByte).data(); } for (int i = 0;i< results.count();i++) { //将value转成QJsonArray或QJsonObject,继续遍历 QJsonObject item = results[i].toObject(); if (item.contains("label")&&item["label"] == "211") { QJsonValue conf = item["confidence"]; double probs = conf.toDouble()*100; info.prob = static_cast<int>(probs); info.label = item["label"].toString(); } } } } else { qDebug() << "error, shoud json object"; } } else { qDebug() << "error:" << jsonpe.errorString(); } return info; }
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。