-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
92 lines (79 loc) · 2.15 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
/***
* @Author: ZhangHao
* @Date: 2020-12-14 11:02:17
* @LastEditTime: 2020-12-14 11:02:17
* @LastEditors: ZhangHao
* @Description:
* @FilePath: /yolov5_inference/main.cpp
*/
#include <dirent.h>
#include "yolov5.h"
using namespace std;
void detect(YOLOV5 &net, vector<cv::Mat>& images)
{
if(net.getBatchSize() != int(images.size()))
{
ERROR << "net size != images.size !!! ";
return;
}
net.preForward(images);
net.forward();
vector<vector<DetectRect>> results = net.postForward();
}
int read_files_in_dir(const char *p_dir_name, std::vector<std::string> &file_names)
{
DIR *p_dir = opendir(p_dir_name);
if (p_dir == nullptr)
{
return -1;
}
struct dirent* p_file = nullptr;
while ((p_file = readdir(p_dir)) != nullptr)
{
if (strcmp(p_file->d_name, ".") != 0 && strcmp(p_file->d_name, "..") != 0)
{
std::string cur_file_name(p_file->d_name);
file_names.push_back(cur_file_name);
}
}
closedir(p_dir);
return 0;
}
int main(int argc, char **argv)
{
FNLog::FastStartDefaultLogger();
INFO << "log init success";
if (argc < 3)
{
ERROR << "USAGE:";
WARN << " " << argv[0] << " <image folder> <bmodel file> ";
exit(1);
}
const char * image_folder = argv[1];
std::vector<std::string> file_names;
if (read_files_in_dir(image_folder, file_names) < 0)
{
ERROR << "read files in dir failed.";
return -1;
}
string bmodel_file = argv[2];
if (access(bmodel_file.c_str(), F_OK ) == -1)
{
ERROR << "Cannot find valid model file.";
exit(1);
}
YOLOV5 net(bmodel_file);
int batch_size = net.getBatchSize();
for(int i=0; i < int(file_names.size()/batch_size); i++)
{
vector<cv::Mat> batch_imgs;
for(int j=0; j<batch_size; j++)
{
cv::Mat img = cv::imread(string(image_folder) + "/" + file_names[i * batch_size + j], cv::IMREAD_COLOR, 0);
batch_imgs.push_back(img);
}
detect(net, batch_imgs);
net.writeBatchResultImg("res/" + to_string(i));
}
}