-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
85 lines (76 loc) · 2.88 KB
/
main.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
#include "detect.h"
#include "crow_all.h"
#include "base64.h"
#include <iostream>
#include <fstream>
#include <exception>
#include <utility>
using namespace std;
Detector testDetector;
int main(int argc, char* argv[]) {
setenv("CUDA_VISIBLE_DEVICES", "", -1);
string GRAPH = "optimized_face_detector.pb";
int loadModelStatus = testDetector.loadModel(GRAPH);
if (loadModelStatus < 0) {
LOG(ERROR) << "Load model failed!";
return -1;
}
// crow app
crow::SimpleApp app;
CROW_ROUTE(app, "/test").methods("POST"_method, "GET"_method)
([](const crow::request& req){
crow::json::wvalue result;
result["boxes"] = "[]";
result["scores"] = "[]";
result["labels"] = "[]";
result["flag"] = 0;
result["errorMsg"] = "";
std::ostringstream os;
try {
auto info = crow::json::load(req.body);
string base64_string = info["detect_img"].s();
// read in base 64 string
string decoded_image = base64_decode(base64_string);
vector<uchar> data(decoded_image.begin(), decoded_image.end());
Mat frame = imdecode(data, IMREAD_UNCHANGED);
// cout << "Frame shape: " << frame.size() << endl;
cvtColor(frame, frame, COLOR_BGR2RGB);
double thresholdScore = 0.5;
double thresholdIOU = 0.8;
vector<float> outBoxes;
vector<float> outScores;
vector<size_t> outLabels;
int detectStatus = testDetector.detect(frame, thresholdScore, thresholdIOU, outBoxes, outScores, outLabels);
if (detectStatus < 0) {
LOG(ERROR) << "detect failed!";
result["errorMsg"] = "Detection Error";
os << crow::json::dump(result);
return crow::response(os.str());
} else {
// for (size_t i=0;i<outLabels.size(); i++){
// LOG(INFO) << "label: " << outLabels[i];
// LOG(INFO) << "score: " << outScores[i];
// int cur_idx = i * 4;
// LOG(INFO) << "boxes: " << outBoxes.at(cur_idx) << ","
// << outBoxes.at(cur_idx+1) << ","
// << outBoxes.at(cur_idx+2) << ","
// << outBoxes.at(cur_idx+3) << endl;
//
result["boxes"] = outBoxes;
result["scores"] = outScores;
result["labels"] = outLabels;
result["flag"] = 1;
os << crow::json::dump(result);
return crow::response{os.str()};
}
}
catch (exception& e){
LOG(ERROR) << "Unpredicted error: " << e.what();
result["errorMsg"] = "Unknowm Error";
os << crow::json::dump(result);
return crow::response(os.str());
}
});
app.port(8181).run();
return 0;
}