-
Notifications
You must be signed in to change notification settings - Fork 27
/
nanodetncnn.cpp
172 lines (130 loc) · 3.72 KB
/
nanodetncnn.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
// 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 <benchmark.h>
#include <simpleocv.h>
#include "nanodet.h"
static int draw_fps(cv::Mat& rgba)
{
// resolve moving average
float avg_fps = 0.f;
{
static double t0 = 0.f;
static float fps_history[10] = {0.f};
double t1 = ncnn::get_current_time();
if (t0 == 0.f)
{
t0 = t1;
return 0;
}
float fps = 1000.f / (t1 - t0);
t0 = t1;
for (int i = 9; i >= 1; i--)
{
fps_history[i] = fps_history[i - 1];
}
fps_history[0] = fps;
if (fps_history[9] == 0.f)
{
return 0;
}
for (int i = 0; i < 10; i++)
{
avg_fps += fps_history[i];
}
avg_fps /= 10.f;
}
char text[32];
sprintf(text, "FPS=%.2f", avg_fps);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
int y = 0;
int x = rgba.cols - label_size.width;
cv::rectangle(rgba, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
cv::Scalar(255, 255, 255, 255), -1);
cv::putText(rgba, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 255));
return 0;
}
static NanoDet* g_nanodet = 0;
static void on_image_render(cv::Mat& rgba)
{
if (!g_nanodet)
{
g_nanodet = new NanoDet;
g_nanodet->load("coco.torchscript.ncnn");
}
std::vector<Object> objects;
g_nanodet->detect(rgba, objects);
g_nanodet->draw(rgba, objects);
draw_fps(rgba);
}
#ifdef __EMSCRIPTEN_PTHREADS__
static const unsigned char* rgba_data = 0;
static int w = 0;
static int h = 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);
}
cv::Mat rgba(h, w, CV_8UC4, (void*)rgba_data);
on_image_render(rgba);
rgba_data = 0;
lock.unlock();
finish_lock.lock();
finish_condition.signal();
finish_lock.unlock();
}
}
#include <thread>
static std::thread t(worker);
extern "C" {
void nanodet_ncnn(unsigned char* _rgba_data, int _w, int _h)
{
lock.lock();
while (rgba_data != 0)
{
condition.wait(lock);
}
rgba_data = _rgba_data;
w = _w;
h = _h;
lock.unlock();
condition.signal();
// wait for finished
finish_lock.lock();
while (rgba_data != 0)
{
finish_condition.wait(finish_lock);
}
finish_lock.unlock();
}
}
#else // __EMSCRIPTEN_PTHREADS__
extern "C" {
void nanodet_ncnn(unsigned char* rgba_data, int w, int h)
{
cv::Mat rgba(h, w, CV_8UC4, (void*)rgba_data);
on_image_render(rgba);
}
}
#endif // __EMSCRIPTEN_PTHREADS__