#include "YoloDetectServerI.h" #include #include #include #include YoloDetectServerI::YoloDetectServerI() : m_thresh(0.5), m_hier_thresh(0.5), m_nms(0.5), names(nullptr), alphabet(nullptr), m_thdInit(init, this), m_bInitThd(false) { } YoloDetectServerI::~YoloDetectServerI() {} ::YoloDetect::ObjInfos YoloDetectServerI::YoloDetect(::Ice::Int w, ::Ice::Int h, const ::std::string &shM, const ::Ice::Current &) { DnDetect::ClockTimer ct("YoloDetectServerI::YoloDetect"); std::thread::id key = std::this_thread::get_id(); DBG("key is " << key); DnDetect::DnDetect *t_dnDetect = nullptr; for (auto &item : map_dnDetRes) { //是否发现pid bool find_pid = false; auto &pid_map = item.second.map_pid; for (auto &pid_item : pid_map) { if (pid_item.second == key) { // 发现pid,获取资源句柄 find_pid = true; break; } } int map_pid_size = item.second.map_pid.size(); if (!find_pid && map_pid_size < item.second.i) { //没发现句柄,并且map容量足够放入新的pid map_pid_size++; //#todo lock ? item.second.map_pid[map_pid_size] = key; find_pid = true; } //发现了pid退出循环 if (find_pid) { t_dnDetect = item.second.dnDetect; break; } } ::YoloDetect::ObjInfos objInfos; if (!m_bInitThd || t_dnDetect == nullptr) { ERR("error "); return objInfos; } QSharedMemory shareMemory(QString(shM.c_str())); if (shareMemory.attach()) { int channel = 3; cv::Mat _mat = bufferToMat(w, h, channel, shareMemory.constData()); auto res = t_dnDetect->detect(_mat); for (auto &item : res) { ::YoloDetect::ObjInfo objInfo; memcpy(&objInfo, &item, sizeof(item)); objInfos.push_back(objInfo); } } return objInfos; } int YoloDetectServerI::init(void *arg) { YoloDetectServerI *p = (YoloDetectServerI *) arg; for (int i = 0; i < 1; i++) { DnDetectRes t_detectRes; // t_detectRes.dnDetect = new DnDetect::DnDetect(i % 2); t_detectRes.dnDetect = new DnDetect::DnDetect(1); int size = appPref.getIntData("poolNum"); t_detectRes.i = size > 0 ? size : 1; p->map_dnDetRes[i] = t_detectRes; } p->m_bInitThd = true; return 0; } cv::Mat YoloDetectServerI::bufferToMat(const int w, const int h, const int channels, const void *buffer) { int nType = -1; switch (channels) { case 1: { nType = CV_8UC1; break; } case 2: { nType = CV_8UC2; break; } case 3: { nType = CV_8UC3; break; } default: { nType = CV_8UC3; break; } } cv::Mat mat(h, w, nType, (void *) buffer); return mat; } YoloDetect::stringData YoloDetectServerI::getCocoData(const Ice::Current &) { YoloDetect::stringData retval; std::fstream fs("./data/coco.names"); std::string str; while (fs >> str) { retval.push_back(str); } return retval; }