背景:
使用配置文件方便修改参数,而不用代码重新编译。
YOLOv7 推理时可以使用用不同的模型,来到达到推理几路(BATCH_SIZE)视频,以及输入图片的大小的目的。
配置文件 yolov7.cfg 的内容,注意等号右边不要加引号
onnx_file=../configs/weights/yolov7_b4_320x320.onnx
engine_file=../configs/weights/yolov7_b4_320x320.trt
labels_file=../configs/labels/ls.names
BATCH_SIZE=4
nms_threshold=0.5
IMAGE_WIDTH=320
IMAGE_HEIGHT=320
主要函数 GetConfigStringValue:
std::string config_file = "../configs/yolov7.cfg";
static void GetConfigStringValue(const char* keyName, char* keyValue)
{
char buff[150] = { 0 };
FILE* file = fopen(config_file.c_str(), "r");
while (fgets(buff, 100, file))
{
char* tempKeyName = strtok(buff, "=");
if (!tempKeyName) continue;
char* tempKeyValue = strtok(NULL, "=");
if (!strcmp(tempKeyName, keyName))
strcpy(keyValue, tempKeyValue);
}
fclose(file);
}
使用过程:
char onnx_file[200] = { 0 };
char engine_file[200] = { 0 };
char labels_file[200] = { 0 };
char nms_threshold[100] = { 0 };
char BATCH_SIZE[100] = { 0 };
char IMAGE_WIDTH[100] = { 0 };
char IMAGE_HEIGHT[100] = { 0 };
GetConfigStringValue("onnx_file",onnx_file);
GetConfigStringValue("engine_file",engine_file);
GetConfigStringValue("labels_file",labels_file);
GetConfigStringValue("nms_threshold",nms_threshold);
GetConfigStringValue("BATCH_SIZE",BATCH_SIZE);
GetConfigStringValue("IMAGE_WIDTH",IMAGE_WIDTH);
GetConfigStringValue("IMAGE_HEIGHT",IMAGE_HEIGHT);
onnx_file[strlen(onnx_file) - 1] = 0;
engine_file[strlen(engine_file) - 1] = 0;
labels_file[strlen(labels_file) - 1] = 0;
CONFIG config_v7; //结构体
config_v7.nms_threshold = (float)atof(nms_threshold);
config_v7.onnx_file = onnx_file;
config_v7.engine_file = engine_file;
config_v7.labels_file = labels_file;
config_v7.BATCH_SIZE = atoi(BATCH_SIZE);
config_v7.IMAGE_WIDTH = atoi(IMAGE_WIDTH);
config_v7.IMAGE_HEIGHT = atoi(IMAGE_HEIGHT);
补充:
//字符
char a = 'a';
//char b = "b"; Cannot initialize a variable of type 'char' with an lvalue of type 'const char [2]'
std::cout << a << std::endl;
//字符串其实就是字符数组
//char c[] = {'a','b','c'}; //abc
char c[] = {'a','b','c'};
std::cout <<"c : "<< c << std::endl;
cout << "c length :"<< sizeof(c) / sizeof(char) <<endl;
for (int i = 0; i < sizeof(c) / sizeof(char); ++i) {
cout <<i<<"="<<c[i]<<"=" <<endl;
}
cout << "------------------------------" << endl;
//这是C语言字符串的写法,末尾位置会有一个 \0 所以看起来它的长度是4
char c1[] = "abc"; // <==> {'a','b','c','\0'} '\0' 没有实际意义,被看成字符串结束符
cout << "c1="<<c1 <<endl;
cout << "c1 length :"<< sizeof(c1) / sizeof(char) <<endl;
for (int i = 0; i < sizeof(c1) / sizeof(char); ++i) {
cout <<i<<"="<<c1[i]<<"=" <<endl;
}