-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.cpp
172 lines (132 loc) · 3.42 KB
/
project.cpp
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
#include "project.h"
#include <QDataStream>
#include <QFile>
#include <QList>
#include <QDebug>
Project::Project() {
}
Project::Project(QString source_project_file) {
Open(source_project_file);
}
bool Project::Create(std::vector<QImage> patterns) {
// add patterns
for(int i = 0; i < patterns.size(); i++)
if(!net_.AddPattern(patterns.at(i)))
return false;
// training hamming network
if(!net_.Train())
return false;
number_of_attempts_ = 0;
number_of_errors_ = 0;
ready_ = true;
return true;
}
bool Project::Create(std::vector<QString> patterns) {
std::vector<QImage> imgs;
for(int i = 0; i < patterns.size(); i++)
imgs.push_back(QImage(patterns.at(i)));
return Create(imgs);
}
int Project::GetSimilarPatternNumber(QImage img) {
if(!ready_)
return -1;
// icrement number of attempts and votes_
number_of_attempts_++;
votes_++;
return net_.TestPattern(img);
}
int Project::GetSimilarPatternNumber(QString file_path) {
if(!ready_)
return -1;
// icrement number of attempts and vots
number_of_attempts_++;
votes_++;
return net_.TestPattern(file_path);
}
QImage Project::GetSimilarPattern(QImage img) {
if(!ready_)
return QImage();
int number = net_.TestPattern(img);
if(number < 0)
return QImage();
// icrement number of attempts and votes_
number_of_attempts_++;
votes_++;
return net_.GetPattern(number);
}
QImage Project::GetSimilarPattern(QString file_path) {
QImage img(file_path);
return GetSimilarPattern(img);
}
int Project::GetAttemptsNumber() {
return number_of_attempts_;
}
int Project::GetErrorsNumber() {
return number_of_errors_;
}
bool Project::Open(QString source_project_file) {
QFile file(source_project_file);
if(!file.open(QIODevice::ReadOnly))
return false;
QDataStream ds(&file);
QList<QImage> patterns;
ds >> patterns;
ds >> number_of_attempts_;
ds >> number_of_errors_;
int width;
int height;
ds >> width;
ds >> height;
net_.Resize(width, height);
// add patterns
for(int i = 0; i < patterns.size(); i++)
if(!net_.AddPattern(patterns.at(i)))
return false;
// training hamming network
if(!net_.Train())
return false;
ready_ = true;
project_file_path_ = source_project_file;
file.close();
return true;
}
NetworkInformation Project::GetNetworkInformation() {
return net_.GetInformation();
}
bool Project::SaveAs(QString project_path) {
QFile file(project_path);
if(!file.open(QIODevice::WriteOnly))
return false;
// write serialized data to file
QDataStream ds(&file);
QList<QImage> patterns;
for(int i = 0; i < net_.GetCountOfPatterns(); i++)
patterns.push_back(net_.GetPattern(i));
ds << patterns;
ds << number_of_attempts_;
ds << number_of_errors_;
ds << net_.GetImageWidth();
ds << net_.GetImageHeight();
//file.flush();
file.close();
project_file_path_ = project_path;
return true;
}
bool Project::Save() {
return SaveAs(project_file_path_);
}
bool Project::IsReady() {
return ready_;
}
bool Project::IsSaved() {
return project_file_path_.size() > 0;
}
void Project::IncreaseError() {
if(votes_) {
number_of_errors_++;
votes_--;
}
}
void Project::Resize(int width, int height) {
net_.Resize(width, height);
}