派生自 development/c++

pansen
2019-01-10 0b63bd2c3eec5c34863b02a6a761c9c7b53dba91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "YoloDetectServerI.h"
 
#include <basic/util/app/AppPreference.hpp>
#include <QtCore/QString>
#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() {}
 
::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;
}