From 8ece1bcf75056d8006e2d56721d1a6498e7d273c Mon Sep 17 00:00:00 2001 From: admin <zzjincn@163.com> Date: 星期二, 29 四月 2025 15:09:55 +0800 Subject: [PATCH] 写入redis处理加上失效ttl --- PreProcessFn.cpp | 476 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 362 insertions(+), 114 deletions(-) diff --git a/PreProcessFn.cpp b/PreProcessFn.cpp index 04e3fb7..3c40632 100644 --- a/PreProcessFn.cpp +++ b/PreProcessFn.cpp @@ -9,7 +9,7 @@ #include <mysql/mysql.h> #include <future> #include "iostream" - +#include <hiredis.h> #include <string> #include <locale> #include <codecvt> @@ -255,7 +255,7 @@ //cout << current_time << endl; // 濡傛灉褰曞埗鏃堕棿瓒呰繃 10 绉掞紝鍒欏仠姝㈠綍鍒� if (seconds >= _recordtime) { - cout << "宸插綍鍒� 10 绉掕棰戯紝鍋滄褰曞埗" << endl; + std::cout << "宸插綍鍒� 10 绉掕棰戯紝鍋滄褰曞埗" << endl; video_writer.release(); } else @@ -284,7 +284,7 @@ if (mysql_query(mysql, query.c_str())) { string err_string = mysql_error(mysql); - cout << err_string << endl; + std::cout << err_string << endl; } } } @@ -321,7 +321,7 @@ if (mysql_query(mysql, query.c_str())) { string err_string = mysql_error(mysql); - cout << err_string << endl; + std::cout << err_string << endl; } } } @@ -330,6 +330,38 @@ size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) { ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; +} + +string join(const vector<string>& sequence, const string& separator) +{ + std::string result; + for (size_t i = 0; i < sequence.size(); ++i) + result += sequence[i] + ((i != sequence.size() - 1) ? separator : ""); + return result; +} + +/// <summary> +/// 鍐欏叆鏁版嵁鑷硆edis妯″潡 +/// </summary> +void fnWriteToRedis(redisContext*& context, string ipccode) +{ + redisReply* reply; + + //妫�娴媟edis鏄惁杩炴帴锛屾姤璀﹀啓鍏edis + if (!context == NULL && !context->err) { + + // 璁板綍鎶ヨ鐨勬椂鍊欙紝鍚屾椂鍐欏叆redis鏁版嵁搴� + std::string key = "camera::run::" + ipccode; + std::string value = "1"; + + //鍐欏叆redis + reply = (redisReply*)redisCommand(context, "SET %s %s EX %d", key.c_str(), value.c_str(),3); + if (reply == NULL) { + printf("淇℃伅鍐欏叆redis澶辫触: %s\n", context->errstr); + redisFree(context); + } + freeReplyObject(reply); + } } /// @brief 瑙嗛娴佹媺鍙栧鐞� @@ -342,7 +374,7 @@ int count = 0; long currentFrame = 1; - long skip_frame_num = deley;//璺冲抚闂撮殧璁剧疆锛堟瘡1甯э級 + long skip_frame_num = skipN;//璺冲抚闂撮殧璁剧疆锛堟瘡1甯э級 while (!asyncStop) { @@ -455,6 +487,65 @@ rtspStream.release(); } +/// <summary> +/// 杩炴帴骞舵牎楠� Redis 鏈嶅姟鍣ㄥ瘑鐮� +/// </summary> +/// <param name="server"></param> +/// <param name="port"></param> +/// <param name="password"></param> +/// <param name="context"></param> +/// <returns></returns> +bool connectToRedis(const char* server, int port, const char* password, redisContext*& context) { + // 杩炴帴 Redis 鏈嶅姟鍣� + context = redisConnect(server, port); + if (context == nullptr || context->err) { + std::cerr << "杩炴帴redis鏈嶅姟绔け璐ワ紒" << std::endl; + return false; + } + + //// 鏍¢獙瀵嗙爜 + //redisReply* reply = (redisReply*)redisCommand(context, "AUTH %s", password); + //if (reply == NULL) { + // std::cerr << "杩炴帴鏍¢獙澶辫触锛�" << std::endl; + // redisFree(context); + // return false; + //} + //else { + // // 妫�鏌ュ洖澶嶄互纭畾鏄惁閴存潈鎴愬姛 + // if (reply->type == REDIS_REPLY_ERROR && strcmp(reply->str, "OK") != 0) { + // std::cerr << "瀵嗙爜閿欒: " << reply->str << std::endl; + // freeReplyObject(reply); + // redisFree(context); + // return false; + // } + // freeReplyObject(reply); + //} + + return true; +} + +//json杞崲瀛楃涓� +std::string jsontostr(Json::Value& json) +{ + string return_str; + if(!json.isNull()&&!json.empty()) + { + for (int i = 0; i < json.size(); i++) + { + return_str += json[i].asString()+","; + } + if (!return_str.empty() && return_str.back() == ',') { + return_str.pop_back(); + } + + } + else + { + return_str = ""; + } + return return_str; +} + /// @brief 瑙嗛甯у鐞嗗嚱鏁� /// @param ipcsNode 鎽勫儚鏈鸿妭鐐� /// @param modelArray 妯″瀷鍒楄〃 @@ -462,10 +553,11 @@ /// @param queJC2 澶勭悊瀹岀殑瑙嗛娴佸抚闃熷垪 void PreProcessFn::fnImRecognitionPro(const Json::Value& ipcsNode, Json::Value modelArray, queue<Mat>& queJC, queue<Mat>& queJC2) { - std::string ipccode = ipcsNode["ipccode"].asString();//鎽勫儚鏈虹紪鐮� + std::string ipccode = ipcsNode["code"].asString();//鎽勫儚鏈虹紪鐮� std::string ipcname = ipcsNode["name"].asString();//鎽勫儚鏈哄悕绉� std::string ipcrtsppath = ipcsNode["video"].asString();//瑙嗛rtsp婧� - + // std::string skipN = ipcsNode["skipN"].asString();//璺冲抚 + // int skipN = ipcsNode["skipN"].asString();//璺冲抚 //mysql杩炴帴鍒濆鍖� MYSQL* mysql = mysql_init(nullptr); if (!mysql_real_connect(mysql, mysqlpath.c_str(), mysqluser.c_str(), mysqlpass.c_str(), mysqldatabase.c_str(), stoi(mysqlport), NULL, 0)) @@ -480,6 +572,18 @@ mysql_set_character_set(mysql, "utf8mb4"); //mysql_query(mysql, "SET NAMES GB2312");//瑙e喅涓枃涔辩爜闂 } + + + //鍒濆鍖杛edis閾炬帴 + redisContext* context; + if (!connectToRedis(redispath.c_str(), stoi(redisport), redispass.c_str(), context)) + { + cout << "redis杩炴帴鍒濆鍖栧け璐ワ紒" << endl; + } + else + { + cout << "redis杩炴帴鍒濆鍖栨垚鍔燂紒" << endl; + } std::string limitDM = "1";//鍫嗙叅闄愬畾闃堝�硷紝瓒呰繃闃堝�艰繘琛屾姤璀︼紝閰嶇疆鏂囦欢璧嬪�硷紝姣忎釜鎽勫儚澶撮兘涓嶄竴鏍� @@ -497,6 +601,14 @@ std::vector<Point> areaDM;//鍫嗙叅妫�娴嬪尯鍩� std::vector<Point> areaWear;//绌挎埓妫�娴嬪尯鍩� std::vector<Point> areaFire;//鐑熺伀妫�娴嬪尯鍩� + std::vector<Point> detectarea;//妫�娴嬪尯鍩� + std::vector<Point> flowarea;//鐨甫澶栧洿鍖哄煙 + std::vector<Point> flowrelate;//鐨甫鐩稿鍖哄煙 + std::vector<Point> leftarea;//宸︿晶鍖哄煙 + std::vector<Point> rightarea;//鍙充晶鍖哄煙 + std::vector<Point> beltarea;//涓棿鍖哄煙 + std::vector<Point> workarea;//宸ヤ綔鍖哄煙 + //妯″瀷澶勭悊 ------绗�2姝� RUN rBelt;//鐨甫杩愯妯″瀷 @@ -626,31 +738,70 @@ //鎻愬崌鏈哄拰鐨甫鍒ゆ柇鏃讹紝闇�瑕佺疮璁′笁寮犲浘鐗囨墠鑳借繘琛屽垽鏂� std::vector<cv::Mat> imagesTsj; std::vector<cv::Mat> imagesBelt; - + std::string color_result; + std::string lable_title; cout <<modelArray.size()<<endl; //寰幆姣忎釜鎽勫儚澶寸殑妯″瀷锛屽姞杞芥ā鍨� for (int j = 0; j < modelArray.size(); ++j) { Json::Value modelNode = modelArray[j]; - std::string modelPath = modelNode["modelpath"].asString(); - std::string modelPath2 = modelNode["modelpath2"].asString();//妯″瀷2璺緞 - std::string imagePath = modelNode["imagepath"].asString(); - std::string modelName = modelNode["name"].asString(); - std::string modelType = modelNode["type"].asString();//妯″瀷鍒嗘瀽绫诲瀷缂栫爜 - std::string modelAnalysis = modelNode["analysis"].asString();//妯″瀷鍒嗘瀽缁撴灉缂栫爜 - std::string leftarea = modelNode.get("leftarea", "").asString();//鐨甫宸︿晶鍖哄煙 - std::string rightarea = modelNode.get("rightarea", "").asString();//鐨甫鍙充晶鍖哄煙 - std::string area = modelNode.get("area", "").asString();//鍖哄煙涓� - std::string area2 = modelNode.get("area2", "").asString();//鍖哄煙浜� - std::string modelLimit = modelNode["limit"].asString(); - std::string modelLimit2 = modelNode.get("limit2", "").asString();//鐨甫绌鸿浇闄愬畾鍊� - std::string modelId = modelNode["modelid"].asString();//妯″瀷id锛屽尯鍒嗗姞杞戒笉鍚岀殑妯″瀷 + std::string modelCode = modelNode["code"].asString();//妯″瀷缂栫爜 鍖哄垎鍔犺浇涓嶅悓鐨勬ā鍨� + std::string modelPath = modelNode["path"].asString(); + std::string modelPath2 = modelNode["path2"].asString();//妯″瀷2璺緞 + std::string imagePath = modelNode["img_path"].asString(); - //鍔犺浇妯″瀷澶勭悊------ - switch (stoi(modelId)) + std::string modelName = modelNode["name"].asString(); + Json::Value modelTypeArr = modelNode["type"];////妯″瀷鍒嗘瀽绫诲瀷缂栫爜 + std::string modelType = jsontostr(modelTypeArr); + + double threshold = stod(modelNode["threshold"].asString());//闃堝�� + float modelLimit = stof(modelNode["limit"].asString());//闄愬畾鍊� + std::string modelAnalysis =""; + color_result = modelNode["color_result"].asString();//棰滆壊鍙嶆鍊� + + Json::Value rects = modelNode["point_rects"];////妯″瀷鍒嗘瀽绫诲瀷缂栫爜 + for (auto k = 0; k < rects.size(); ++k) { - case 1://鐨甫鐘舵�� - if (!rBelt.initConfig(modelPath.c_str(), stringToIntArray(area))) + Json::Value pointNode = rects[k]; + lable_title=pointNode["title"].asString(); + std::string point_type=pointNode["type"].asString(); + Json::Value point_Arr= pointNode["points"]; + for (auto x = 0; x < point_Arr.size(); ++x) + { + int croodx = stoi(point_Arr[x]["x"].asString()); + int croody = stoi(point_Arr[x]["y"].asString()); + if(point_type=="detectarea") + { + detectarea.push_back(cv::Point(croodx, croody)); + } + if (point_type=="flowarea") + { + flowarea.push_back(cv::Point(croodx, croody)); + } + if (point_type=="flowrelate") + { + flowrelate.push_back(cv::Point(croodx, croody)); + } + if (point_type=="leftroller") + { + leftarea.push_back(cv::Point(croodx, croody)); + } + if (point_type=="rightroller") + { + rightarea.push_back(cv::Point(croodx, croody)); + } + if (point_type=="beltarea") + { + beltarea.push_back(cv::Point(croodx, croody)); + } + } + } + + int* arr = new int[detectarea.size() * 2]; + //鍔犺浇妯″瀷澶勭悊------ + if(modelCode=="1") //鐨甫鐘舵�� + { + if (!rBelt.initConfig(modelPath.c_str(),arr)) { printf("鐨甫杩愯妯″瀷鍒濆鍖栧け璐ャ�俓n"); } @@ -659,7 +810,7 @@ printf("鐨甫杩愯妯″瀷鍒濆鍖栨垚鍔熴�俓n"); rBelt.isLoad = true; } - areaBelt = stringToPoints(area); + areaBelt =detectarea; videoPathBelt = coalCode + ipccode + modelType + modelAnalysis; wfBelt.setCoalCode(coalCode); wfBelt.setCameraCode(ipccode); @@ -667,10 +818,10 @@ wfBelt.setRtspUrl(ipcrtsppath); wfBelt.setAnalyse(modelType); wfBelt.setAnalyseResult(modelAnalysis); - break; - - case 2://鎻愬崌鏈鸿繍琛屾娴嬫ā鍨� - if (!rTSJ.initConfig(modelPath.c_str(), stringToIntArray(area))) + } + if(modelCode=="2") //鎻愬崌鏈鸿繍琛屾娴嬫ā鍨� + { + if (!rTSJ.initConfig(modelPath.c_str(), arr)) { printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲澶辫触銆俓n"); } @@ -679,7 +830,7 @@ printf("鎻愬崌鏈烘ā鍨嬪垵濮嬪寲鎴愬姛銆俓n"); rTSJ.isLoad = true; } - areaTSJ = stringToPoints(area); + areaTSJ = detectarea; videoPathTSJ = coalCode + ipccode + modelType; wfTSJ.setCoalCode(coalCode); wfTSJ.setCameraCode(ipccode); @@ -688,9 +839,9 @@ wfTSJ.setAnalyse(modelType); wfTSJ.setAnalyseResult(modelAnalysis); break; - - case 3://鍖哄煙闂叆妯″瀷 - + } + if(modelCode=="3") //浜哄憳鍖哄煙闂叆妯″瀷 + { if (!hatJC.initConfig(modelPath.c_str(), 0.5, 0.5)) { printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲澶辫触\n"); @@ -700,8 +851,8 @@ printf("鐩爣妫�娴嬫ā鍨嬪垵濮嬪寲鎴愬姛銆俓n"); hatJC.isLoad = true; } - areaPerson = stringToPoints(area);//鍗遍櫓鍖哄煙 - workareaPerson = stringToPoints(area2);//宸ヤ綔鍖哄煙 + areaPerson = detectarea;//鍗遍櫓鍖哄煙 + workareaPerson = workarea;//宸ヤ綔鍖哄煙 videoPathPerson = coalCode + ipccode + modelType + modelAnalysis; wfPerson.setCoalCode(coalCode); wfPerson.setCameraCode(ipccode); @@ -709,10 +860,9 @@ wfPerson.setRtspUrl(ipcrtsppath); wfPerson.setAnalyse(modelType); wfPerson.setAnalyseResult(modelAnalysis); - - break; - - case 4://鎽勫儚澶撮伄鎸$畻娉� + } + if(modelCode=="4") //鎽勫儚澶撮伄鎸$畻娉� + { if (!cover.initConfig()) { printf("鎽勫儚澶撮伄鎸$畻娉曞垵濮嬪寲澶辫触\n"); } @@ -728,29 +878,30 @@ wfCameraCover.setRtspUrl(ipcrtsppath); wfCameraCover.setAnalyse(modelType); wfCameraCover.setAnalyseResult(modelAnalysis); - break; - - case 5://鎽勫儚澶寸Щ鍔ㄧ畻娉� + } + if(modelCode=="5") //鎽勫儚澶寸Щ鍔ㄧ畻娉� + { if (!camera.initConfig()) { - printf("鎽勫儚澶寸Щ鍔ㄧ畻娉曞垵濮嬪寲澶辫触\n"); - } - else + printf("鎽勫儚澶寸Щ鍔ㄧ畻娉曞垵濮嬪寲澶辫触\n"); + } + else + { + std::cout << "鎽勫儚澶寸Щ鍔ㄧ畻娉曞垵濮嬪寲鎴愬姛" << std::endl; + camera.isLoad = true; + camera.stdMat = cv::imread(imagePath); + } + videoPathCameraMove = coalCode + ipccode + modelType + modelAnalysis; + wfCameraMove.setCoalCode(coalCode); + wfCameraMove.setCameraCode(ipccode); + wfCameraMove.setCameraName(ipcname); + wfCameraMove.setRtspUrl(ipcrtsppath); + wfCameraMove.setAnalyse(modelType); + wfCameraMove.setAnalyseResult(modelAnalysis); + } + if(modelCode=="6") //鍫嗙叅妫�娴嬫ā鍨� + { + if (!dm.initConfig(modelPath.c_str(), arr)) { - std::cout << "鎽勫儚澶寸Щ鍔ㄧ畻娉曞垵濮嬪寲鎴愬姛" << std::endl; - camera.isLoad = true; - camera.stdMat = cv::imread(imagePath); - } - videoPathCameraMove = coalCode + ipccode + modelType + modelAnalysis; - wfCameraMove.setCoalCode(coalCode); - wfCameraMove.setCameraCode(ipccode); - wfCameraMove.setCameraName(ipcname); - wfCameraMove.setRtspUrl(ipcrtsppath); - wfCameraMove.setAnalyse(modelType); - wfCameraMove.setAnalyseResult(modelAnalysis); - break; - - case 6://鍫嗙叅妫�娴嬫ā鍨� - if (!dm.initConfig(modelPath.c_str(), stringToIntArray(area))) { printf("鍫嗙叅妯″瀷鍒濆鍖栧け璐n"); } else @@ -759,7 +910,7 @@ dm.isLoad = true; } videoPathDM = coalCode + ipccode + modelType + modelAnalysis; - areaDM = stringToPoints(area2); + areaDM = detectarea; limitDM = modelLimit;//鍫嗙叅闄愬畾鐓ら噺鍗犳瘮 wfDM.setCoalCode(coalCode); @@ -768,10 +919,14 @@ wfDM.setRtspUrl(ipcrtsppath); wfDM.setAnalyse(modelType); wfDM.setAnalyseResult(modelAnalysis); - break; + } + if(modelCode=="7") //鐨甫璺戝亸鍜屽紓鐗╂娴� + { + int* leftarr = new int[leftarea.size() * 2]; + int* rightarr = new int[rightarea.size() * 2]; + int* midd_arr = new int[beltarea.size() * 2]; - case 7://鐨甫璺戝亸鍜屽紓鐗╂娴� - if (!beltJC.initConfig(modelPath.c_str(), stringToIntArray(leftarea), stringToIntArray(rightarea), stringToIntArray(area),stof(modelLimit))) + if (!beltJC.initConfig(modelPath.c_str(), leftarr, rightarr, midd_arr, modelLimit)) { printf("鐨甫璺戝亸鍜屽紓鐗╂娴嬫ā鍨嬪垵濮嬪寲澶辫触\n"); } @@ -780,9 +935,9 @@ printf("鐨甫璺戝亸鍜屽紓鐗╂娴嬫ā鍨嬪垵濮嬪寲鎴愬姛銆俓n"); beltJC.isLoad = true; } - leftareaBeltJC = stringToPoints(leftarea); - rightareaBeltJC = stringToPoints(rightarea); - areaBeltJC = stringToPoints(area); + leftareaBeltJC = leftarea; + rightareaBeltJC = rightarea; + areaBeltJC = beltarea; videoPathBeltJC = coalCode + ipccode + modelType + modelAnalysis; wfBeltJC.setCoalCode(coalCode); wfBeltJC.setCameraCode(ipccode); @@ -790,8 +945,9 @@ wfBeltJC.setRtspUrl(ipcrtsppath); wfBeltJC.setAnalyse(modelType); wfBeltJC.setAnalyseResult(modelAnalysis); - break; - case 8://鐚磋溅澶х墿浠� + } + if(modelCode=="8") //鐚磋溅澶х墿浠� + { if (!hatHC.initConfig(modelPath.c_str(), 0.5, 0.5)) { printf("涔樼尨杞︽惡甯﹀ぇ浠舵ā鍨嬪垵濮嬪寲澶辫触\n"); @@ -801,7 +957,7 @@ printf("涔樼尨杞︽惡甯﹀ぇ浠舵ā鍨嬪垵濮嬪寲鎴愬姛銆俓n"); hatHC.isLoad = true; } - areaHC = stringToPoints(area); + areaHC = detectarea; videoPathHC = coalCode + ipccode + modelType + modelAnalysis; wfHC.setCoalCode(coalCode); wfHC.setCameraCode(ipccode); @@ -809,9 +965,10 @@ wfHC.setRtspUrl(ipcrtsppath); wfHC.setAnalyse(modelType); wfHC.setAnalyseResult(modelAnalysis); - break; + } - case 9://鐫″矖鍒ゆ柇 + if (modelCode == "9")//鐫″矖鍒ゆ柇 + { if (!hatSleep.initConfig(modelPath.c_str(), 0.5, 0.5)) { printf("鐫″矖妯″瀷鍒濆鍖栧け璐n"); @@ -821,7 +978,7 @@ printf("鐫″矖妯″瀷鍒濆鍖栨垚鍔熴�俓n"); hatSleep.isLoad = true; } - areaSleep = stringToPoints(area); + areaSleep = detectarea; videoPathSleep = coalCode + ipccode + modelType + modelAnalysis; wfSleep.setCoalCode(coalCode); wfSleep.setCameraCode(ipccode); @@ -829,9 +986,9 @@ wfSleep.setRtspUrl(ipcrtsppath); wfSleep.setAnalyse(modelType); wfSleep.setAnalyseResult(modelAnalysis); - break; - - case 11://绌挎埓妫�娴�,鍒濆鍖栦袱涓ā鍨� + } + if (modelCode == "10")//绌挎埓妫�娴�,鍒濆鍖栦袱涓ā鍨� + { if (!hatWear.initConfig(modelPath.c_str(), 0.5, 0.5)) { printf("绌挎埓妯″瀷1鍒濆鍖栧け璐n"); @@ -849,7 +1006,7 @@ } hatWear.isLoad = true; } - areaWear = stringToPoints(area); + areaWear = detectarea; videoPathWear = coalCode + ipccode + modelType + modelAnalysis; wfWear.setCoalCode(coalCode); wfWear.setCameraCode(ipccode); @@ -857,10 +1014,9 @@ wfWear.setRtspUrl(ipcrtsppath); wfWear.setAnalyse(modelType); wfWear.setAnalyseResult(modelAnalysis); - - break; - case 12://鐑熺伀妯″瀷 - + } + if (modelCode == "11")//鐑熺伀妯″瀷 + { if (!hatFire.initConfig(modelPath.c_str())) { printf("鐑熺伀妯″瀷鍒濆鍖栧け璐n"); } @@ -869,7 +1025,7 @@ printf("鐑熺伀妯″瀷鍒濆鍖栨垚鍔熴�俓n"); hatFire.isLoad = true; } - areaFire = stringToPoints(area); + areaFire = detectarea; videoPathFire = coalCode + ipccode + modelType + modelAnalysis; wfFire.setCoalCode(coalCode); wfFire.setCameraCode(ipccode); @@ -877,11 +1033,8 @@ wfFire.setRtspUrl(ipcrtsppath); wfFire.setAnalyse(modelType); wfFire.setAnalyseResult(modelAnalysis); - break; - default: - break; } - } + } std::queue<int> quTSJ;//璁板綍鎻愬崌鏈鸿繍鍔ㄧ殑鍒ゆ柇缁撴灉 std::queue<int> quBelt;//璁板綍鐨甫杩愬姩鍒ゆ柇缁撴灉 @@ -891,7 +1044,7 @@ Mat iMatWear;//浜哄憳绌挎埓鐨勭涓�甯� long currentFrame = 1;//璁板綍褰撳墠甯ф暟 - long skip_frame_num = deley;//璺冲抚闂撮殧璁剧疆 + long skip_frame_num = skipN;//璺冲抚闂撮殧璁剧疆 Mat frame; while (!asyncStop) { @@ -930,7 +1083,7 @@ //浜哄憳闂叆妯″瀷澶勭悊鍙婂垽鏂� modelId = 3 if (hatJC.isLoad) { - futurePerson = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelHAT, &ppm, std::ref(frame), std::ref(hatJC), areaPerson,workareaPerson); + futurePerson = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelHAT1, &ppm, std::ref(frame), std::ref(hatJC), areaPerson,workareaPerson,c_list,lable_title,color_result); } //鎽勫儚澶寸Щ鍔ㄥ拰閬尅涓撶敤 @@ -1629,7 +1782,9 @@ currentFrame = 0; } - currentFrame++; + currentFrame++; + + fnWriteToRedis(context,ipccode); } catch (const std::exception& ex) { @@ -1659,14 +1814,14 @@ Mat frame; long currentFrame = 1; - long skip_frame_num = deley;//璺冲抚闂撮殧璁剧疆锛堟瘡1甯э級 + long skip_frame_num = skipN;//璺冲抚闂撮殧璁剧疆锛堟瘡1甯э級 //鍒涘缓url杩炴帴锛屽畾鏃跺垽鏂湁娌℃湁椤甸潰杩涜鎷夋祦 CURL* curl; CURLcode res; std::string readBuffer; curl = curl_easy_init(); - bool pushflg = false;//鏄惁鎺ㄦ祦鏍囧織 + bool pushflg = true;//鏄惁鎺ㄦ祦鏍囧織 if(curl) { @@ -1745,17 +1900,7 @@ //cv::resize(frame, frame, Size(1280, 720)); imgStr = Mat2Base64(frame, ".jpg");//杈撳嚭Base64缂栫爜鐨勫瓧绗︿覆 - //std::cout << "Base64 size: " << imgStr.size() << std::endl; - - // // 缂栫爜涓篔PEG鏍煎紡鐨勪簩杩涘埗鏁版嵁 - // std::vector<uchar> encoded; - // std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 90}; - // cv::imencode(".jpg", frame, encoded, params); - // imgStr = std::string(reinterpret_cast<const char*>(encoded.data()), encoded.size()); - // std::cout << "Binary size: " << imgStr.size() << std::endl; - - //cout << getCurrentDateTime("%Y%m%d%H%M%S") << endl; - + //std::cout << "Base64 size: " << imgStr.size() << std::endl; objRabbitmq.Publish(imgStr, ipccode, ""); //cv::resize(frame, frame, Size(1280, 720)); @@ -1796,6 +1941,37 @@ //瀹氫箟鎺ュ彈甯� Mat frame; + vector<string> arguments = { + "ffmpeg " + "-hwaccel","cuvid", + "-hwaccel_output_format","cuda", + "-y", "-an", + "-f", "rawvideo", + "-vcodec", "rawvideo", + "-pix_fmt", "bgr24", + "-s", "640x480", + "-r", "15", + "-i", "-", + "-pix_fmt", "yuv420p", + "-f", "flv", + "-max_delay", "1000", + "-flvflags", "no_duration_filesize", + "-c:v","h264_nvenc", + "-b:v", "3M", + "-g:v", "15", + "-bf", "0", + "-bufsize", "50000000", + "-rtbufsize", "50000000", + "rtmp://192.168.1.8:1935/live/camera1" }; + + string ffmpeg_command = join(arguments, " "); + + // 鎵撳紑FFmpeg杩涚▼ + FILE* pipe = popen(ffmpeg_command.c_str(), "w"); + if (!pipe) { + std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl; + } + while (!asyncStop) { //std::cout << ipccode + "褰撳墠绾跨▼Rabbitmq鏁版嵁鏁伴噺2222:" << queJC2.size() << std::endl; try @@ -1814,20 +1990,92 @@ queJC2.pop(); if (frame.empty())//甯т负绌猴紝鍒欒垗寮� + continue; + + + // 灏嗗抚鍐欏叆鍒癋Fmpeg绠¢亾涓� + fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe); + + //cv::resize(frame, frame, Size(1280, 720)); + //cv::imshow(ipccode, frame);//灞曠ず甯у浘鐗� + //waitKey(10); + + //std::this_thread::sleep_for(std::chrono::milliseconds(10));// 寤惰繜绛夊緟涓�娈垫椂闂� + + } + catch (const std::exception& ex) + { + std::string errorMessage = "淇濆瓨鍒癛abbitMQ澶辫触:-"; + errorMessage += ex.what(); + cout << errorMessage << endl; + continue; + } + } + + frame.release(); +} + + + +/// @brief 鎺ㄦ祦鍒版祦濯掍綋鏈嶅姟鍣� +/// @param queJC2 澶勭悊瀹岀殑瑙嗛娴佸抚闃熷垪 +/// @param ipccode 鎽勫儚鏈虹紪鍙� +void PreProcessFn::fnPushVideoToUrl(queue<Mat>& queJC2,string toRtsp, string fps, string video_size,string ipccode) +{ + //瀹氫箟鎺ュ彈甯� + Mat frame; + + vector<string> arguments = { + "ffmpeg " + "-hwaccel","cuvid", + "-hwaccel_output_format","cuda", + "-y", "-an", + "-f", "rawvideo", + "-vcodec", "rawvideo", + "-pix_fmt", "bgr24", + "-s", video_size, + "-r", fps, + "-i", "-", + "-pix_fmt", "yuv420p", + "-f", "flv", + "-max_delay", "1000", + "-flvflags", "no_duration_filesize", + "-c:v","h264_nvenc", + "-b:v", "3M", + "-g:v", "15", + "-bf", "0", + "-bufsize", "50000000", + "-rtbufsize", "50000000", + toRtsp }; + + string ffmpeg_command = join(arguments, " "); + + // 鎵撳紑FFmpeg杩涚▼ + FILE* pipe = popen(ffmpeg_command.c_str(), "w"); + if (!pipe) { + std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl; + } + + while (!asyncStop) { + //std::cout << ipccode + "褰撳墠绾跨▼Rabbitmq鏁版嵁鏁伴噺2222:" << queJC2.size() << std::endl; + try + { + if (queJC2.size() > 500) + { + cout << "鎺ㄩ�佽棰戞椂锛屽瓨鍦ㄥ唴瀛樻孩鍑洪闄╋紒" << endl; + } + + if (queJC2.empty() || queJC2.size() == 0) + { continue; - - // FFmpeg鎺ㄦ祦鍛戒护 - std::string ffmpeg_command = - "ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 640x480 " - "-framerate 30 -i - -c:v libx264 -pix_fmt yuv420p -f rtsp " - "rtsp://192.168.1.188:8554/live/stream"; - - // 鎵撳紑FFmpeg杩涚▼ - FILE* pipe = popen(ffmpeg_command.c_str(), "w"); - if (!pipe) { - std::cerr << "鏃犳硶鍚姩FFmpeg" << std::endl; } + frame = queJC2.front(); + queJC2.pop(); + + if (frame.empty())//甯т负绌猴紝鍒欒垗寮� + continue; + // 灏嗗抚鍐欏叆鍒癋Fmpeg绠¢亾涓� fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe); -- Gitblit v1.8.0