Commit 13f5f3ca by guanghui.cui

多线程处理用户信息,解决卡顿问题

parent d34be2f7
...@@ -7,6 +7,7 @@ struct TargetInfo ...@@ -7,6 +7,7 @@ struct TargetInfo
std::string strPhone; //手机号 std::string strPhone; //手机号
dlib::rectangle rc; //头像位置 dlib::rectangle rc; //头像位置
int iCode = 0; //用户编码 int iCode = 0; //用户编码
bool bSuccess = false; //是否成功获取到用户信息
}; };
struct sReqCertificate struct sReqCertificate
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "utility/utilCommonAPI.h" #include "utility/utilCommonAPI.h"
#include "utility/jsonAnalysis.h" #include "utility/jsonAnalysis.h"
#include "curl/curl.h" #include "curl/curl.h"
#include "utility/threadpool.h"
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
//using namespace cv; //using namespace cv;
using namespace std; using namespace std;
...@@ -44,7 +45,9 @@ using namespace std; ...@@ -44,7 +45,9 @@ using namespace std;
#endif #endif
std::string strUrl = "http://139.196.195.9:8788/facesearch/search4File"; std::string strUrl = "http://139.196.195.9:8788/facesearch/search4File";
std::string postResponseStr;
std::vector<TargetInfo> track_dets; //跟踪目标所在位置
std::vector<dlib::rectangle> _get_rc_detectors(std::vector<TargetInfo> &vecTargets) std::vector<dlib::rectangle> _get_rc_detectors(std::vector<TargetInfo> &vecTargets)
{ {
...@@ -144,6 +147,40 @@ CURLcode curl_post_req(const string &url, const string imgPath, string &response ...@@ -144,6 +147,40 @@ CURLcode curl_post_req(const string &url, const string imgPath, string &response
return res; return res;
} }
void reqUserInfoByImg(int index, string &imgPath)
{
std::string postResponseStr;
bool rlt = false;
auto res = curl_post_req(strUrl, imgPath, postResponseStr);
if (res != CURLE_OK) {
LOG(INFO) << "curl_easy_perform() failed: " + string(curl_easy_strerror(res));
}
else {
postResponseStr = UnicodeToAscii(Utf8ToUnicode(postResponseStr));
LOG(INFO) << "utf8 to ascii:" << postResponseStr;
rlt = true;
}
sServerResponse response = GetServerResultData(postResponseStr.data());
//更新查询到的信息
for (auto &target : track_dets) {
if (target.iCode == index) {
if (rlt&&response.vecUser.size() > 0) {
float fNum = 0.00;
fNum = atof(response.vecUser[0].confidence.data());
if (fNum > 80.000) {
target.strName = response.vecUser[0].memberName;
target.strPhone = response.vecUser[0].memberTel;
}
}
target.bSuccess = rlt;
break;
}
}
}
int main() int main()
{ {
try try
...@@ -169,12 +206,14 @@ int main() ...@@ -169,12 +206,14 @@ int main()
std::string strFilePath = GetProcDir(); std::string strFilePath = GetProcDir();
//std::string strPathImg = strFilePath + "tmpimg.jpg"; //std::string strPathImg = strFilePath + "tmpimg.jpg";
std::threadpool pool; //线程池
int frame_count = 0; int frame_count = 0;
std::vector<dlib::rectangle> dets; std::vector<dlib::rectangle> dets;
std::vector<TargetInfo> temp_dets; //缓存位置信息 std::vector<TargetInfo> temp_dets; //缓存位置信息
std::vector<TargetInfo> track_dets; //跟踪目标所在位置
std::vector<dlib::correlation_tracker> vecTracker; //跟踪目标 std::vector<dlib::correlation_tracker> vecTracker; //跟踪目标
unsigned int iReqServerCount = 0; //服务器请求计数,同时也是目标唯一标识 unsigned int iReqServerCount = 1; //服务器请求计数,同时也是目标唯一标识
int max_target = 4; //最大目标数量 int max_target = 4; //最大目标数量
for (int i = 0; i < max_target; i++) { for (int i = 0; i < max_target; i++) {
dlib::correlation_tracker tracker; dlib::correlation_tracker tracker;
...@@ -242,7 +281,7 @@ int main() ...@@ -242,7 +281,7 @@ int main()
} }
} }
if (!bTargetFind) { if (!bTargetFind || !track_dets[i].bSuccess) {
track_dets[i].rc = new_position; track_dets[i].rc = new_position;
track_dets[i].iCode = iReqServerCount; track_dets[i].iCode = iReqServerCount;
//启动查询线程,查询用户信息 //启动查询线程,查询用户信息
...@@ -251,23 +290,25 @@ int main() ...@@ -251,23 +290,25 @@ int main()
_save_target_img(temp, new_position, strPathImg.data()); _save_target_img(temp, new_position, strPathImg.data());
postResponseStr.clear(); pool.commit(reqUserInfoByImg, iReqServerCount, strPathImg);
auto res = curl_post_req(strUrl, strPathImg, postResponseStr);
if (res != CURLE_OK) //std::string postResponseStr;
LOG(INFO) << "curl_easy_perform() failed: " + string(curl_easy_strerror(res)); //auto res = curl_post_req(strUrl, strPathImg, postResponseStr);
else { //if (res != CURLE_OK)
postResponseStr = UnicodeToAscii(Utf8ToUnicode(postResponseStr)); // LOG(INFO) << "curl_easy_perform() failed: " + string(curl_easy_strerror(res));
LOG(INFO) << "utf8 to ascii:" << postResponseStr; //else {
} // postResponseStr = UnicodeToAscii(Utf8ToUnicode(postResponseStr));
sServerResponse response = GetServerResultData(postResponseStr.data()); // LOG(INFO) << "utf8 to ascii:" << postResponseStr;
if (response.vecUser.size() > 0) { //}
track_dets[i].strName = response.vecUser[0].memberName; //sServerResponse response = GetServerResultData(postResponseStr.data());
track_dets[i].strPhone = response.vecUser[0].memberTel; //if (response.vecUser.size() > 0) {
} // track_dets[i].strName = response.vecUser[0].memberName;
// track_dets[i].strPhone = response.vecUser[0].memberTel;
//}
cout << "cols:" << temp.cols << " rows:" << temp.rows << endl; //
cout << "new_position.height():" << new_position.height() << " new_position.width():" << new_position.width() << endl;
//cout << "cols:" << temp.cols << " rows:" << temp.rows << endl;
//cout << "new_position.height():" << new_position.height() << " new_position.width():" << new_position.width() << endl;
cout << "iReqServerCount:" << iReqServerCount << endl; cout << "iReqServerCount:" << iReqServerCount << endl;
iReqServerCount++; iReqServerCount++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment