Repository navigation
Expand file tree
/
Copy pathXML_creator.cpp
More file actions
379 lines (328 loc) · 12.8 KB
/
Copy pathXML_creator.cpp
File metadata and controls
379 lines (328 loc) · 12.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "XML_creator.h"
#include "ui_XML_creator.h"
#include <QFileDialog>
#include <QDirIterator>
#include <QDebug>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QKeyEvent>
#include <QPixmap>
#include <QWidget>
#include <QStringList>
const QString XML_creator::CASCADETRAININGFOLDER = QDir::currentPath() + "/cascade_training";
XML_creator::XML_creator(QWidget *parent) :
QWidget(parent),
ui(new Ui::XML_creator)
{
ui->setupUi(this);
positiveImageLoaded = false;
stopAllOtherFunction = false;
statusBar = new QStatusBar(this);
ui->statusBarLayout->addWidget(statusBar);
}
XML_creator::~XML_creator()
{
delete ui;
}
//display negative image list and write the list to a ****bg.txt file
void XML_creator::get_negative_image_list(){
QDirIterator it(negativeFolderPath,QDir::Files);
numberofn = 0;
negativeInfoFileName = QString("%1/%2").arg(targetPath.absolutePath()).arg("neg.txt");
QFile file;
file.setFileName(negativeInfoFileName);
if (!file.open(QIODevice::Append | QIODevice::Text)){
qDebug()<<"could not open file";
}
QTextStream out(&file);
while(it.hasNext()){
it.next();
out <<it.filePath()<<'\n';
numberofn++;
ui->plainTextEdit->appendPlainText(it.fileName());
}
file.close();
QString NegativeResult = QString("%1%2").arg( numberofn).arg(" negative image are provided");
ui->plainTextEdit->appendPlainText(NegativeResult);
ui->plainTextEdit->appendPlainText("-----------Proccess Completed-----------");
}
//start creating sample process
void XML_creator::on_pushButton_sample_clicked(){
int processedImageNumber = 0;
NumberOfPositiveImage = 0;
if(!targetPath.exists())
targetPath.mkdir(targetPath.absolutePath() + targetName);
positiveInfoFileName = QString("%1/%2").arg(targetPath.absolutePath()).arg("pos.txt");
pictureIt = new QDirIterator(positiveFolderPath,QDir::Files);
QDirIterator it(positiveFolderPath,QDir::Files);
while(it.hasNext()){
it.next();
NumberOfPositiveImage++;
}
// check if there's file exist before, if so, append to it instead of making new file.
QFile file;
file.setFileName(positiveInfoFileName);
NumberOfPositiveImageDisplayed = 0;
if(!file.exists()){
statusBar->showMessage("New Object");
} else if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
QString line;
while (!in.atEnd()) {
line = in.readLine();
processedImageNumber++;
pictureIt->next();
}
NumberOfPositiveImageDisplayed = processedImageNumber;
statusBar->showMessage("Existed " + QString::number(NumberOfPositiveImageDisplayed) + " objects in pos.txt");
} else {
QMessageBox::critical(this,"Error","pos.txt exists, but is not readable");
}
file.close();
if(processedImageNumber < NumberOfPositiveImage){
displayPositiveImage();
positiveImageLoaded = true;
NumberOfSelectedObject =0;
}else{
ui->plainTextEdit->appendPlainText("<All postive images have been processed>");
}
}
//update picture Iterator
void XML_creator::updatePictureIterator(){
if(pictureIt != NULL){
delete pictureIt;
pictureIt = NULL;
}
pictureIt = new QDirIterator(QFileInfo(positiveimgpath).absoluteDir());
}
//part 1:display and output positive image information to ***infor.txt file
//part 2:move on to the next positive image
void XML_creator::mouseDoubleClickEvent(QMouseEvent *event){
if(event->buttons()==Qt::LeftButton
&& mouseIsOnPixmap
&& NumberOfPositiveImageDisplayed < NumberOfPositiveImage
&& NumberOfSelectedObject !=0
&& !stopAllOtherFunction){
if(pictureIt == NULL){
event->ignore();
qDebug() << "picture null";
return;
}
QFile file(positiveInfoFileName);
if (!file.open(QIODevice::Append | QIODevice::Text)){
qDebug()<<"could not open file";
}
ui->plainTextEdit->appendPlainText(QString("%1 %2 %3").arg(positiveimgpath).arg(NumberOfSelectedObject).arg(PositiveInfo));
QTextStream out(&file);
out << QString("%1 %2 %3%4").arg(positiveimgpath).arg(NumberOfSelectedObject).arg(PositiveInfo).arg("\n");
file.close();
if(pictureIt->hasNext() ){
event->accept();
displayPositiveImage();
}
PositiveInfo.clear();
NumberOfSelectedObject=0;
NumberOfPositiveImageDisplayed++;
if(NumberOfPositiveImageDisplayed==(NumberOfPositiveImage)){
QString positiveResult = QString("%1 %2 %3").arg("Total of").arg( NumberOfPositiveImage).arg(" positive image are proccessed");
ui->plainTextEdit->appendPlainText(positiveResult);
ui->plainTextEdit->appendPlainText("-----------Proccess Completed-----------");
}
}
}
//update the displayed image size as the XML_creator changes
void XML_creator::resizeEvent(QResizeEvent *event){
if(positiveimgpath.isEmpty()){
event->ignore();
}else{
QSize size = ui->label_displayposi->size();
ui->label_displayposi->setPixmap(QPixmap::fromImage(posdisplay).scaled(size,Qt::KeepAspectRatio)); //keep picture size ratio
displayRect = false;
event->accept();
pictureRatio = double(QImage(positiveimgpath).width())/double(ui->label_displayposi->pixmap()->width());
}
}
//display a positve image on the XML_creator
void XML_creator::displayPositiveImage(){
if(pictureIt->hasNext()){
positiveimgpath = pictureIt->next();
posdisplay = QImage(positiveimgpath);
posdisplay_px = QPixmap::fromImage(posdisplay);
QSize size = ui->label_displayposi->size();
ui->label_displayposi->setPixmap(QPixmap::fromImage(posdisplay).scaled(size,Qt::KeepAspectRatio)); //keep picture size ratio
pictureRatio = double(QImage(positiveimgpath).width())/double(ui->label_displayposi->pixmap()->width());
}else{
ui->label_displayposi->acceptDrops();
}
}
//mouse press event->getting initial position of the rectangle
void XML_creator::mousePressEvent(QMouseEvent *event){
if(!stopAllOtherFunction&& event->button()==Qt::LeftButton){
qpPixmapDimension.setX(0);
qpPixmapDimension.setY(0);
qpPixmapInitial = event->pos() - getPixmapTopleftPos();
qpMainInitial = event->pos();
statusBar->showMessage("");
}
if(!stopAllOtherFunction&&event->button()==Qt::RightButton &&qpPixmapDimension.x()!=0 &&qpPixmapDimension.y()!=0){
qpPixmapDimension.setX(0);
qpPixmapDimension.setY(0);
qpPixmapInitial = event->pos() - getPixmapTopleftPos();
qpMainInitial = event->pos();
QString cPositiveInfo = QString("%1 %2 %3 %4 ")
.arg((int)(qpPixmapInitial.x()*pictureRatio))
.arg((int)(qpPixmapInitial.y()*pictureRatio))
.arg((int)(qpPixmapDimension.x()*pictureRatio))
.arg((int)(qpPixmapDimension.y()*pictureRatio));
PositiveInfo = PositiveInfo + cPositiveInfo;
NumberOfSelectedObject++;
if(NumberOfPositiveImageDisplayed < NumberOfPositiveImage){
statusBar->showMessage("[x y width height] -> [" +cPositiveInfo+"]",5000);
}
}
}
//check if mouse position occurs within the pixmap window
void XML_creator::mouseMoveEvent(QMouseEvent *event){
if(positiveimgpath.isNull())
return;
qpPixmapFinal = event->pos() - getPixmapTopleftPos();
qpMainFinal = event->pos();
qpPixmapDimension = qpMainFinal-qpMainInitial;
QPoint p = ui->label_displayposi->mapFromParent(event->pos());
QSize Correction = (ui->label_displayposi->size() - ui->label_displayposi->pixmap()->size())/2;
int displayW = ui->label_displayposi->pixmap()->width();
int displayH = ui->label_displayposi->pixmap()->height();
QPoint CorrectedP(p.x() - Correction.width(),p.y() - Correction.height());
if(CorrectedP.x() < 0 || CorrectedP.y() < 0 ||
CorrectedP.x() > displayW || CorrectedP.y() > displayH)
{
mouseIsOnPixmap = false;
displayRect = false;
}else{
mouseIsOnPixmap = true;
displayRect = true;
}
}
//get dimensions of the marked positive ojbect
void XML_creator::mouseReleaseEvent(QMouseEvent *event)
{
if(!stopAllOtherFunction && event->button()==Qt::LeftButton){
qpPixmapFinal = event->pos() - getPixmapTopleftPos();
qpMainFinal = event->pos();
qpPixmapDimension = qpMainFinal-qpMainInitial;
}
}
//get top-left position of the pixmap window respect to the main window
QPoint XML_creator::getPixmapTopleftPos()
{
QPoint p;
if(!stopAllOtherFunction && positiveImageLoaded){
int i = (ui->label_displayposi->width()-ui->label_displayposi->pixmap()->width())/2+ui->label_displayposi->pos().x();
int j = (ui->label_displayposi->height()-ui->label_displayposi->pixmap()->height())/2+ui->label_displayposi->pos().y();
p.setX(i);
p.setY(j);
}
return p;
}
//to mark the positive image
void XML_creator::paintEvent(QPaintEvent *p)
{
p->accept();
this->update();
if(!stopAllOtherFunction
&& mouseIsOnPixmap
&& displayRect
&& positiveImageLoaded){
posdisplay_px = QPixmap::fromImage(posdisplay);
QSize size = ui->label_displayposi->size();
posdisplay_px = posdisplay_px.scaled(size,Qt::KeepAspectRatio);
QPainter painter(&posdisplay_px);
painter.setPen(Qt::blue);
QRect rec(qpPixmapInitial.x(),qpPixmapInitial.y(),qpPixmapDimension.x(),qpPixmapDimension.y());
painter.drawRect(rec);
ui->label_displayposi->setPixmap(posdisplay_px);
}
}
void XML_creator::on_pushButton_XML_clicked()
{
stopAllOtherFunction = true;
QDir workingDir(CASCADETRAININGFOLDER);
if(!workingDir.exists()){
QMessageBox::critical(this,"Error","cascade_training folder not exists");
return;
}
resetCascadeFolder();
createPositiveImageVector();
haarTraining();
}
void XML_creator::getProcessOutput()
{
statusBar->showMessage("Generating XML: cascades creation");
ui->plainTextEdit->appendPlainText(haarTrain->readAll());
}
void XML_creator::generateFileXML(const int &result)
{
if(result == QProcess::CrashExit)
{
statusBar->showMessage("");
ui->plainTextEdit->appendPlainText("Vector creation is completed!");
return;
} else {
ui->plainTextEdit->appendPlainText("Cascade files are ready for XML creation!");
}
QProcess *generateXML = new QProcess(this);
QString generateXMLprogram = CASCADETRAININGFOLDER + "/haarconv.exe";
QStringList xmlArgument;
xmlArgument << tempCascadePath
<< QString("%1%2").arg(targetPath.absolutePath()).arg(targetName + "xml")
<< "24"
<< "24";
generateXML->start(generateXMLprogram,xmlArgument);
}
void XML_creator::createPositiveImageVector()
{
statusBar->showMessage("Generating XML: vector creation");
QProcess *createVector = new QProcess(this);
QString vectorProgram = CASCADETRAININGFOLDER + "/createsamples.exe";
QStringList vectorArgument;
vecPath = QString("%1/%2").arg(targetPath.absolutePath()).arg("vec.vec");
vectorArgument<<"-info"<< positiveInfoFileName
<<"-vec" << vecPath
<<"-num" << "204"
<<"-w" << "24"
<<"-h" << "24";
createVector->start(vectorProgram,vectorArgument);
createVector->waitForFinished(-1);
}
void XML_creator::haarTraining()
{
haarTrain = new QProcess(this);
connect(haarTrain,SIGNAL(readyRead()),this,SLOT(getProcessOutput()));
connect(haarTrain,SIGNAL(finished(int)),this,SLOT(generateFileXML(int)));
QString haarTrainingProgram = CASCADETRAININGFOLDER + "/haartraining.exe";
QStringList haarTrainArgument;
QDir cascade(tempCascadePath);
if(!cascade.exists())
cascade.mkdir(tempCascadePath);
haarTrainArgument<< "-data"<< tempCascadePath
<< "-vec"<< vecPath
<< "-bg" << negativeInfoFileName
<< "-npos"<< QString::number(NumberOfPositiveImageDisplayed)
<< "-nneg"<< QString::number(numberofn)
<< "-nstages"<< "15"
<< "-mem"<<"1024"
<< "-mode"<< "ALL"
<< "-w"<< "24"
<< "-h"<< "24"
<< "rem"<<"-nonsym";
haarTrain->start(haarTrainingProgram,haarTrainArgument);
haarTrain->setReadChannelMode(QProcess::ForwardedChannels);
}
void XML_creator::resetCascadeFolder()
{
tempCascadePath = QString("%1/%2").arg(targetPath.absolutePath()).arg("cascades");
QDir cascade(tempCascadePath);
cascade.removeRecursively();
}