-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree.cpp
More file actions
123 lines (101 loc) · 3.89 KB
/
Copy paththree.cpp
File metadata and controls
123 lines (101 loc) · 3.89 KB
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
#include <opencv2/opencv.hpp>
#include <inference_engine.hpp>
#include <string>
using namespace cv;
using namespace cv::dnn;
using namespace InferenceEngine;
Mat frame, blob;
float confThreshold = 0.7;
void postprocess(Mat& frame, const Mat& outs)
{
// Network produces output blob with a shape 1x1xNx7 where N is a number of
// detections and an every detection is a vector of values
// [batchId, classId, confidence, left, top, right, bottom]
float* data = (float*)outs.data;
for (size_t i = 0; i < outs.total(); i += 7)
{
float confidence = data[i + 2];
if (confidence > confThreshold)
{
int left = (int)(data[i + 3] * frame.cols);
int top = (int)(data[i + 4] * frame.rows);
int right = (int)(data[i + 5] * frame.cols);
int bottom = (int)(data[i + 6] * frame.rows);
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0));
}
}
}
static inline void genData(const std::vector<size_t>& dims, Mat& m, Blob::Ptr& dataPtr)
{
//m.create(std::vector<int>(dims.begin(), dims.end()), CV_32F);
blobFromImage(frame, m, 1, Size(300, 300));
dataPtr = make_shared_blob<float>({Precision::FP32, dims, Layout::ANY}, (float*)m.data);
}
static inline void getData(const std::vector<size_t>& dims, Mat& m, Blob::Ptr& dataPtr)
{
blobFromImage(frame, blob, 1, Size(300, 300));
dataPtr = make_shared_blob<float>({Precision::FP32, dims, Layout::ANY}, (float*)blob.data);
}
void runIE(const std::string& xmlPath, const std::string& binPath, std::map<std::string, cv::Mat>& inputsMap, std::map<std::string, cv::Mat>& outputsMap)
{
CNNNetReader reader;
reader.ReadNetwork(xmlPath);
reader.ReadWeights(binPath);
CNNNetwork net = reader.getNetwork();
InferenceEnginePluginPtr enginePtr;
InferencePlugin plugin;
ExecutableNetwork netExec;
InferRequest infRequest;
try
{
auto dispatcher = InferenceEngine::PluginDispatcher({""});
enginePtr = dispatcher.getPluginByDevice("CPU");
IExtensionPtr extension = make_so_pointer<IExtension>("libcpu_extension.so");
enginePtr->AddExtension(extension, 0);
plugin = InferencePlugin(enginePtr);
netExec = plugin.LoadNetwork(net, {});
infRequest = netExec.CreateInferRequest();
}
catch (const std::exception& ex)
{
CV_Error(Error::StsAssert, format("Failed to initialize Inference Engine backend: %s", ex.what()));
}
// Fill input blobs.
inputsMap.clear();
BlobMap inputBlobs;
for (auto& it : net.getInputsInfo())
{
genData(it.second->getTensorDesc().getDims(), inputsMap[it.first], inputBlobs[it.first]);
}
infRequest.SetInput(inputBlobs);
// Fill output blobs.
outputsMap.clear();
BlobMap outputBlobs;
for (auto& it : net.getOutputsInfo())
{
genData(it.second->getTensorDesc().getDims(), outputsMap[it.first], outputBlobs[it.first]);
}
infRequest.SetOutput(outputBlobs);
infRequest.Infer();
}
int main(int argc, char* argv[])
{
VideoCapture cap(0);
cap.read(frame);
//====================================================================================================================================
std::string xmlPath = "/home/volskig/src/weights/face-detection-retail-0004/FP32/face-detection-retail-0004.xml";
std::string binPath = "/home/volskig/src/weights/face-detection-retail-0004/FP32/face-detection-retail-0004.bin";
std::map<std::string, cv::Mat> inputsMap, ieOutputsMap;
runIE(xmlPath, binPath, inputsMap, ieOutputsMap);
postprocess(frame, ieOutputsMap["detection_out"]);
//====================================================================================================================================
while(true)
{
imshow("window", frame);
if ((int)waitKey(10) == 27)
{
break;
}
}
return 0;
}