123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #pragma execution_character_set("utf-8")
- #include "Httpserver.h"
- //#include "mongoose.c"
- #include <QCoreApplication>
- #include <QtCore/qthread.h>
- #include <iostream>
- #include <thread>
- #include <nlohmann/json.hpp>
- #include "JdydAlgorithnm.h"
- #include "YundaTecAlgorithnm.h"
- #include <QtCore/QTextStream>
- #include <QtCore/qfile.h>
- #include <QFileInfo>
- //#include "WriteLog.h"
- #include "YunDaISASImageRecognitionService.h"
- using json = nlohmann::json;
- //static std::map<std::string, AlgorithmModel*> _plugins;
- std::string HttpServer::dump_headers(const Headers& headers) {
- std::string s;
- char buf[BUFSIZ];
- for (auto it = headers.begin(); it != headers.end(); ++it) {
- const auto& x = *it;
- snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
- s += buf;
- }
- return s;
- }
- std::string HttpServer::log(const Request& req, const Response& res) {
- std::string s;
- char buf[BUFSIZ];
- s += "================================\n";
- snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
- req.version.c_str(), req.path.c_str());
- s += buf;
- std::string query;
- for (auto it = req.params.begin(); it != req.params.end(); ++it) {
- const auto& x = *it;
- snprintf(buf, sizeof(buf), "%c%s=%s",
- (it == req.params.begin()) ? '?' : '&', x.first.c_str(),
- x.second.c_str());
- query += buf;
- }
- snprintf(buf, sizeof(buf), "%s\n", query.c_str());
- s += buf;
- s += dump_headers(req.headers);
- s += "--------------------------------\n";
- snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
- s += buf;
- s += dump_headers(res.headers);
- s += "\n";
- if (!res.body.empty()) { s += res.body; }
- s += "\n";
- return s;
- }
- static std::mutex mtx; // 保护counter
- static int errorCount = 0;
- int HttpServer::RunHttpServer()
- {
- std::thread th([]{
- Server svr;
- if(!svr.is_valid()) {
- printf("server has an error...\n");
- return -1;
- }
- svr.Get("/", [=](const Request& /*req*/, Response& res) {
- res.set_redirect("/hi");
- });
- svr.Get("/hi", [](const Request& /*req*/, Response& res) {
- res.set_content( "Imgage Recognize Serive Running!\n", "text/plain");
- });
- svr.Post("/api/recognize", [](const Request& req, Response& res)
- {
- mtx.lock();
- try
- {
- json resultJson = {};
- json ex1 = json::parse(req.body);
- bool isNewAlgorithnm = false;
- if (ex1["isNewAlgorithnm"].is_boolean())
- {
- isNewAlgorithnm = ex1["isNewAlgorithnm"].get<bool>();
- }
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--isNewAlgorithnm:%1").arg(QString::number(isNewAlgorithnm)));
- string pluginName = "";
- if (ex1["pluginName"].is_string())
- {
- pluginName = ex1["pluginName"].get<string>();
- }
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--pluginName:%1").arg(QString::fromLocal8Bit(pluginName.c_str())));
- string filePath = "";
- if (ex1["filePath"].is_string())
- {
- filePath = UTF8string(ex1["filePath"].get<string>());
- }
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--filePath:%1").arg(QString::fromLocal8Bit(filePath.c_str())));
- string modelPath = "";
- if (ex1["modelPath"].is_string())
- {
- modelPath = ex1["modelPath"].get<string>();
- }
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--modelPath:%1").arg(QString::fromLocal8Bit(modelPath.c_str())));
- vector<int> roiVec;
- if (ex1["roi"].is_array())
- {
- roiVec = ex1["roi"].get<vector<int>>();
- }
- if (8 == roiVec.size())
- {
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--roi %1,%2,%3,%4,%5,%6,%7,%8").arg(QString::number(roiVec[0]) , QString::number(roiVec[1]), QString::number(roiVec[2]), QString::number(roiVec[3]), QString::number(roiVec[4]), QString::number(roiVec[5]), QString::number(roiVec[6]), QString::number(roiVec[7])));
- }
- else
- {
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg: roi error"));
- }
- int* roiArr = new int[sizeof(roiVec)];
- if (!roiVec.empty())
- {
- memcpy(roiArr, &roiVec[0], roiVec.size() * sizeof(int));
- }
- string pcArg = "";
- if (ex1["pcArg"].is_string())
- {
- pcArg = ex1["pcArg"].get<string>();
- }
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("recvMsg--pcArg:%1").arg(QString::fromLocal8Bit(pcArg.c_str())));
- if (!isNewAlgorithnm)
- {
- if (pluginName.size() > 0)
- {
- ifstream qfile(filePath);
- if (qfile.good())
- {
- resultJson = YundaTecAlgorithnm::CallAlgorithnm(pluginName, filePath, modelPath, roiArr, pcArg);
- }
- else
- {
- resultJson = { {"flag", -1}, {"result" , "识别文件未找到"} };
- }
- qfile.close();
- }
- else
- {
- resultJson = { {"flag", -1}, {"result" , "算法路径为空"} };
- }
- }
- else
- {
- if (8 == roiVec.size())
- {
- ifstream qfile(filePath);
- if (qfile.good())
- {
- resultJson = JdydAlgorithnm::CallAlgorithnm(pluginName, filePath, modelPath, roiArr, pcArg);
- }
- else
- {
- resultJson = { {"flag", -1}, {"result" , "识别文件未找到"} };
- }
- qfile.close();
- }
- }
- free(roiArr);
- std::string serialized_returnstring = resultJson.dump();
- //std::string serialized_requeststring = req.dump();
- YunDaISASImageRecognitionService::ConsoleLog(QStringLiteral("returnMsg:%1").arg(QString::fromLocal8Bit(serialized_returnstring.c_str())));
- res.set_content(serialized_returnstring, "application/json");
- }
- catch (const std::exception& ex)
- {
- YunDaISASImageRecognitionService::ConsoleLog(ex.what());
- /* errorCount++;
- if (errorCount>20)
- {
- exit(0);
- }*/
- }
- mtx.unlock();
- //return 0;
- });
- //svr.Get("/stop",
- // [&](const Request& /*req*/, Response& /*res*/) { svr.stop(); });
- svr.set_error_handler([](const Request& /*req*/, Response& res) {
- const char* fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
- char buf[BUFSIZ];
- snprintf(buf, sizeof(buf), fmt, res.status);
- res.set_content(buf, "text/html");
- });
- svr.set_logger([](const Request& req, const Response& res) {
- printf("%s", log(req, res).c_str());
- });
- svr.listen("0.0.0.0", 35689);
- return 0;
- });
-
- th.detach();
- return 0;
- }
- //void HttpServer::SaveLog(QString msg) {
- // //WriteLog::SaveLog(msg);
- //}
- string HttpServer::stringUTF8(const string& str)
- {
- int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
- wchar_t* pwBuf = new wchar_t[nwLen + 1];
- ZeroMemory(pwBuf, nwLen * 2 + 2);
- ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
- int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
- char* pBuf = new char[nLen + 1];
- ZeroMemory(pBuf, nLen + 1);
- ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
- std::string retStr(pBuf);
- delete[]pwBuf;
- delete[]pBuf;
- pwBuf = NULL;
- pBuf = NULL;
- return retStr;
- }
- string HttpServer::UTF8string(string strTemp)
- {
- char buf[1024 * 60];
- snprintf(buf, sizeof(buf), u8"%s", strTemp.c_str());
- TCHAR wscBuffer[1024 * 10] = { 0 };
- MultiByteToWideChar(CP_UTF8, 0, buf, (int)strlen(buf) + 1, wscBuffer, sizeof(wscBuffer) / sizeof(wchar_t));
- memset(buf, 0, 1024 * 9);
- WideCharToMultiByte(CP_ACP, 0, wscBuffer, -1, buf, 1024 * 9, NULL, NULL);
- return buf;
- }
- void HttpServer::fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data) {
-
- }
|