-
Notifications
You must be signed in to change notification settings - Fork 131
/
main.cpp
110 lines (77 loc) · 3 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
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
#include <torch/torch.h>
#include <iostream>
#include <chrono>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "Darknet.h"
using namespace std;
using namespace std::chrono;
int main(int argc, const char* argv[])
{
if (argc != 2) {
std::cerr << "usage: yolo-app <image path>\n";
return -1;
}
torch::DeviceType device_type;
if (torch::cuda::is_available() ) {
device_type = torch::kCUDA;
} else {
device_type = torch::kCPU;
}
torch::Device device(device_type);
// input image size for YOLO v3
int input_image_size = 416;
Darknet net("../models/yolov3.cfg", &device);
map<string, string> *info = net.get_net_info();
info->operator[]("height") = std::to_string(input_image_size);
std::cout << "loading weight ..." << endl;
net.load_weights("../models/yolov3.weights");
std::cout << "weight loaded ..." << endl;
net.to(device);
torch::NoGradGuard no_grad;
net.eval();
std::cout << "start to inference ..." << endl;
cv::Mat origin_image, resized_image;
// origin_image = cv::imread("../139.jpg");
origin_image = cv::imread(argv[1]);
cv::cvtColor(origin_image, resized_image, cv::COLOR_BGR2RGB);
cv::resize(resized_image, resized_image, cv::Size(input_image_size, input_image_size));
cv::Mat img_float;
resized_image.convertTo(img_float, CV_32F, 1.0/255);
auto img_tensor = torch::from_blob(img_float.data, {1, input_image_size, input_image_size, 3}).to(device);
img_tensor = img_tensor.permute({0,3,1,2});
auto start = std::chrono::high_resolution_clock::now();
auto output = net.forward(img_tensor);
// filter result by NMS
// class_num = 80
// confidence = 0.6
auto result = net.write_results(output, 80, 0.6, 0.4);
auto end = std::chrono::high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
// It should be known that it takes longer time at first time
std::cout << "inference taken : " << duration.count() << " ms" << endl;
if (result.dim() == 1)
{
std::cout << "no object found" << endl;
}
else
{
int obj_num = result.size(0);
std::cout << obj_num << " objects found" << endl;
float w_scale = float(origin_image.cols) / input_image_size;
float h_scale = float(origin_image.rows) / input_image_size;
result.select(1,1).mul_(w_scale);
result.select(1,2).mul_(h_scale);
result.select(1,3).mul_(w_scale);
result.select(1,4).mul_(h_scale);
auto result_data = result.accessor<float, 2>();
for (int i = 0; i < result.size(0) ; i++)
{
cv::rectangle(origin_image, cv::Point(result_data[i][1], result_data[i][2]), cv::Point(result_data[i][3], result_data[i][4]), cv::Scalar(0, 0, 255), 1, 1, 0);
}
cv::imwrite("out-det.jpg", origin_image);
}
std::cout << "Done" << endl;
return 0;
}