-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-thread.cpp
More file actions
112 lines (101 loc) · 3 KB
/
worker-thread.cpp
File metadata and controls
112 lines (101 loc) · 3 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
#include "worker-thread.h"
#include "main-window.h"
#include <QDir>
WorkerThread::WorkerThread(MainWindow *pWindow) : m_pWindow(pWindow){}
WorkerThread::~WorkerThread()
{
delete m_Perceptron;
}
const QImage *WorkerThread::getResultImage() const
{
return m_ptrResult.data();
}
int WorkerThread::getImgType() const
{
return m_imgType;
}
void WorkerThread::startLoadFile(const QString &rcFilePath)
{
if (isRunning())
return;
//
m_nOperation = LoadFile;
m_FilePath = rcFilePath;
m_ptrInput.reset(new QImage);
QThread::start();
}
void WorkerThread::startTrainModel()
{
if (isRunning())
return;
//
m_nOperation = TrainModel;
QThread::start();
}
void WorkerThread::startClassifyImage(const QImage &rcImageInput)
{
if (isRunning())
return;
//
m_nOperation = RecognizeImage;
m_ptrInput.reset(new QImage(rcImageInput));
QThread::start();
}
void WorkerThread::stop()
{
requestInterruption();
emit canceled();
}
void WorkerThread::createModel(int nSensors, int nHiddenLayers, int nHiddenNeurons, int nPatterns)
{
m_Perceptron = new Perceptron(nSensors, nHiddenLayers, nHiddenNeurons, nPatterns);
}
void WorkerThread::run()
{
switch (m_nOperation)
{
case LoadFile:
if(m_ptrInput->load(m_FilePath))
m_ptrResult.reset(new QImage(*m_ptrInput));
break;
//
case TrainModel:
{
// формирование обучающей выборки
QList<Pattern> trainSet;
// папка с обучающими изображениями
QDir imgDir(":/images/images");
QFileInfoList files = imgDir.entryInfoList(QStringList("*.jpg"));
foreach (QFileInfo file, files) {
QString fName = file.fileName();
int type = fName[fName.indexOf(QChar('-')) - 1].digitValue();
if(type != -1)
{
QImage img(imgDir.absoluteFilePath(fName));
trainSet << Pattern(img,type);
}
}
int nIters = 100; // число циклов обучения
int nPercent, nPercentPrev = 0;
for(int iter = 0; iter < nIters; ++iter){
if(isInterruptionRequested()){
break;
}
m_Perceptron->train(trainSet);
nPercent = (100 * iter) / nIters;
if(nPercent > nPercentPrev){
nPercentPrev = nPercent;
QMetaObject::invokeMethod(m_pWindow,
"updateProgress",
Qt::QueuedConnection,
Q_ARG(int, nPercent));
}
}
break;
}
case RecognizeImage:
if(m_ptrInput)
m_imgType = m_Perceptron->classify(*m_ptrInput.data());
break;
}
}