Repository navigation
Expand file tree
/
Copy pathyoloexport.cpp
More file actions
243 lines (213 loc) · 7.48 KB
/
Copy pathyoloexport.cpp
File metadata and controls
243 lines (213 loc) · 7.48 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "yoloexport.h"
#include <iostream>
#include <QRandomGenerator>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QPolygonF>
#include <QProgressDialog>
#include <QObject>
#include <QApplication>
#include <QMessageBox>
void YOLOExporter::setExportParam(ExportMode mode, const QString& inputDir, const QString& outputDir,
const QVector<QString> classNames, int trainSplit, int valSplit, int testSplit)
{
m_mode=mode;
m_inputDir=inputDir;
m_outputDir=outputDir;
m_trainSplit=trainSplit;
m_valSplit=valSplit;
m_testSplit=testSplit;
m_classNames=classNames;
}
void YOLOExporter::exportDataset() {
// 创建必要的目录结构
createOutputDirs();
QDir inputDir(m_inputDir);
QStringList fileNames = inputDir.entryList(QStringList() << "*.path", QDir::Files);
// 随机打乱文件列表
shuffleFileList(fileNames);
// 根据比例划分训练集、验证集和测试集
int totalFiles = fileNames.size();
int sum = m_trainSplit + m_valSplit + m_testSplit;
int trainCount = static_cast<int>(totalFiles * m_trainSplit*1.0/sum);
int valCount = static_cast<int>(totalFiles * m_valSplit*1.0/sum);
//int testCount = totalFiles - trainCount - valCount;
// 创建进度条对话框
QProgressDialog progressDialog;
progressDialog.setWindowTitle(tr("处理进度"));
progressDialog.setLabelText(tr("正在转换..."));
progressDialog.setRange(0, totalFiles);
progressDialog.setModal(true); // 设置为模态对话框,阻塞用户其他操作
progressDialog.show();
//qDebug()<<trainCount;
for (int i = 0; i < totalFiles; ++i) {
QString fileName = fileNames[i];
QString destinationDir;
if (i < trainCount) {
destinationDir = "/train/";
} else if (i < trainCount + valCount) {
destinationDir = "/val/";
} else {
destinationDir = "/test/";
}
processFile(fileName, destinationDir);
progressDialog.setValue(i);
QApplication::processEvents();
}
progressDialog.setValue(totalFiles);
// 创建数据集配置文件
createYmalFile();
}
void YOLOExporter::createOutputDirs() {
QDir dir(m_outputDir);
if (!dir.exists()) {
dir.mkpath(".");
}
else{
if(!dir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries).isEmpty())//路径不为空提示用户是否删除
{
// 创建消息框询问用户
QMessageBox::StandardButton reply;
reply = QMessageBox::question(nullptr,
"警告",
"目录为非空,YES删除再输出。NO覆盖输出。",
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
dir.removeRecursively();
}
}
}
QDir imagesDir(m_outputDir + "/images");
if (!imagesDir.exists()) {
imagesDir.mkpath(".");
}
QDir labelsDir(m_outputDir + "/labels");
if (!labelsDir.exists()) {
labelsDir.mkpath(".");
}
imagesDir.mkpath("train");
imagesDir.mkpath("val");
imagesDir.mkpath("test");
labelsDir.mkpath("train");
labelsDir.mkpath("val");
labelsDir.mkpath("test");
}
void YOLOExporter::shuffleFileList(QStringList& fileNames) {
// 随机打乱文件列表
for (int i = fileNames.size() - 1; i > 0; --i) {
int j = QRandomGenerator::global()->bounded(i + 1);
fileNames.swapItemsAt(i, j);
}
}
void YOLOExporter::processFile(const QString& fileName, const QString& destinationDir) {
//判断原始图片文件
QString piaxName = "";
QStringList pixExtNames{".jpg",".png",".jpeg",".jfif"};
for(auto one:pixExtNames){
QString fileBaseName = fileName;
piaxName = m_inputDir + "/" + fileBaseName.replace(".path", one);
if(QFile::exists(piaxName)){
break;
}
piaxName = "";
}
if(piaxName.isEmpty()) return;
QString outPixname = m_outputDir + "/images" + destinationDir + QFileInfo(piaxName).fileName();
QFile::copy(piaxName,outPixname);
//打开path文件
QString pathFileName = m_inputDir + "/" + fileName;
QFile inputFile(pathFileName);
if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "Failed to open file: " << fileName.toStdString() << std::endl;
return;
}
//创建输出txt文件
QString labelName = fileName;
labelName.replace(".path", ".txt");
QString outputFileName = m_outputDir + "/labels" + destinationDir + labelName;
QFile outputFile(outputFileName);
if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
std::cerr << "Failed to open output file: " << outputFileName.toStdString() << std::endl;
return;
}
//qDebug()<<piaxName<<" "<<outPixname;
//qDebug()<<pathFileName<<" "<<outputFileName;
//YOLO txt文件转换
QTextStream in(&inputFile);
QTextStream out(&outputFile);
while (!in.atEnd()) {
QString line = in.readLine();
QStringList tokens = line.split(" ");
if(tokens.size()<4) continue;
//qDebug()<<tokens;
QString type = tokens[0];
int classId = tokens[1].toInt();
QVector<QPointF> points;
for (int i = 2; i < tokens.size(); i += 2) {
if((i+1)>=tokens.size()) continue;
qreal x = tokens[i].toDouble();
qreal y = tokens[i + 1].toDouble();
points.append(QPointF(x, y));
}
// 根据标注类型导出
if (type == "rect") {
handleRect(classId, points, out);
} else if (type == "path") {
handlePath(classId, points, out);
}
}
//qDebug()<<"**************************";
inputFile.close();
outputFile.close();
}
void YOLOExporter::handleRect(int classId, const QVector<QPointF>& points, QTextStream& out) {
if (m_mode == OBJECT_DETECTION) {
// 导出为目标检测的矩形框
QPolygonF polygon;
polygon.append(points);
QRectF rect = polygon.boundingRect();
out << classId << " "
<< (rect.left() + rect.right()) / 2 << " "
<< (rect.top() + rect.bottom()) / 2 << " "
<< rect.width() << " "
<< rect.height() << "\n";
}
else if (m_mode == ROTATED_RECTANGLE) {
// 导出旋转矩形框
out << classId ;
for(auto one:points)
{
out<<" "<<one.x()<<" "<<one.y();
}
out<<"\n";
}
}
void YOLOExporter::handlePath(int classId, const QVector<QPointF>& points, QTextStream& out) {
if (m_mode == INSTANCE_SEGMENTATION) {
// 导出为实例分割
out << classId ;
for (const QPointF& point : points) {
out<< " " << point.x() << " " << point.y();
}
out << "\n";
}
}
void YOLOExporter::createYmalFile() {
QFile dataFile(m_outputDir + "/classes.yaml");
if (!dataFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
std::cerr << "Failed to open classes.yaml" << std::endl;
return;
}
QTextStream out(&dataFile);
out << "path: " << m_outputDir << "\n";
out << "train: images/train\n";
out << "val: images/val\n";
out << "test: images/test\n\n\n";
out << "names:\n";
for(int i=0;i!=m_classNames.size();i++)
{
out << " "<<QString::number(i)<<": "<<m_classNames[i]<<"\n";
}
dataFile.close();
}