forked from AlexeyAB/darknet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crow.cpp
90 lines (80 loc) · 2.84 KB
/
crow.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
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
#include "crow.h"
#include "./src/yolo_v2_class.hpp"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
//to eliminate converting std::__cxx11::string to std::string error
#define _GLIBCXX_USE_CXX11_ABI 0
std::string return_result(std::vector<bbox_t> const result_vec, std::vector<std::string> const obj_names) {
using namespace rapidjson;
using namespace std;
StringBuffer s;
Writer<StringBuffer> writer(s);
writer.StartObject();
writer.Key("url");
writer.String("some.jpg");
writer.Key("response");
writer.StartObject();
writer.Key("annotations");
writer.StartArray();
for (auto &i : result_vec) {
writer.StartObject();
//if (obj_names.size() > i.obj_id) { obj_names[i.obj_id] = " - ";}
const std::string label = obj_names[i.obj_id];
const char* label_char = label.c_str();
writer.Key("width");
writer.Uint(i.h);
writer.Key("height");
writer.Uint(i.h);
writer.Key("left");
writer.Uint(i.x);
writer.Key("top");
writer.Uint(i.y);
writer.Key("label");
writer.String(label_char);
writer.EndObject();
};
writer.EndArray();
writer.EndObject();
writer.EndObject();
return s.GetString();
}
std::vector<std::string> objects_names_from_file(std::string const filename) {
std::ifstream file(filename);
std::vector<std::string> file_lines;
if (!file.is_open()) return file_lines;
for(std::string line; getline(file, line);) file_lines.push_back(line);
std::cout << "object names loaded \n";
return file_lines;
}
bool is_file_exist(const char *fileName)
{
std::ifstream infile(fileName);
return infile.good();
}
int main(int argc, char* argv[])
{
crow::SimpleApp app;
//initiate a variable of Detector type called detector
Detector detector("./cfg/slyz.cfg" , "./slyz_3300.weights");
auto obj_names = objects_names_from_file("data/slyz.names");
CROW_ROUTE(app, "/detect/<str>")([&detector, &obj_names](std::string filename){
//initiate a vector variable called detection that will only contain bbox_t type data
std::string path = "./data/test/slyz" + filename + ".jpg";
const char* path_char = path.c_str();
if(is_file_exist(path_char)){
std::vector<bbox_t> detection = detector.detect(path, 0.5, 0);
std::string json = return_result(detection, obj_names);
return json;
} else {
std::string no_existing_dir = "directory does not exist";
return no_existing_dir;
}
});
app.port(3205).multithreaded().run();
}