forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnn.cpp
143 lines (112 loc) · 3.94 KB
/
dnn.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
#include "dnn.h"
Net Net_ReadNet(const char* model, const char* config) {
Net n = new cv::dnn::Net(cv::dnn::readNet(model, config));
return n;
}
Net Net_ReadNetBytes(const char* framework, struct ByteArray model, struct ByteArray config) {
std::vector<uchar> modelv(model.data, model.data + model.length);
std::vector<uchar> configv(config.data, config.data + config.length);
Net n = new cv::dnn::Net(cv::dnn::readNet(framework, modelv, configv));
return n;
}
Net Net_ReadNetFromCaffe(const char* prototxt, const char* caffeModel) {
Net n = new cv::dnn::Net(cv::dnn::readNetFromCaffe(prototxt, caffeModel));
return n;
}
Net Net_ReadNetFromCaffeBytes(struct ByteArray prototxt, struct ByteArray caffeModel) {
Net n = new cv::dnn::Net(cv::dnn::readNetFromCaffe(prototxt.data, prototxt.length,
caffeModel.data, caffeModel.length));
return n;
}
Net Net_ReadNetFromTensorflow(const char* model) {
Net n = new cv::dnn::Net(cv::dnn::readNetFromTensorflow(model));
return n;
}
Net Net_ReadNetFromTensorflowBytes(struct ByteArray model) {
Net n = new cv::dnn::Net(cv::dnn::readNetFromTensorflow(model.data, model.length));
return n;
}
void Net_Close(Net net) {
delete net;
}
bool Net_Empty(Net net) {
return net->empty();
}
void Net_SetInput(Net net, Mat blob, const char* name) {
net->setInput(*blob, name);
}
Mat Net_Forward(Net net, const char* outputName) {
return new cv::Mat(net->forward(outputName));
}
void Net_ForwardLayers(Net net, struct Mats* outputBlobs, struct CStrings outBlobNames) {
std::vector< cv::Mat > blobs;
std::vector< cv::String > names;
for (int i = 0; i < outBlobNames.length; ++i) {
names.push_back(cv::String(outBlobNames.strs[i]));
}
net->forward(blobs, names);
// copy blobs into outputBlobs
outputBlobs->mats = new Mat[blobs.size()];
for (size_t i = 0; i < blobs.size(); ++i) {
outputBlobs->mats[i] = new cv::Mat(blobs[i]);
}
outputBlobs->length = (int)blobs.size();
}
void Net_SetPreferableBackend(Net net, int backend) {
net->setPreferableBackend(backend);
}
void Net_SetPreferableTarget(Net net, int target) {
net->setPreferableTarget(target);
}
int64_t Net_GetPerfProfile(Net net) {
std::vector<double> layersTimes;
return net->getPerfProfile(layersTimes);
}
void Net_GetUnconnectedOutLayers(Net net, IntVector* res) {
std::vector< int > cids(net->getUnconnectedOutLayers());
int* ids = new int[cids.size()];
for (size_t i = 0; i < cids.size(); ++i) {
ids[i] = cids[i];
}
res->length = cids.size();
res->val = ids;
return;
}
Mat Net_BlobFromImage(Mat image, double scalefactor, Size size, Scalar mean, bool swapRB,
bool crop) {
cv::Size sz(size.width, size.height);
cv::Scalar cm = cv::Scalar(mean.val1, mean.val2, mean.val3, mean.val4);
// TODO: handle different version signatures of this function v2 vs v3.
return new cv::Mat(cv::dnn::blobFromImage(*image, scalefactor, sz, cm, swapRB, crop));
}
Mat Net_GetBlobChannel(Mat blob, int imgidx, int chnidx) {
size_t w = blob->size[3];
size_t h = blob->size[2];
return new cv::Mat(h, w, CV_32F, blob->ptr<float>(imgidx, chnidx));
}
Scalar Net_GetBlobSize(Mat blob) {
Scalar scal = Scalar();
scal.val1 = blob->size[0];
scal.val2 = blob->size[1];
scal.val3 = blob->size[2];
scal.val4 = blob->size[3];
return scal;
}
Layer Net_GetLayer(Net net, int layerid) {
return new cv::Ptr<cv::dnn::Layer>(net->getLayer(layerid));
}
void Layer_Close(Layer layer) {
delete layer;
}
int Layer_InputNameToIndex(Layer layer, const char* name) {
return (*layer)->inputNameToIndex(name);
}
int Layer_OutputNameToIndex(Layer layer, const char* name) {
return (*layer)->outputNameToIndex(name);
}
const char* Layer_GetName(Layer layer) {
return (*layer)->name.c_str();
}
const char* Layer_GetType(Layer layer) {
return (*layer)->type.c_str();
}