表格线识别通用库文档
载入中...
搜索中...
未找到
inout.hpp
浏览该文件的文档.
1/*
2 * @Description: 输入输出工具 头文件及其实现
3 * @Version:
4 * @Autor: dreamy-xay
5 * @date: 2023-12-14
6 * @LastEditors: dreamy-xay
7 * @LastEditTime: 2024-03-13
8 */
9
10#ifndef COMMON_UTILS_INOUT_HPP
11#define COMMON_UTILS_INOUT_HPP
12
13#include <fstream>
14#include <opencv2/core.hpp>
15#include <opencv2/imgcodecs.hpp>
16#include <opencv2/opencv.hpp>
17#include <unordered_map>
18#include <vector>
19
20#include "common/enum.h"
21#include "common/macro.h"
22#include "common/type.h"
23#include "common/utils/file.hpp"
25
26namespace cm {
27
38template <typename T, typename HASH = EnumHash<T>>
40 private:
41 std::unordered_map<T, std::string, HASH> path_table; // 文件夹路径表
42 std::unordered_map<ImageType, std::string, EnumHash<ImageType>> image_type_table; // 图片类型表
43 std::unordered_map<FileType, std::string, EnumHash<FileType>> file_type_table; // 文件类型表
44
45 std::string output_root_path; // 输出根路径
46
47 public:
49 std::string output_file_prefix;
50
51 FileManger(const std::string& output_root_path = "", const std::string& output_file_prefix = "");
52
53 FileManger<T, HASH>& SetDirectionName(T dir, const std::string& name);
54
55 FileManger<T, HASH>& SetDirectionNames(const std::unordered_map<T, std::string, HASH>& names_table);
56
57 FileManger<T, HASH>& OutputImage(T dir, const cv::Mat& image, const std::vector<int>& image_params = {}, const std::string& image_name_suffix = "", ImageType type = IMT_JPG);
58
59 FileManger<T, HASH>& OutputTextFile(T dir, const std::string& text, const std::string& file_name_suffix = "", FileType type = FT_TXT);
60};
61
72template <typename T, typename HASH>
73inline FileManger<T, HASH>::FileManger(const std::string& output_root_path, const std::string& output_file_prefix) : output_root_path(output_root_path), output_file_prefix(output_file_prefix) {
74 // 支持输出图片类型
75 image_type_table[IMT_JPG] = "jpg";
76 image_type_table[IMT_JPEG] = "jpeg";
77 image_type_table[IMT_PNG] = "png";
78 image_type_table[IMT_BMP] = "bmp";
79 image_type_table[IMT_TIF] = "tif";
80 image_type_table[IMT_GIF] = "gif";
81
82 // 支持输出文件类型
83 file_type_table[FT_TXT] = "txt";
84 file_type_table[FT_LOG] = "log";
85 file_type_table[FT_JSON] = "json";
86 file_type_table[FT_XML] = "xml";
87 file_type_table[FT_CSV] = "csv";
88}
89
104template <typename T, typename HASH>
106 // 当前文件夹路径
107 std::string current_path = path_table[dir] = output_root_path + '/' + name;
108
109 // 如果文件夹不存在,则创建文件夹,创建失败则抛出异常
110 if (!CreateFolder(current_path.c_str()))
111 throw Exception("Failed to create folder.");
112
113 return *this;
114}
115
127template <typename T, typename HASH>
128inline FileManger<T, HASH>& FileManger<T, HASH>::SetDirectionNames(const std::unordered_map<T, std::string, HASH>& names_table) {
129 // 遍历该表,设置输出文件目录名
130 for (auto it = names_table.begin(); it != names_table.end(); ++it)
131 this->SetDirectionName(it->first, it->second);
132
133 return *this;
134}
135
153template <typename T, typename HASH>
154inline FileManger<T, HASH>& FileManger<T, HASH>::OutputImage(T dir, const cv::Mat& image, const std::vector<int>& image_params, const std::string& image_name_suffix, ImageType type) {
155 // 输出图片
156 cv::imwrite(Sprintf("%s/%s%s%s.%s", path_table[dir].c_str(), output_file_prefix.c_str(), (image_name_suffix.empty() ? "" : "_"), image_name_suffix.c_str(), image_type_table[type].c_str()), image, image_params);
157
158 return *this;
159}
160
175template <typename T, typename HASH>
176inline FileManger<T, HASH>& FileManger<T, HASH>::OutputTextFile(T dir, const std::string& text, const std::string& file_name_suffix, FileType type) {
177 // 文件写入流
178 std::ofstream ofs;
179
180 // 打开文件,不存在则创建
181 ofs.open(Sprintf("%s/%s%s%s.%s", path_table[dir].c_str(), output_file_prefix.c_str(), (file_name_suffix.empty() ? "" : "_"), file_name_suffix.c_str(), file_type_table[type].c_str())); // flawfinder: ignore
182
183 // 断言文件是否成功打开
184 Cm_Assert(ofs.is_open(), "failed to open the file!!!");
185
186 ofs << text; // 写入数据到文件
187 ofs.close(); // 关闭文件流
188
189 return *this;
190}
191
192} // namespace cm
193
194#endif
通用异常类
Definition type.h:39
文件管理器类
Definition inout.hpp:39
FileManger(const std::string &output_root_path="", const std::string &output_file_prefix="")
文件管理类构造函数
Definition inout.hpp:73
FileManger< T, HASH > & SetDirectionNames(const std::unordered_map< T, std::string, HASH > &names_table)
批量设置指定输出文件夹的名称
Definition inout.hpp:128
std::string output_file_prefix
输出文件名前缀
Definition inout.hpp:49
FileManger< T, HASH > & OutputImage(T dir, const cv::Mat &image, const std::vector< int > &image_params={}, const std::string &image_name_suffix="", ImageType type=IMT_JPG)
输出指定类型图片文件到指定文件夹
Definition inout.hpp:154
FileManger< T, HASH > & SetDirectionName(T dir, const std::string &name)
设置指定输出文件夹的名称
Definition inout.hpp:105
FileManger< T, HASH > & OutputTextFile(T dir, const std::string &text, const std::string &file_name_suffix="", FileType type=FT_TXT)
输出指定类型文本文件到指定文件夹
Definition inout.hpp:176
点类
Definition point.hpp:52
Point()
点类的默认构造函数
Definition point.hpp:125
#define Cm_Assert(expr, message)
断言宏
Definition macro.h:109
std::string Sprintf(const char *format,...)
格式化输出字符串
Definition string.hpp:47
ImageType
图片类型枚举
Definition enum.h:126
@ IMT_BMP
bmp 后缀图片
Definition enum.h:134
@ IMT_TIF
tif 后缀图片
Definition enum.h:136
@ IMT_GIF
gif 后缀图片
Definition enum.h:138
@ IMT_JPEG
jpeg 后缀图片
Definition enum.h:130
@ IMT_JPG
jpg 后缀图片
Definition enum.h:128
@ IMT_PNG
png 后缀图片
Definition enum.h:132
bool CreateFolder(const char *path)
创建文件夹函数
Definition file.hpp:188
FileType
文本文件类型枚举
Definition enum.h:146
@ FT_LOG
log 后缀文件
Definition enum.h:150
@ FT_TXT
txt 后缀文件
Definition enum.h:148
@ FT_JSON
json 后缀文件
Definition enum.h:152
@ FT_CSV
csv 后缀文件
Definition enum.h:156
@ FT_XML
xml 后缀文件
Definition enum.h:154