Skip to content

Commit f7c7606

Browse files
authored
Update main.cpp
1 parent 5767b8b commit f7c7606

File tree

1 file changed

+147
-147
lines changed

1 file changed

+147
-147
lines changed

main.cpp

Lines changed: 147 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
#define _CRT_SECURE_NO_WARNINGS
2-
#include <iostream>
3-
#include <fstream>
4-
#include <numeric>
5-
#include <opencv2/imgproc.hpp>
6-
#include <opencv2/highgui.hpp>
7-
//#include <cuda_provider_factory.h> ///nvidia-cuda加速
8-
#include <onnxruntime_cxx_api.h>
9-
10-
using namespace cv;
11-
using namespace std;
12-
using namespace Ort;
13-
14-
typedef struct BoxInfo
15-
{
16-
int xmin;
17-
int ymin;
18-
int xmax;
19-
int ymax;
20-
float score;
21-
string name;
22-
} BoxInfo;
23-
24-
class Detic
25-
{
26-
public:
27-
Detic(string modelpath);
28-
vector<BoxInfo> detect(Mat cv_image);
29-
private:
30-
void preprocess(Mat srcimg);
1+
#define _CRT_SECURE_NO_WARNINGS
2+
#include <iostream>
3+
#include <fstream>
4+
#include <numeric>
5+
#include <opencv2/imgproc.hpp>
6+
#include <opencv2/highgui.hpp>
7+
//#include <cuda_provider_factory.h> ///nvidia-cuda加速
8+
#include <onnxruntime_cxx_api.h>
9+
10+
using namespace cv;
11+
using namespace std;
12+
using namespace Ort;
13+
14+
typedef struct BoxInfo
15+
{
16+
int xmin;
17+
int ymin;
18+
int xmax;
19+
int ymax;
20+
float score;
21+
string name;
22+
} BoxInfo;
23+
24+
class Detic
25+
{
26+
public:
27+
Detic(string modelpath);
28+
vector<BoxInfo> detect(Mat cv_image);
29+
private:
30+
void preprocess(Mat srcimg);
3131
vector<float> input_image_;
3232
int inpWidth;
33-
int inpHeight;
34-
vector<string> class_names;
35-
const int max_size = 800;
36-
37-
//存储初始化获得的可执行网络
38-
Env env = Env(ORT_LOGGING_LEVEL_ERROR, "Head Pose Estimation");
33+
int inpHeight;
34+
vector<string> class_names;
35+
const int max_size = 800;
36+
37+
//存储初始化获得的可执行网络
38+
Env env = Env(ORT_LOGGING_LEVEL_ERROR, "Detic");
3939
Ort::Session *ort_session = nullptr;
4040
SessionOptions sessionOptions = SessionOptions();
4141
vector<char*> input_names;
4242
vector<char*> output_names;
4343
vector<vector<int64_t>> input_node_dims; // >=1 outputs
44-
vector<vector<int64_t>> output_node_dims; // >=1 outputs
45-
};
46-
47-
Detic::Detic(string model_path)
48-
{
49-
//OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, 0); ///nvidia-cuda加速
44+
vector<vector<int64_t>> output_node_dims; // >=1 outputs
45+
};
46+
47+
Detic::Detic(string model_path)
48+
{
49+
//OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, 0); ///nvidia-cuda加速
5050
sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_BASIC);
51-
std::wstring widestr = std::wstring(model_path.begin(), model_path.end()); ///如果在windows系统就这么写
52-
ort_session = new Session(env, widestr.c_str(), sessionOptions); ///如果在windows系统就这么写
53-
///ort_session = new Session(env, model_path.c_str(), sessionOptions); ///如果在linux系统,就这么写
51+
std::wstring widestr = std::wstring(model_path.begin(), model_path.end()); ///如果在windows系统就这么写
52+
ort_session = new Session(env, widestr.c_str(), sessionOptions); ///如果在windows系统就这么写
53+
///ort_session = new Session(env, model_path.c_str(), sessionOptions); ///如果在linux系统,就这么写
5454

5555
size_t numInputNodes = ort_session->GetInputCount();
5656
size_t numOutputNodes = ort_session->GetOutputCount();
@@ -70,46 +70,46 @@ Detic::Detic(string model_path)
7070
auto output_tensor_info = output_type_info.GetTensorTypeAndShapeInfo();
7171
auto output_dims = output_tensor_info.GetShape();
7272
output_node_dims.push_back(output_dims);
73-
}
74-
75-
ifstream ifs("imagenet_21k_class_names.txt");
76-
string line;
77-
while (getline(ifs, line))
78-
{
79-
this->class_names.push_back(line); ///你可以用随机数给每个类别分配RGB值
80-
}
81-
}
82-
83-
void Detic::preprocess(Mat srcimg)
84-
{
85-
Mat dstimg;
86-
cvtColor(srcimg, dstimg, COLOR_BGR2RGB);
87-
int im_h = srcimg.rows;
88-
int im_w = srcimg.cols;
89-
float oh, ow, scale;
90-
if (im_h < im_w)
91-
{
92-
scale = (float)max_size / (float)im_h;
93-
oh = max_size;
94-
ow = scale * (float)im_w;
95-
}
96-
else
97-
{
98-
scale = (float)max_size / (float)im_h;
99-
oh = scale * (float)im_h;
100-
ow = max_size;
101-
}
102-
float max_hw = std::max(oh, ow);
103-
if (max_hw > max_size)
104-
{
105-
scale = (float)max_size / max_hw;
106-
oh *= scale;
107-
ow *= scale;
108-
}
109-
110-
resize(dstimg, dstimg, Size(int(ow + 0.5), int(oh + 0.5)), INTER_LINEAR);
73+
}
74+
75+
ifstream ifs("imagenet_21k_class_names.txt");
76+
string line;
77+
while (getline(ifs, line))
78+
{
79+
this->class_names.push_back(line); ///你可以用随机数给每个类别分配RGB值
80+
}
81+
}
82+
83+
void Detic::preprocess(Mat srcimg)
84+
{
85+
Mat dstimg;
86+
cvtColor(srcimg, dstimg, COLOR_BGR2RGB);
87+
int im_h = srcimg.rows;
88+
int im_w = srcimg.cols;
89+
float oh, ow, scale;
90+
if (im_h < im_w)
91+
{
92+
scale = (float)max_size / (float)im_h;
93+
oh = max_size;
94+
ow = scale * (float)im_w;
95+
}
96+
else
97+
{
98+
scale = (float)max_size / (float)im_h;
99+
oh = scale * (float)im_h;
100+
ow = max_size;
101+
}
102+
float max_hw = std::max(oh, ow);
103+
if (max_hw > max_size)
104+
{
105+
scale = (float)max_size / max_hw;
106+
oh *= scale;
107+
ow *= scale;
108+
}
109+
110+
resize(dstimg, dstimg, Size(int(ow + 0.5), int(oh + 0.5)), INTER_LINEAR);
111111
this->inpHeight = dstimg.rows;
112-
this->inpWidth = dstimg.cols;
112+
this->inpWidth = dstimg.cols;
113113
this->input_image_.resize(this->inpWidth * this->inpHeight * dstimg.channels());
114114
int k = 0;
115115
for (int c = 0; c < 3; c++)
@@ -123,71 +123,71 @@ void Detic::preprocess(Mat srcimg)
123123
k++;
124124
}
125125
}
126-
}
127-
}
128-
129-
vector<BoxInfo> Detic::detect(Mat srcimg)
130-
{
131-
int im_h = srcimg.rows;
132-
int im_w = srcimg.cols;
133-
this->preprocess(srcimg);
126+
}
127+
}
128+
129+
vector<BoxInfo> Detic::detect(Mat srcimg)
130+
{
131+
int im_h = srcimg.rows;
132+
int im_w = srcimg.cols;
133+
this->preprocess(srcimg);
134134
array<int64_t, 4> input_shape_{ 1, 3, this->inpHeight, this->inpWidth };
135135

136136
auto allocator_info = MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU);
137137
Value input_tensor_ = Value::CreateTensor<float>(allocator_info, input_image_.data(), input_image_.size(), input_shape_.data(), input_shape_.size());
138138

