派生自 development/c++

pansen
2019-03-07 d3b7bbe7102cd089680a828f5d8f6402c8cf6342
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#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 &) {
    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());
 
//        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;
}
 
int YoloDetectServerI::init(void *arg) {
    YoloDetectServerI *p = (YoloDetectServerI *) arg;
 
    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;
}
 
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;
}
 
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;
    std::fstream fs("./data/coco.names");
    std::string str;
    while (fs >> str) {
        retval.push_back(str);
    }
    return retval;
}