From f2a3422c62a1176b50038e0e7bd81f05227834b7 Mon Sep 17 00:00:00 2001 From: pansen <pansen626@sina.com> Date: 星期一, 04 三月 2019 09:46:51 +0800 Subject: [PATCH] 测试代码变动,及格式化变更 --- QiaoJiaSystem/YoloServer/YoloDetectServerI.cpp | 190 ++++++++++++++++++++++++++++++++++++----------- 1 files changed, 146 insertions(+), 44 deletions(-) diff --git a/QiaoJiaSystem/YoloServer/YoloDetectServerI.cpp b/QiaoJiaSystem/YoloServer/YoloDetectServerI.cpp index 02ef93f..33ebe72 100644 --- a/QiaoJiaSystem/YoloServer/YoloDetectServerI.cpp +++ b/QiaoJiaSystem/YoloServer/YoloDetectServerI.cpp @@ -14,40 +14,40 @@ ::YoloDetect::ObjInfos YoloDetectServerI::YoloDetect(::Ice::Int w, ::Ice::Int h, const ::std::string &shM, const ::Ice::Current &) { - DnDetect::ClockTimer ct("YoloDetectServerI::YoloDetect"); + 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; - } - //鍙戠幇浜唒id閫�鍑哄惊鐜� - if (find_pid) { - t_dnDetect = item.second.dnDetect; - break; - } - } +// 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; +// } +// //鍙戠幇浜唒id閫�鍑哄惊鐜� +// if (find_pid) { +// t_dnDetect = item.second.dnDetect; +// break; +// } +// } ::YoloDetect::ObjInfos objInfos; - if (!m_bInitThd || t_dnDetect == nullptr) { + if (!m_bInitThd /*|| t_dnDetect == nullptr*/) { ERR("error "); return objInfos; } @@ -55,12 +55,64 @@ 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); + +// double bttime=what_time_is_it_now(); + image im = matToImg(_mat); +// DBG("matToImg : "<<what_time_is_it_now()-bttime); +// printf("matToImg %f seconds.\n", what_time_is_it_now()-bttime); + image sized = letterbox_image(im, m_net->w, m_net->h); + layer l = m_net->layers[m_net->n - 1]; + + float *X = sized.data; + //attime=what_time_is_it_now();p-> + network_predict(m_net, X); + //printf("Predicted in %f seconds.\n", what_time_is_it_now()-attime); + int nboxes = 0; + detection *dets = get_network_boxes(m_net, im.w, im.h, m_thresh, m_hier_thresh, 0, 1, &nboxes); + if (nboxes > 30) { + DBG("nboxes="<<nboxes); + return objInfos; } + if (m_nms) do_nms_sort(dets, nboxes, l.classes, m_nms); + // draw_detections(im, dets, nboxes, m_thresh, names, alphabet, l.classes); + for (int i = 0; i < nboxes; i++) { + ::YoloDetect::ObjInfo objInfo; + std::vector<float> vec(80); + memcpy(&vec[0], dets[i].prob, sizeof(float) * 80); + int type = -1; + for (int j = 0; j < l.classes; ++j) { +// if(j != 0){ +// continue; +// } + if (dets[i].prob[j] > 0.0f) { + if (type < 0) { + type = j; + objInfo.prob = dets[i].prob[j]; + } else { + } + } else { + } + } + if (type >= 0) { +// if(type != 0){ +// continue; +// } + objInfo.type = type; + objInfo.rcObj.left = (dets[i].bbox.x - dets[i].bbox.w / 2.); + objInfo.rcObj.top = (dets[i].bbox.y - dets[i].bbox.h / 2.); + objInfo.rcObj.right = (dets[i].bbox.x + dets[i].bbox.w / 2.); + objInfo.rcObj.bottom = (dets[i].bbox.y + dets[i].bbox.h / 2.); + objInfos.push_back(objInfo); + } + + + } + free_detections(dets, nboxes); + // show_image(im, "Video"); + // cv::waitKey(10); + free_image(im); + free_image(sized); + //printf("all time use %f seconds.\n", what_time_is_it_now()-bttime); } return objInfos; } @@ -68,15 +120,24 @@ 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_thresh = appPref.getFloatData("thresh.detect"); + cuda_set_device(appPref.getIntData("gpu.index")); - } + char *datacfg = "cfg/coco.data"; + char *cfgfile = "cfg/yolov3.cfg"; + char *weightfile = "./yolov3.weights"; + + double loadtime = what_time_is_it_now(); + list *options = read_data_cfg(datacfg); + char *name_list = option_find_str(options, "names", "data/names.list"); + p->names = get_labels(name_list); + + p->alphabet = load_alphabet(); + p->m_net = load_network(cfgfile, weightfile, 0); + set_batch_network(p->m_net, 1); + printf("load mod use %f seconds.\n", what_time_is_it_now() - loadtime); + + srand(2222222); p->m_bInitThd = true; return 0; } @@ -105,6 +166,47 @@ return mat; } +image YoloDetectServerI::matToImg(cv::Mat &RefImg) { + CV_Assert(RefImg.depth() == CV_8U); + + int h = RefImg.rows; + int w = RefImg.cols; + int channels = RefImg.channels(); + image im = make_image(w, h, 3); + int count = 0; + switch (channels) { + case 1: { + cv::MatIterator_<unsigned char> it, end; + for (it = RefImg.begin<unsigned char>(), end = RefImg.end<unsigned char>(); it != end; ++it) { + im.data[count] = im.data[w * h + count] = im.data[w * h * 2 + count] = (float) (*it) / 255.0; + + ++count; + } + break; + } + case 3: { + float *desData = im.data; + uchar *srcData = RefImg.data; + + int size = w * h; + int size2 = size * 2; + for (int i = 0; i < size; i++) { + *(desData) = *(srcData + 2) / 255.0f; + *(desData + size) = *(srcData + 1) / 255.0f; + *(desData + size2) = *(srcData) / 255.0f; + + desData++; + srcData += 3; + } + break; + } + + default: + printf("Channel number not supported.\n"); + break; + } + return im; +} YoloDetect::stringData YoloDetectServerI::getCocoData(const Ice::Current &) { YoloDetect::stringData retval; -- Gitblit v1.8.0