| | |
| | | #include <QtCore/QSharedMemory> |
| | | #include <basic/timer_counter/Clocktimer.h> |
| | | |
| | | 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() : 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() |
| | | {} |
| | | YoloDetectServerI::~YoloDetectServerI() {} |
| | | |
| | | ::YoloDetect::ObjInfos YoloDetectServerI::YoloDetect(::Ice::Int w, ::Ice::Int h, const ::std::string& shM, const ::Ice::Current&) |
| | | { |
| | | ::YoloDetect::ObjInfos |
| | | YoloDetectServerI::YoloDetect(::Ice::Int w, ::Ice::Int h, const ::std::string &shM, const ::Ice::Current &) { |
| | | ClockTimer ct("YoloDetectServerI::YoloDetect"); |
| | | ::YoloDetect::ObjInfos objInfos; |
| | | if(!m_bInitThd) return objInfos; |
| | | if (!m_bInitThd) return objInfos; |
| | | QSharedMemory shareMemory(QString(shM.c_str())); |
| | | if(shareMemory.attach()){ |
| | | if (shareMemory.attach()) { |
| | | int channel = 3; |
| | | cv::Mat _mat = bufferToMat(w,h,channel,shareMemory.constData()); |
| | | cv::Mat _mat = bufferToMat(w, h, channel, shareMemory.constData()); |
| | | |
| | | // 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]; |
| | | layer l = m_net->layers[m_net->n - 1]; |
| | | |
| | | float *X = sized.data; |
| | | //attime=what_time_is_it_now();p-> |
| | |
| | | detection *dets = get_network_boxes(m_net, im.w, im.h, m_thresh, m_hier_thresh, 0, 1, &nboxes); |
| | | 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++) |
| | | { |
| | | for (int i = 0; i < nboxes; i++) { |
| | | ::YoloDetect::ObjInfo objInfo; |
| | | std::vector<float> vec(80); |
| | | memcpy(&vec[0],dets[i].prob,sizeof(float)*80); |
| | | memcpy(&vec[0], dets[i].prob, sizeof(float) * 80); |
| | | int type = -1; |
| | | for(int j = 0; j < l.classes; ++j){ |
| | | for (int j = 0; j < l.classes; ++j) { |
| | | // if(j != 0){ |
| | | // continue; |
| | | // } |
| | | if (dets[i].prob[j] > 0.0f){ |
| | | if (dets[i].prob[j] > 0.0f) { |
| | | if (type < 0) { |
| | | type = j; |
| | | objInfo.prob = dets[i].prob[j]; |
| | | } else { |
| | | } |
| | | } else{ |
| | | } else { |
| | | } |
| | | } |
| | | if(type >= 0){ |
| | | 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.); |
| | | 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); |
| | | // 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; |
| | | } |
| | | |
| | | int YoloDetectServerI::init(void* arg) |
| | | { |
| | | YoloDetectServerI* p = (YoloDetectServerI*)arg; |
| | | int YoloDetectServerI::init(void *arg) { |
| | | YoloDetectServerI *p = (YoloDetectServerI *) arg; |
| | | |
| | | p->m_thresh = appPref.getFloatData("thresh.detect"); |
| | | cuda_set_device(appPref.getIntData("gpu.index")); |
| | |
| | | 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); |
| | | printf("load mod use %f seconds.\n", what_time_is_it_now() - loadtime); |
| | | |
| | | srand(2222222); |
| | | p->m_bInitThd = true; |
| | | return 0; |
| | | } |
| | | |
| | | cv::Mat YoloDetectServerI::bufferToMat(const int w,const int h,const int channels,const void* buffer) |
| | | { |
| | | 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); |
| | | 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; |
| | | } |
| | | |
| | | image YoloDetectServerI::matToImg(cv::Mat& RefImg) { |
| | | image YoloDetectServerI::matToImg(cv::Mat &RefImg) { |
| | | CV_Assert(RefImg.depth() == CV_8U); |
| | | |
| | | int h = RefImg.rows; |
| | |
| | | int channels = RefImg.channels(); |
| | | image im = make_image(w, h, 3); |
| | | int count = 0; |
| | | switch(channels){ |
| | | case 1:{ |
| | | 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; |
| | | 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; |
| | | 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; |
| | | 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; |
| | | srcData += 3; |
| | | } |
| | | break; |
| | | } |
| | |
| | | return im; |
| | | } |
| | | |
| | | YoloDetect::stringData YoloDetectServerI::getCocoData(const Ice::Current &) |
| | | { |
| | | YoloDetect::stringData YoloDetectServerI::getCocoData(const Ice::Current &) { |
| | | YoloDetect::stringData retval; |
| | | std::fstream fs("./data/coco.names"); |
| | | std::string str; |
| | | while(fs>>str){ |
| | | while (fs >> str) { |
| | | retval.push_back(str); |
| | | } |
| | | return retval; |