-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetector.cc
192 lines (167 loc) · 5.62 KB
/
detector.cc
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
#include <pistache/http.h>
#include <pistache/router.h>
#include <pistache/endpoint.h>
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/cvstd.hpp>
#include "arcsoft_face_sdk.h"
#include "amcomdef.h"
#include "asvloffscreen.h"
#include "merror.h"
#include <iostream>
#include <stdarg.h>
#include <string>
#include <sstream>
#include "shared_queue.hpp"
#pragma comment(lib, "libarcsoft_face_engine.lib")
#pragma comment(lib, "libpistache.lib")
#define psw1 "txwUMMgDJr1SZurx1HV1PvdMAdhDEXBmTt1E79QtRtE"
#define psw2 "9H1e5nrF8SY52npAEnuaLGTdnaUasVdVYUvKkqRWR4Nk"
#define str std::to_string
using namespace Pistache;
using cv::String;
void recognize(rapidjson::Document &doc, const MHandle &handle, cv::Mat &img, MRESULT &res)
{
ASF_MultiFaceInfo detectedFaces;
if (img.cols % 4 != 0) //4-bytes alignment
{
cv::Mat align = cv::Mat::zeros(img.rows, (4 - img.cols % 4), img.type());
img = img.t();
align = align.t();
img.push_back(align);
img = img.t();
}
res = ASFDetectFaces(handle, img.cols, img.rows, ASVL_PAF_RGB24_B8G8R8, (MUInt8 *)img.ptr<uchar>(0), &detectedFaces);
if (res != MOK)
{
return;
}
auto &allocator = doc.GetAllocator();
doc.AddMember("count", detectedFaces.faceNum, allocator);
doc.AddMember("boxes", rapidjson::Value(rapidjson::kArrayType), allocator);
for (int i = 0, x1, y1, x2, y2; i < detectedFaces.faceNum; i += 1)
{
rapidjson::Value array(rapidjson::kArrayType);
x1 = detectedFaces.faceRect[i].left;
y1 = detectedFaces.faceRect[i].top;
x2 = detectedFaces.faceRect[i].right;
y2 = detectedFaces.faceRect[i].bottom;
x1 = std::max(0, x1);
y1 = std::max(0, y1);
x2 = std::min(img.cols, x2);
y2 = std::min(img.rows, y2);
array.PushBack(x1, allocator);
array.PushBack(y1, allocator);
array.PushBack(x2, allocator);
array.PushBack(y2, allocator);
doc["boxes"].PushBack(array, allocator);
}
}
class StatsEndpoint
{
public:
StatsEndpoint(Address addr)
: httpEndpoint(std::make_shared<Http::Endpoint>(addr))
{
}
void init(MInt32 mask, size_t thr = 5)
{
auto opts = Http::Endpoint::options().threads(thr).maxRequestSize(1e9);
std::vector<MRESULT> results;
for (int i = 0; i < thr; i += 1)
{
MHandle handle = nullptr;
auto res = ASFInitEngine(ASF_DETECT_MODE_IMAGE, ASF_OP_0_ONLY, 30, 50, mask, &handle);
results.push_back(res);
queue.push(handle);
}
if (std::any_of(results.begin(), results.end(), [&](MRESULT &res) { return res != MOK; }))
{
std::cout << "engines init failed" << std::endl;
}
httpEndpoint->init(opts);
setupRoutes();
}
void start()
{
httpEndpoint->setHandler(router.handler());
httpEndpoint->serve();
}
private:
void setupRoutes()
{
using namespace Rest;
Routes::Post(router, "/detect", Routes::bind(&StatsEndpoint::doDetect, this));
Routes::Get(router, "/ping", Routes::bind(&StatsEndpoint::doPing, this));
}
void doPing(const Rest::Request &request, Http::ResponseWriter response)
{
rapidjson::Document doc;
doc.SetObject();
rapidjson::StringBuffer result;
rapidjson::Writer<rapidjson::StringBuffer> writer(result);
doc.AddMember("code", static_cast<int>(Http::Code::Ok), doc.GetAllocator());
doc.Accept(writer);
response.send(Http::Code::Ok, std::string(result.GetString()));
}
void doDetect(const Rest::Request &request, Http::ResponseWriter response)
{
auto image_data = request.body();
auto image_data_vector = std::vector<char>(image_data.begin(), image_data.end());
auto image = cv::imdecode(image_data_vector, cv::IMREAD_ANYCOLOR);
rapidjson::Document doc;
doc.SetObject();
rapidjson::StringBuffer result;
rapidjson::Writer<rapidjson::StringBuffer> writer(result);
MHandle handle = nullptr;
MRESULT res = -1;
queue.wait_and_pop(handle);
recognize(doc, handle, image, res);
if (res != MOK)
{
doc.AddMember("code", static_cast<int>(Http::Code::Not_Acceptable), doc.GetAllocator());
doc.Accept(writer);
response.send(Http::Code::Not_Acceptable, std::string(result.GetString()));
}
doc.AddMember("code", static_cast<int>(Http::Code::Ok), doc.GetAllocator());
queue.push(handle);
doc.Accept(writer);
response.send(Http::Code::Ok, std::string(result.GetString()));
}
shared_queue<MHandle> queue;
std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Router router;
};
int main(int argc, char *argv[])
{
auto res = ASFActivation(psw1, psw2);
if (res != MOK && res != MERR_ASF_ALREADY_ACTIVATED)
{
std::cout << res << " wdnmd" << std::endl;
return 0;
}
else
{
std::cout << "activation successful" << std::endl;
}
MInt32 mask = ASF_FACE_DETECT;
Port port(11459);
int thr = 5;
if (argc >= 2)
{
port = std::stol(argv[1]);
if (argc == 3)
thr = std::stol(argv[2]);
}
Address addr(Ipv4::any(), port);
std::cout << "Cores = " << hardware_concurrency() << std::endl;
std::cout << "Using " << thr << " threads" << std::endl;
StatsEndpoint stats(addr);
stats.init(mask, thr);
stats.start();
return 0;
}