forked from andrewssobral/bgslibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoCapture.cpp
299 lines (249 loc) · 8.3 KB
/
VideoCapture.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
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
/*
This file is part of BGSLibrary.
BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
*/
#include "VideoCapture.h"
#include <opencv2/highgui/highgui_c.h>
namespace bgslibrary
{
namespace VC_ROI
{
IplImage* img_input1 = 0;
IplImage* img_input2 = 0;
int roi_x0 = 0;
int roi_y0 = 0;
int roi_x1 = 0;
int roi_y1 = 0;
int numOfRec = 0;
int startDraw = 0;
bool roi_defined = false;
bool use_roi = true;
bool disable_event = false;
void reset(void)
{
disable_event = false;
startDraw = false;
}
void VideoCapture_on_mouse(int evt, int x, int y, int flag, void* param)
{
if (use_roi == false || disable_event == true)
return;
if (evt == CV_EVENT_LBUTTONDOWN)
{
if (!startDraw)
{
roi_x0 = x;
roi_y0 = y;
startDraw = 1;
}
else
{
roi_x1 = x;
roi_y1 = y;
startDraw = 0;
roi_defined = true;
disable_event = true;
}
}
if (evt == CV_EVENT_MOUSEMOVE && startDraw)
{
//redraw ROI selection
img_input2 = cvCloneImage(img_input1);
cvRectangle(img_input2, cvPoint(roi_x0, roi_y0), cvPoint(x, y), CV_RGB(255, 0, 0), 1);
cvShowImage("Input", img_input2);
cvReleaseImage(&img_input2);
//startDraw = false;
//disable_event = true;
}
}
}
VideoCapture::VideoCapture() :
key(0), start_time(0), delta_time(0), freq(0),
fps(0), frameNumber(0), stopAt(0), useCamera(false),
useVideo(false), input_resize_percent(100), showOutput(true),
enableFlip(false), cameraIndex(0)
{
std::cout << "VideoCapture()" << std::endl;
setup("./config/VideoCapture.xml");
}
VideoCapture::~VideoCapture()
{
std::cout << "~VideoCapture()" << std::endl;
}
void VideoCapture::setFrameProcessor(IFrameProcessor* frameProcessorPtr)
{
frameProcessor = frameProcessorPtr;
}
void VideoCapture::setCamera(int _index)
{
useCamera = true;
cameraIndex = _index;
useVideo = false;
}
void VideoCapture::setUpCamera()
{
std::cout << "Camera index:" << cameraIndex << std::endl;
capture.open(cameraIndex);
if (!capture.isOpened())
std::cerr << "Cannot initialize webcam!\n" << std::endl;
}
void VideoCapture::setVideo(std::string _filename)
{
useVideo = true;
videoFileName = _filename;
useCamera = false;
}
void VideoCapture::setUpVideo()
{
std::cout << "Openning: " << videoFileName << std::endl;
capture.open(videoFileName.c_str());
if (!capture.isOpened())
std::cerr << "Cannot open video file " << videoFileName << std::endl;
else
std::cout << "OK" << std::endl;
}
void VideoCapture::start()
{
if (useCamera) setUpCamera();
if (useVideo) setUpVideo();
//if (!capture) std::cerr << "Capture error..." << std::endl;
//using namespace std::chrono_literals;
do
{
capture >> frame;
if (frame.empty())
{
std::cout << "Frame is not ready" << std::endl;
std::string dummy;
std::cout << "Enter to continue..." << std::endl;
std::getline(std::cin, dummy);
//cv::waitKey(1000);
//std::this_thread::sleep_for(1s);
}
else
break;
} while (1);
int input_fps = capture.get(CV_CAP_PROP_FPS);
std::cout << "input->fps:" << input_fps << std::endl;
std::cout << "input->width:" << frame.size().width << std::endl;
std::cout << "input->height:" << frame.size().height << std::endl;
if (input_fps > 0)
loopDelay = (1. / input_fps)*1000.;
std::cout << "loopDelay:" << loopDelay << std::endl;
std::cout << "Press 'ESC' to stop..." << std::endl;
do
{
frameNumber++;
capture >> frame;
if (frame.empty()) break;
cv::resize(frame, frame, cv::Size(), input_resize_percent/100., input_resize_percent / 100.);
if (firstTime && input_resize_percent != 100)
{
std::cout << "Resized to:" << std::endl;
std::cout << "input->width:" << frame.size().width << std::endl;
std::cout << "input->height:" << frame.size().height << std::endl;
}
//if (enableFlip)
// cvFlip(frame, frame, 0);
if (VC_ROI::use_roi == true && VC_ROI::roi_defined == false && firstTime == true)
{
VC_ROI::reset();
do
{
cv::Mat img_input;
frame.copyTo(img_input);
if (showOutput)
{
cv::imshow("Input", img_input);
std::cout << "Set ROI (press ESC to skip)" << std::endl;
VC_ROI::img_input1 = new IplImage(img_input);
cvSetMouseCallback("Input", VC_ROI::VideoCapture_on_mouse, NULL);
key = cv::waitKey(0);
delete VC_ROI::img_input1;
}
else
key = KEY_ESC;
if (key == KEY_ESC)
{
std::cout << "ROI disabled" << std::endl;
VC_ROI::reset();
VC_ROI::use_roi = false;
break;
}
if (VC_ROI::roi_defined)
{
std::cout << "ROI defined (" << VC_ROI::roi_x0 << "," << VC_ROI::roi_y0 << "," << VC_ROI::roi_x1 << "," << VC_ROI::roi_y1 << ")" << std::endl;
break;
}
else
std::cout << "ROI undefined" << std::endl;
} while (1);
}
if (VC_ROI::use_roi == true && VC_ROI::roi_defined == true)
{
cv::Rect roi(VC_ROI::roi_x0, VC_ROI::roi_y0, VC_ROI::roi_x1 - VC_ROI::roi_x0, VC_ROI::roi_y1 - VC_ROI::roi_y0);
frame = frame(roi);
}
cv::Mat img_input;
frame.copyTo(img_input);
if (showOutput)
cv::imshow("Input", img_input);
start_time = cv::getTickCount();
frameProcessor->process(img_input);
int64 delta_time = cv::getTickCount() - start_time;
freq = cv::getTickFrequency();
fps = freq / delta_time;
//std::cout << "FPS: " << fps << std::endl;
//cvResetImageROI(frame);
key = cv::waitKey(loopDelay);
//std::cout << "key: " << key << std::endl;
if (key == KEY_SPACE)
key = cv::waitKey(0);
if (key == KEY_ESC)
break;
if (stopAt > 0 && stopAt == frameNumber)
key = cv::waitKey(0);
firstTime = false;
} while (1);
capture.release();
}
void VideoCapture::saveConfig()
{
CvFileStorage* fs = cvOpenFileStorage(config_xml.c_str(), 0, CV_STORAGE_WRITE);
cvWriteInt(fs, "stopAt", stopAt);
cvWriteInt(fs, "input_resize_percent", input_resize_percent);
cvWriteInt(fs, "enableFlip", enableFlip);
cvWriteInt(fs, "use_roi", VC_ROI::use_roi);
cvWriteInt(fs, "roi_defined", VC_ROI::roi_defined);
cvWriteInt(fs, "roi_x0", VC_ROI::roi_x0);
cvWriteInt(fs, "roi_y0", VC_ROI::roi_y0);
cvWriteInt(fs, "roi_x1", VC_ROI::roi_x1);
cvWriteInt(fs, "roi_y1", VC_ROI::roi_y1);
cvWriteInt(fs, "showOutput", showOutput);
cvReleaseFileStorage(&fs);
}
void VideoCapture::loadConfig()
{
CvFileStorage* fs = cvOpenFileStorage(config_xml.c_str(), nullptr, CV_STORAGE_READ);
stopAt = cvReadIntByName(fs, nullptr, "stopAt", 0);
input_resize_percent = cvReadIntByName(fs, nullptr, "input_resize_percent", 100);
enableFlip = cvReadIntByName(fs, nullptr, "enableFlip", false);
VC_ROI::use_roi = cvReadIntByName(fs, nullptr, "use_roi", true);
VC_ROI::roi_defined = cvReadIntByName(fs, nullptr, "roi_defined", false);
VC_ROI::roi_x0 = cvReadIntByName(fs, nullptr, "roi_x0", 0);
VC_ROI::roi_y0 = cvReadIntByName(fs, nullptr, "roi_y0", 0);
VC_ROI::roi_x1 = cvReadIntByName(fs, nullptr, "roi_x1", 0);
VC_ROI::roi_y1 = cvReadIntByName(fs, nullptr, "roi_y1", 0);
showOutput = cvReadIntByName(fs, nullptr, "showOutput", true);
cvReleaseFileStorage(&fs);
}
}