139-
// 开始推理
140-
vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &input_names[0], &input_tensor_, 1, output_names.data(), output_names.size());
141-
142-
const float *pred_boxes = ort_outputs[0].GetTensorMutableData<float>();
143-
const float *scores = ort_outputs[1].GetTensorMutableData<float>();
144-
const int *pred_classes = ort_outputs[2].GetTensorMutableData<int>();
145-
//const float *pred_masks = ort_outputs[3].GetTensorMutableData<float>();
146-
147-
int num_box = ort_outputs[0].GetTensorTypeAndShapeInfo().GetShape()[0];
148-
const float scale_x = float(im_w) / float(inpWidth);
149-
const float scale_y = float(im_h) / float(inpHeight);
150-
vector<BoxInfo> preds;
151-
for (int i = 0; i < num_box; i++)
152-
{
153-
float xmin = pred_boxes[i * 4] * scale_x;
154-
float ymin = pred_boxes[i * 4 + 1] * scale_y;
155-
float xmax = pred_boxes[i * 4 + 2] * scale_x;
156-
float ymax = pred_boxes[i * 4 + 3] * scale_y;
157-
xmin = std::min(std::max(xmin, 0.f), float(im_w));
158-
ymin = std::min(std::max(ymin, 0.f), float(im_h));
159-
xmax = std::min(std::max(xmax, 0.f), float(im_w));
160-
ymax = std::min(std::max(ymax, 0.f), float(im_h));
161-
162-
const float threshold = 0;
163-
const float width = xmax - xmin;
164-
const float height = ymax - ymin;
165-
if (width > threshold && height > threshold)
166-
{
167-
preds.push_back({ int(xmin), int(ymin), int(xmax), int(ymax), scores[i], class_names[pred_classes[i]] });
168-
}
169-
}
170-
return preds;
171-
}
172-
173-
int main()
174-
{
175-
Detic mynet("weights/Detic_C2_R50_640_4x_in21k.onnx");
176-
string imgpath = "desk.jpg";
177-
Mat srcimg = imread(imgpath);
178-
vector<BoxInfo> preds = mynet.detect(srcimg);
179-
for (size_t i = 0; i < preds.size(); ++i)
180-
{
181-
rectangle(srcimg, Point(preds[i].xmin, preds[i].ymin), Point(preds[i].xmax, preds[i].ymax), Scalar(0, 0, 255), 2);
182-
string label = format("%.2f", preds[i].score);
183-
label = preds[i].name + " :" + label;
184-
putText(srcimg, label, Point(preds[i].xmin, preds[i].ymin - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0), 1);
185-
}
186-
187-
//imwrite("result.jpg", srcimg);
188-
static const string kWinName = "Deep learning object detection in ONNXRuntime";
189-
namedWindow(kWinName, WINDOW_NORMAL);
190-
imshow(kWinName, srcimg);
191-
waitKey(0);
192-
destroyAllWindows();
193-
}
139+
// 开始推理
140+
vector<Value> ort_outputs = ort_session->Run(RunOptions{ nullptr }, &input_names[0], &input_tensor_, 1, output_names.data(), output_names.size());
141+
142+
const float *pred_boxes = ort_outputs[0].GetTensorMutableData<float>();
143+
const float *scores = ort_outputs[1].GetTensorMutableData<float>();
144+
const int *pred_classes = ort_outputs[2].GetTensorMutableData<int>();
145+
//const float *pred_masks = ort_outputs[3].GetTensorMutableData<float>();
146+
147+
int num_box = ort_outputs[0].GetTensorTypeAndShapeInfo().GetShape()[0];
148+
const float scale_x = float(im_w) / float(inpWidth);
149+
const float scale_y = float(im_h) / float(inpHeight);
150+
vector<BoxInfo> preds;
151+
for (int i = 0; i < num_box; i++)
152+
{
153+
float xmin = pred_boxes[i * 4] * scale_x;
154+
float ymin = pred_boxes[i * 4 + 1] * scale_y;
155+
float xmax = pred_boxes[i * 4 + 2] * scale_x;
156+
float ymax = pred_boxes[i * 4 + 3] * scale_y;
157+
xmin = std::min(std::max(xmin, 0.f), float(im_w));
158+
ymin = std::min(std::max(ymin, 0.f), float(im_h));
159+
xmax = std::min(std::max(xmax, 0.f), float(im_w));
160+
ymax = std::min(std::max(ymax, 0.f), float(im_h));
161+
162+
const float threshold = 0;
163+
const float width = xmax - xmin;
164+
const float height = ymax - ymin;
165+
if (width > threshold && height > threshold)
166+
{
167+
preds.push_back({ int(xmin), int(ymin), int(xmax), int(ymax), scores[i], class_names[pred_classes[i]] });
168+
}
169+
}
170+
return preds;
171+
}
172+
173+
int main()
174+
{
175+
Detic mynet("weights/Detic_C2_R50_640_4x_in21k.onnx");
176+
string imgpath = "desk.jpg";
177+
Mat srcimg = imread(imgpath);
178+
vector<BoxInfo> preds = mynet.detect(srcimg);
179+
for (size_t i = 0; i < preds.size(); ++i)
180+
{
181+
rectangle(srcimg, Point(preds[i].xmin, preds[i].ymin), Point(preds[i].xmax, preds[i].ymax), Scalar(0, 0, 255), 2);
182+
string label = format("%.2f", preds[i].score);
183+
label = preds[i].name + " :" + label;
184+
putText(srcimg, label, Point(preds[i].xmin, preds[i].ymin - 5), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0), 1);
185+
}
186+
187+
//imwrite("result.jpg", srcimg);
188+
static const string kWinName = "Deep learning object detection in ONNXRuntime";
189+
namedWindow(kWinName, WINDOW_NORMAL);
190+
imshow(kWinName, srcimg);
191+
waitKey(0);
192+
destroyAllWindows();
193+
}

0 commit comments

Comments
 (0)