forked from kimphg/FireDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlameDecider.cpp
233 lines (205 loc) · 6.2 KB
/
FlameDecider.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
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
//
// FlameDecider.cpp
// FlameDetection
//
// Created by liberize on 14-5-20.
// Copyright (c) 2014年 liberize. All rights reserved.
//
#include "FlameDecider.h"
#include "FlameDetector.h"
#include "FeatureAnalyzer.h"
const string FlameDecider::SVM_DATA_FILE("svmdata.xml");
#ifdef TRAIN_MODE
const string FlameDecider::SAMPLE_FILE("sample.txt");
#endif
#ifdef TRAIN_MODE
FlameDecider::FlameDecider()
: mSampleEnough(false)
, mFlameCount(0)
, mNonFlameCount(0)
, mFrameCount(0)
{
Feature feature;
bool isFlame;
// read the previous learning data from sample file
ifstream ifs(SAMPLE_FILE);
while (ifs >> feature >> isFlame) {
mFeatureVec.push_back(feature);
mResultVec.push_back(isFlame);
if (isFlame) {
mFlameCount++;
} else {
mNonFlameCount++;
}
}
ifs.close();
if (mFlameCount >= MIN_SAMPLE_COUNT && mNonFlameCount >= MIN_SAMPLE_COUNT) {
mSampleEnough = true;
cout << "Flame count: " << mFlameCount << ", non-flame count: " << mNonFlameCount << "." << endl;
}
}
#else
FlameDecider::FlameDecider()
{
mSVM.load(SVM_DATA_FILE.c_str());
}
#endif
#ifdef TRAIN_MODE
void FlameDecider::userInput(const map<int, Target>& targets)
{
ofstream ofs(SAMPLE_FILE, ios::app);
for (map<int, Target>::const_iterator it = targets.begin(); it != targets.end(); it++) {
if (it->second.lostTimes > 0) {
continue;
}
if(!it->second.feature.dataReady)continue;
const Feature& feature = it->second.feature;
const Rectangle& rect = it->second.region.rect;
#ifdef DEBUG_MODE
//cout << "freq: " << feature.frequency << endl;
//feature.printAreaVec();
feature.printValue();
std::cout.flush();
#endif
Mat temp;
mFrame.copyTo(temp);
bool flag = true;
while (true) {
int key = waitKey(200);
switch (key) {
case -1: // no key pressed
rectangle(temp, rect, flag ? Scalar(0, 0, 255) : Scalar(0, 255, 0));
//namedWindow("temp");
moveWindow("temp", 0, 0);
imshow("temp", temp);
flag = !flag;
break;
case 'y': // press 'y' to add a positive record to sample
ofs << feature << true << endl;
mFeatureVec.push_back(feature);
mResultVec.push_back(true);
mFlameCount++;
goto next;
case 'n': // press 'n' to add a negative record to sample
ofs << feature << false << endl;
mFeatureVec.push_back(feature);
mResultVec.push_back(false);
mNonFlameCount++;
goto next;
case ' ': // press SPACE to skip current target
goto next;
case 's': // press 's' to skip current frameo
goto end;
case 27: // press ESC to stop training and exit program
// trainComplete = true;
goto end;
case 'o': // press 'o' to stop input and start studying
mSampleEnough = true;
goto end;
default:
break;
}
}
next:
if (mFlameCount >= MIN_SAMPLE_COUNT && mNonFlameCount >= MIN_SAMPLE_COUNT) {
mSampleEnough = true;
goto end;
}
}
end:
ofs.close();
cout << "Flame count: " << mFlameCount << ", non-flame count: " << mNonFlameCount << "." << endl;
}
void FlameDecider::svmStudy()
{
assert(mFeatureVec.size() == mResultVec.size());
int size = int(mFeatureVec.size());
Mat data(size, Feature::LEN, CV_32FC1);
Mat label(size, 1, CV_32FC1);
for (int i = 0; i < size; i++) {
Mat(mFeatureVec[i]).copyTo(data.row(i));
label.at<float>(i, 0) = mResultVec[i] ? 1.0 : 0.0;
}
CvSVMParams params;
params.svm_type = CvSVM::C_SVC;
params.kernel_type = CvSVM::LINEAR;
params.term_crit = cvTermCriteria(CV_TERMCRIT_ITER, 100, 1e-6);
mSVM.train(data, label, Mat(), Mat(), params);
mSVM.save(SVM_DATA_FILE.c_str());
}
void FlameDecider::train(const map<int, Target>& targets)
{
if (!mSampleEnough) {
if (mFrameCount++ % FRAME_GAP == 0) {
userInput(targets);
}
} else {
svmStudy();
//trainComplete = true;
}
}
#else
inline bool FlameDecider::svmPredict(const Feature& feature)
{
Mat tesst = Mat(feature);
tesst =tesst;
float result = mSVM.predict(Mat(feature));
//cout << "result: " << result << endl;
// if(result)
// {
// int a;
// a=0;
// }
return result == 1.0;
}
int FlameDecider::judge(map<int, Target>& targets)
{
int flameDetected = 0;
// Mat temp;
// mFrame.copyTo(temp);
for (map<int, Target>::iterator it = targets.begin(); it != targets.end(); it++)
{
if(!it->second.feature.dataReady)continue;
if(it->second.times<100)continue;
if(!it->second.feature.checkValid()){continue;}
bool isFlame = svmPredict(it->second.feature);
if(!it->second.isFlame)
{
it->second.isFlame = isFlame;
it->second.flameCount = 0;
}
if(isFlame)
{
it->second.flameCount++;
//it->second.feature.printValue();
}
else
{
if(it->second.flameCount)it->second.flameCount--;
}
if(flameDetected<it->second.flameCount)flameDetected = it->second.flameCount;
if (flameDetected)
{
m_Rect = it->second.region.rect;
ofstream ofs("detection.txt", ios::app);
ofs << it->second.feature << false << endl;
}
}
//#ifdef DEBUG_MODE
// namedWindow("result");
// moveWindow("result", 0, 0);
// imshow("result", temp);
//#endif
return flameDetected;
}
#endif
int FlameDecider::decide(const Mat& frame, map<int, Target>& targets)
{
mFrame = frame;
#ifdef TRAIN_MODE
train(targets);
return false;
#else
return judge(targets);
#endif
}