forked from opengazer/OpenGazer
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathVideo.cpp
executable file
·215 lines (167 loc) · 5.72 KB
/
Video.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
#include <sys/time.h>
#include <opencv2/imgproc/imgproc.hpp>
#include "Video.h"
#include "HiResTimer.h"
#include "utils.h"
VideoInput::VideoInput():
frameCount(1),
frameRate(0),
captureFromVideo(false),
resolutionParameter(0),
_capture(cv::VideoCapture(0))
{
timeval time;
gettimeofday(&time, NULL);
_lastFrameTime = (time.tv_sec * 1000) + (time.tv_usec / 1000);
_capture.read(frame);
size = cv::Size(frame.size().width, frame.size().height);
flip(frame, frame, 1);
prepareDebugFrame();
}
VideoInput::VideoInput(std::string resolution):
frameCount(1),
frameRate(0),
captureFromVideo(false),
resolutionParameter(resolution),
_capture(cv::VideoCapture(0))
{
if (resolution.compare("720") == 0) {
_capture.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
_capture.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
} else if (resolution.compare("1080") == 0) {
_capture.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
_capture.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
} else if (resolution.compare("480") == 0) {
_capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
_capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
}
//cvSetCaptureProperty(_capture, CV_CAP_PROP_FOURCC, CV_FOURCC('M','J','P','G'));
timeval time;
gettimeofday(&time, NULL);
_lastFrameTime = (time.tv_sec * 1000) + (time.tv_usec / 1000);
_capture.read(frame);
size = cv::Size(frame.size().width, frame.size().height);
flip(frame, frame, 1);
prepareDebugFrame();
cvtColor(frame, frameGrey, CV_BGR2GRAY);
}
VideoInput::VideoInput(std::string resolution, std::string filename, bool dummy):
_capture(cv::VideoCapture(filename.c_str())),
frameCount(1),
frameRate(0),
captureFromVideo(true),
resolutionParameter(resolution)
{
timeval time;
gettimeofday(&time, NULL);
_lastFrameTime = (time.tv_sec * 1000) + (time.tv_usec / 1000);
_capture.read(frame);
std::cout << "Frame is read with size " << frame.size().width << ", " << frame.size().height << std::endl;
size = cv::Size(frame.size().width, frame.size().height);
videoResolution = frame.size().height;
double trackerResolution = atoi(resolution.c_str());
// In case the video is 1280/720 and we want to execute 480 (or 1280 -> 720)
if (videoResolution != trackerResolution) {
std::cout << "Video res ("<< videoResolution << ") different from tracker res ("<< trackerResolution << ")" << std::endl;
cv::Mat tempimage = cv::Mat(cv::Size(640, 480), CV_8UC3);
cv::Mat roi;
if (videoResolution == 720 && trackerResolution == 480) {
cv::Rect rect(160, 0, 960, 720);
roi = frame(rect);
} else if (videoResolution == 1080 && trackerResolution == 480) {
cv::Rect rect(240, 0, 1440, 1080);
roi = frame(rect);
}
else {
roi = frame;
}
//std::cout << "FRAME: " << frame->height << "x" << frame->width << " " << frame->depth << std::std::endl;
//std::cout << "TEMP: " << tempImage->height << "x" << tempImage->width << " " << tempImage->depth << std::endl;
std::cout << "Resizing to: " << tempimage.size().width << "x" << tempimage.size().height << std::endl;
resize(roi, tempimage, tempimage.size());
frame = tempimage;
size.width = frame.size().width;
size.height = frame.size().height;
std::cout << "Successfully resized first frame" << std::endl;
}
prepareDebugFrame();
cvtColor(frame, frameGrey, CV_BGR2GRAY);
}
VideoInput::~VideoInput() {
_capture.release();
}
void VideoInput::updateFrame() {
static HiResTimer timer;
// Start the timer for fps calculations
if(frameCount == 1)
timer.start();
static double trackerResolution = frame.size().height;
frameCount++;
// Every 20 frames, calculate the fps and reset timer
if(frameCount % 20 == 0) {
timer.stop();
frameRate = (double) 20 / (timer.getElapsedTime()/1000.0);
timer.start();
}
if (!(captureFromVideo && frameCount == 1)) {
// If capturing from video and video size is not equal to desired resolution, carry on with resizing
if (captureFromVideo && videoResolution != trackerResolution) {
cv::Mat temp_image;
_capture.read(temp_image);
if(temp_image.empty()) {
throw Utils::QuitNow();
}
cv::Mat roi = temp_image;
if (videoResolution == 720 && trackerResolution == 480) {
roi = temp_image(cv::Rect(160, 0, 960, 720));
} else if (videoResolution == 1080 && trackerResolution == 480) {
roi = temp_image(cv::Rect(240, 0, 1440, 1080));
}
resize(roi, frame, frame.size(), 0, 0, CV_INTER_CUBIC);
} else {
_capture.read(frame);
}
}
if(frame.empty()) {
throw Utils::QuitNow();
}
if (!captureFromVideo) {
flip(frame, frame, 1);
}
copyToDebugFrame();
cvtColor(frame, frameGrey, CV_BGR2GRAY);
}
void VideoInput::prepareDebugFrame() {
cv::Rect *geometry = Utils::getFirstMonitorGeometry();
int width = geometry->width;
int height = geometry->height;
double screenAspectRatio = width / (double) height;
double frameAspectRatio = size.width / (double) size.height;
if(screenAspectRatio > frameAspectRatio) {
// Screen is wider than necessary, height is the bounding factor
width = frameAspectRatio * height;
} else {
// Screen is narrower, width is the bounding factor
height = (int) (width / frameAspectRatio);
}
debugFrame.create(cv::Size(width, height), CV_8UC3);
copyToDebugFrame();
}
void VideoInput::copyToDebugFrame() {
cv::resize(frame, debugFrame, debugFrame.size());
}
// Returns 480 or 720 depending on camera resolution
double VideoInput::getResolution() {
double value = atof(resolutionParameter.c_str());
return value > 0 ? value : _capture.get(CV_CAP_PROP_FRAME_HEIGHT);
}
VideoWriter::VideoWriter(cv::Size size, std::string filename):
_video(filename.c_str(), 0x58564944, 15.0, size, 1)
{
}
VideoWriter::~VideoWriter() {
_video.release();
}
void VideoWriter::write(const cv::Mat &image) {
_video.write(image);
}