forked from nihui/ncnn-webassembly-yolov5
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyolo.cpp
237 lines (182 loc) · 5.35 KB
/
yolo.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "layer.h"
#include "net.h"
#include <stdlib.h>
#include <float.h>
#include <stdio.h>
#include <vector>
struct Object
{
float x;
float y;
float w;
float h;
int label;
float prob;
};
static ncnn::Net* yolo = 0;
static int detect_yolo(const unsigned char* rgba_data, int width, int height, std::vector<Object>& objects)
{
if (!yolo)
{
yolo = new ncnn::Net;
fprintf(stderr, "opt num_threads = %d\n", yolo->opt.num_threads);
yolo->opt.use_vulkan_compute = true;
// yolo->opt.use_bf16_storage = true;
yolo->load_param("yolo-fastest-opt.param");
yolo->load_model("yolo-fastest-opt.bin");
}
const int target_size = 320;
// letterbox pad to multiple of 32
int w = width;
int h = height;
float scale = 1.f;
if (w > h)
{
scale = (float)target_size / w;
w = target_size;
h = h * scale;
}
else
{
scale = (float)target_size / h;
h = target_size;
w = w * scale;
}
ncnn::Mat in = ncnn::Mat::from_pixels_resize(rgba_data, ncnn::Mat::PIXEL_RGBA2RGB, width, height, w, h);
// pad to target_size rectangle
// yolo/utils/datasets.py letterbox
int wpad = (w + 31) / 32 * 32 - w;
int hpad = (h + 31) / 32 * 32 - h;
ncnn::Mat in_pad;
ncnn::copy_make_border(in, in_pad, hpad / 2, hpad - hpad / 2, wpad / 2, wpad - wpad / 2, ncnn::BORDER_CONSTANT, 114.f);
const float norm_vals[3] = {1 / 255.f, 1 / 255.f, 1 / 255.f};
in_pad.substract_mean_normalize(0, norm_vals);
ncnn::Extractor ex = yolo->create_extractor();
ex.input("data", in_pad);
ncnn::Mat out;
ex.extract("output", out);
int count = out.h;
// // apply nms with nms_threshold
// std::vector<int> picked;
// nms_sorted_bboxes(proposals, picked, nms_threshold);
objects.resize(count);
for (int i = 0; i < count; i++)
{
int label;
float x1, y1, x2, y2, score;
float pw,ph,cx,cy;
const float* values = out.row(i);
x1 = values[2] * width;
y1 = values[3] * height;
x2 = values[4] * width;
y2 = values[5] * height;
score = values[1];
label = values[0];
//处理坐标越界问题
if(x1<0) x1=0;
if(y1<0) y1=0;
if(x2<0) x2=0;
if(y2<0) y2=0;
if(x1>width) x1=width;
if(y1>height) y1=height;
if(x2>width) x2=width;
if(y2>height) y2=height;
objects[i].label = label;
objects[i].prob = score;
objects[i].x = x1;
objects[i].y = y1;
objects[i].w = x2 - x1;
objects[i].h = y2 - y1;
}
return 0;
}
#include <emscripten.h>
static const unsigned char* rgba_data = 0;
static int w = 0;
static int h = 0;
static float* result_buffer = 0;
static ncnn::Mutex lock;
static ncnn::ConditionVariable condition;
static ncnn::Mutex finish_lock;
static ncnn::ConditionVariable finish_condition;
static void worker()
{
while (1)
{
lock.lock();
while (rgba_data == 0)
{
condition.wait(lock);
}
std::vector<Object> objects;
detect_yolo(rgba_data, w, h, objects);
// result_buffer max 20 objects
if (objects.size() > 20)
objects.resize(20);
size_t i = 0;
for (; i < objects.size(); i++)
{
const Object& obj = objects[i];
result_buffer[0] = obj.label;
result_buffer[1] = obj.prob;
result_buffer[2] = obj.x;
result_buffer[3] = obj.y;
result_buffer[4] = obj.w;
result_buffer[5] = obj.h;
result_buffer += 6;
}
for (; i < 20; i++)
{
result_buffer[0] = -233;
result_buffer[1] = -233;
result_buffer[2] = -233;
result_buffer[3] = -233;
result_buffer[4] = -233;
result_buffer[5] = -233;
result_buffer += 6;
}
rgba_data = 0;
lock.unlock();
finish_lock.lock();
finish_condition.signal();
finish_lock.unlock();
}
}
#include <thread>
static std::thread t(worker);
extern "C" {
void yolo_ncnn(const unsigned char* _rgba_data, int _w, int _h, float* _result_buffer)
{
lock.lock();
while (rgba_data != 0)
{
condition.wait(lock);
}
rgba_data = _rgba_data;
w = _w;
h = _h;
result_buffer = _result_buffer;
lock.unlock();
condition.signal();
// wait for finished
finish_lock.lock();
while (rgba_data != 0)
{
finish_condition.wait(finish_lock);
}
finish_lock.unlock();
}
}