Skip to content

Commit d6647c6

Browse files
authored
Merge pull request #3191 from Wovchena/demos/cpp-add-LazyVideoWriter
demos/cpp: add LazyVideoWriter
2 parents 0fd7ab0 + 999b26b commit d6647c6

File tree

17 files changed

+70
-194
lines changed

17 files changed

+70
-194
lines changed

demos/background_subtraction_demo/cpp_gapi/main.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <utils/args_helper.hpp>
77
#include <utils_gapi/stream_source.hpp>
88
#include <utils/config_factory.h>
9+
#include <utils/ocv_common.hpp>
910

1011
#include <opencv2/gapi/streaming/cap.hpp>
1112
#include <opencv2/gapi/imgproc.hpp>
@@ -160,20 +161,13 @@ int main(int argc, char *argv[]) {
160161
cv::Size graphSize{static_cast<int>(frame_size.width / 4), 60};
161162
Presenter presenter(FLAGS_u, frame_size.height - graphSize.height - 10, graphSize);
162163

163-
/** Save output result **/
164-
cv::VideoWriter videoWriter;
165-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
166-
cap->fps(), frame_size)) {
167-
throw std::runtime_error("Can't open video writer");
168-
}
164+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
169165

170166
bool isStart = true;
171-
uint64_t curr_frame_num = 0;
172167
const auto startTime = std::chrono::steady_clock::now();
173168
pipeline.start();
174169

175170
while(pipeline.pull(cv::gout(output))) {
176-
++curr_frame_num;
177171
presenter.drawGraphs(output);
178172
if (isStart) {
179173
metrics.update(startTime, output, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX,
@@ -185,10 +179,7 @@ int main(int argc, char *argv[]) {
185179
0.65, { 200, 10, 10 }, 2, PerformanceMetrics::MetricTypes::FPS);
186180
}
187181

188-
if (videoWriter.isOpened() &&
189-
(FLAGS_limit <= 0 || curr_frame_num <= FLAGS_limit)) {
190-
videoWriter.write(output);
191-
}
182+
videoWriter.write(output);
192183

193184
if (!FLAGS_no_show) {
194185
cv::imshow(windowName, output);

demos/common/cpp/utils/include/utils/ocv_common.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,28 @@ class InputTransform {
315315
cv::Scalar means;
316316
cv::Scalar stdScales;
317317
};
318+
319+
class LazyVideoWriter {
320+
cv::VideoWriter writer;
321+
unsigned nwritten;
322+
public:
323+
const std::string filenames;
324+
const double fps;
325+
const unsigned lim;
326+
327+
LazyVideoWriter(const std::string& filenames, double fps, unsigned lim) :
328+
nwritten{1}, filenames{filenames}, fps{fps}, lim{lim} {}
329+
void write(cv::InputArray im) {
330+
if (writer.isOpened() && (nwritten < lim || 0 == lim)) {
331+
writer.write(im);
332+
++nwritten;
333+
return;
334+
}
335+
if (!writer.isOpened() && !filenames.empty()) {
336+
if (!writer.open(filenames, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, im.size())) {
337+
throw std::runtime_error("Can't open video writer");
338+
}
339+
writer.write(im);
340+
}
341+
}
342+
};

demos/crossroad_camera_demo/cpp/main.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ int main(int argc, char* argv[]) {
8888
throw std::logic_error("Can't read an image from the input");
8989
}
9090

91-
cv::VideoWriter videoWriter;
92-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
93-
cap->fps(), frame.size())) {
94-
throw std::runtime_error("Can't open video writer");
95-
}
96-
uint32_t framesProcessed = 0;
91+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
9792
cv::Size graphSize{frame.cols / 4, 60};
9893
Presenter presenter(FLAGS_u, frame.rows - graphSize.height - 10, graphSize);
9994

@@ -310,10 +305,7 @@ int main(int argc, char* argv[]) {
310305
}
311306
}
312307
}
313-
framesProcessed++;
314-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit)) {
315-
videoWriter.write(frame);
316-
}
308+
videoWriter.write(frame);
317309
if (!FLAGS_no_show) {
318310
cv::imshow("Detection results", frame);
319311
const int key = cv::waitKey(1);

demos/face_detection_mtcnn_demo/cpp_gapi/main.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,7 @@ int main(int argc, char* argv[]) {
179179
cv::Size graphSize{static_cast<int>(frame_size.width / 4), 60};
180180
Presenter presenter(FLAGS_u, frame_size.height - graphSize.height - 10, graphSize);
181181

182-
/** Save output result **/
183-
cv::VideoWriter videoWriter;
184-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
185-
cap->fps(), frame_size)) {
186-
throw std::runtime_error("Can't open video writer");
187-
}
182+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
188183

189184
/** Output Mat for result **/
190185
cv::Mat out_image;
@@ -201,9 +196,7 @@ int main(int argc, char* argv[]) {
201196
metrics.update({}, out_image, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX,
202197
0.65, { 200, 10, 10 }, 2, PerformanceMetrics::MetricTypes::FPS);
203198
}
204-
if (videoWriter.isOpened()) {
205-
videoWriter.write(out_image);
206-
}
199+
videoWriter.write(out_image);
207200
if (!FLAGS_no_show) {
208201
cv::imshow("Face detection mtcnn demo G-API", out_image);
209202
int key = cv::waitKey(1);

demos/gaze_estimation_demo/cpp/main.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,7 @@ int main(int argc, char* argv[]) {
112112
throw std::runtime_error("Can't read an image from the input");
113113
}
114114

115-
cv::VideoWriter videoWriter;
116-
if (!FLAGS_o.empty() &&
117-
!videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), cap->fps(), frame.size()))
118-
{
119-
throw std::runtime_error("Can't open video writer");
120-
}
121-
uint32_t framesProcessed = 0;
115+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
122116
cv::Size graphSize{frame.cols / 4, 60};
123117
Presenter presenter(FLAGS_u, frame.rows - graphSize.height - 10, graphSize);
124118

@@ -148,10 +142,7 @@ int main(int argc, char* argv[]) {
148142
}
149143
}
150144

151-
framesProcessed++;
152-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit)) {
153-
videoWriter.write(frame);
154-
}
145+
videoWriter.write(frame);
155146
if (!FLAGS_no_show) {
156147
cv::imshow(windowName, frame);
157148

demos/gaze_estimation_demo/cpp_gapi/main.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,7 @@ int main(int argc, char *argv[]) {
246246
cv::Size graphSize{static_cast<int>(frame_size.width / 4), 60};
247247
Presenter presenter(FLAGS_u, frame_size.height - graphSize.height - 10, graphSize);
248248

249-
/** Save output result **/
250-
cv::VideoWriter videoWriter;
251-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
252-
cap->fps(), frame_size)) {
253-
throw std::runtime_error("Can't open video writer");
254-
}
249+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
255250

256251
bool isStart = true;
257252
const auto startTime = std::chrono::steady_clock::now();
@@ -317,9 +312,7 @@ int main(int argc, char *argv[]) {
317312
}
318313
}
319314

320-
if (videoWriter.isOpened()) {
321-
videoWriter.write(frame);
322-
}
315+
videoWriter.write(frame);
323316
if (!FLAGS_no_show) {
324317
cv::imshow(windowName, frame);
325318
/** Controls the information being displayed while demo runs **/

demos/gesture_recognition_demo/cpp_gapi/main.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,7 @@ int main(int argc, char *argv[]) {
141141
cv::Size graphSize{static_cast<int>(frame_size.width / 4), 60};
142142
Presenter presenter(FLAGS_u, frame_size.height - graphSize.height - 10, graphSize);
143143

144-
/** Save output result **/
145-
cv::VideoWriter videoWriter;
146-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
147-
cap->fps(), frame_size)) {
148-
throw std::runtime_error("Can't open video writer");
149-
}
144+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
150145

151146
/** Fill labels container from file with classes **/
152147
const auto labels = fill_labels(FLAGS_c);
@@ -177,9 +172,7 @@ int main(int argc, char *argv[]) {
177172
visualizer.show(out_frame, out_detections, out_label_number, current_id, gesture);
178173
gesture = 0;
179174

180-
if (videoWriter.isOpened()) {
181-
videoWriter.write(out_frame);
182-
}
175+
videoWriter.write(out_frame);
183176

184177
/** Controls **/
185178
int key = cv::waitKey(1);

demos/human_pose_estimation_demo/cpp/main.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ int main(int argc, char *argv[]) {
223223
throw std::logic_error("Can't read an image from the input");
224224
}
225225

226-
cv::VideoWriter videoWriter;
226+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
227227

228228
OutputTransform outputTransform = OutputTransform();
229229
cv::Size outputResolution = curr_frame.size();
@@ -236,10 +236,6 @@ int main(int argc, char *argv[]) {
236236
outputTransform = OutputTransform(curr_frame.size(), outputResolution);
237237
outputResolution = outputTransform.computeResolution();
238238
}
239-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
240-
cap->fps(), outputResolution)) {
241-
throw std::runtime_error("Can't open video writer");
242-
}
243239

244240
//------------------------------ Running Human Pose Estimation routines ----------------------------------------------
245241

@@ -302,9 +298,7 @@ int main(int argc, char *argv[]) {
302298
renderMetrics.update(renderingStart);
303299
metrics.update(result->metaData->asRef<ImageMetaData>().timeStamp,
304300
outFrame, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX, 0.65);
305-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit - 1)) {
306-
videoWriter.write(outFrame);
307-
}
301+
videoWriter.write(outFrame);
308302
framesProcessed++;
309303
if (!FLAGS_no_show) {
310304
cv::imshow("Human Pose Estimation Results", outFrame);
@@ -331,9 +325,7 @@ int main(int argc, char *argv[]) {
331325
renderMetrics.update(renderingStart);
332326
metrics.update(result->metaData->asRef<ImageMetaData>().timeStamp,
333327
outFrame, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX, 0.65);
334-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit - 1)) {
335-
videoWriter.write(outFrame);
336-
}
328+
videoWriter.write(outFrame);
337329
if (!FLAGS_no_show) {
338330
cv::imshow("Human Pose Estimation Results", outFrame);
339331
//--- Updating output window

demos/image_processing_demo/cpp/main.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ int main(int argc, char *argv[]) {
187187
bool keepRunning = true;
188188
std::unique_ptr<ResultBase> result;
189189
uint32_t framesProcessed = 0;
190-
cv::VideoWriter videoWriter;
190+
LazyVideoWriter videoWriter{FLAGS_o, cap->fps(), FLAGS_limit};
191191

192192
cv::Size outputResolution;
193193
OutputTransform outputTransform = OutputTransform();
@@ -265,14 +265,6 @@ int main(int argc, char *argv[]) {
265265

266266
outputTransform = OutputTransform(result->asRef<ImageResult>().resultImage.size(), outputResolution);
267267
outputResolution = outputTransform.computeResolution();
268-
269-
// Preparing video writer if needed
270-
if (!FLAGS_o.empty() && !videoWriter.isOpened()) {
271-
if (!videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
272-
cap->fps(), outputResolution)) {
273-
throw std::runtime_error("Can't open video writer");
274-
}
275-
}
276268
}
277269

278270
cv::Mat outFrame = view.renderResultData(result->asRef<ImageResult>(), outputResolution);
@@ -281,9 +273,7 @@ int main(int argc, char *argv[]) {
281273
renderMetrics.update(renderingStart);
282274
metrics.update(result->metaData->asRef<ImageMetaData>().timeStamp,
283275
outFrame, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX, 0.65);
284-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit - 1)) {
285-
videoWriter.write(outFrame);
286-
}
276+
videoWriter.write(outFrame);
287277
if (!FLAGS_no_show) {
288278
view.show(outFrame);
289279

@@ -321,24 +311,14 @@ int main(int argc, char *argv[]) {
321311
}
322312
outputTransform = OutputTransform(result->asRef<ImageResult>().resultImage.size(), outputResolution);
323313
outputResolution = outputTransform.computeResolution();
324-
325-
// Preparing video writer if needed
326-
if (!FLAGS_o.empty() && !videoWriter.isOpened()) {
327-
if (!videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
328-
cap->fps(), outputResolution)) {
329-
throw std::runtime_error("Can't open video writer");
330-
}
331-
}
332314
}
333315

334316
cv::Mat outFrame = view.renderResultData(result->asRef<ImageResult>(), outputResolution);
335317
//--- Showing results and device information
336318
presenter.drawGraphs(outFrame);
337319
metrics.update(result->metaData->asRef<ImageMetaData>().timeStamp,
338320
outFrame, { 10, 22 }, cv::FONT_HERSHEY_COMPLEX, 0.65);
339-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesProcessed <= FLAGS_limit - 1)) {
340-
videoWriter.write(outFrame);
341-
}
321+
videoWriter.write(outFrame);
342322
if (!FLAGS_no_show) {
343323
view.show(outFrame);
344324

demos/interactive_face_detection_demo/cpp/main.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ int main(int argc, char *argv[]) {
8181
visualizer.enableEmotionBar(emotionsDetector.emotionsVec);
8282
}
8383

84-
cv::VideoWriter videoWriter;
85-
if (!FLAGS_o.empty() && !videoWriter.open(FLAGS_o, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
86-
!FLAGS_no_show && FLAGS_fps > 0.0 ? FLAGS_fps : cap->fps(),
87-
frame.size())) {
88-
throw std::runtime_error("Can't open video writer");
89-
}
84+
LazyVideoWriter videoWriter{FLAGS_o, FLAGS_fps > 0.0 ? FLAGS_fps : cap->fps(), FLAGS_limit};
9085

9186
// Detecting all faces on the first frame and reading the next one
9287
faceDetector.submitRequest(frame);
@@ -204,9 +199,7 @@ int main(int argc, char *argv[]) {
204199

205200
timer.finish("total");
206201

207-
if (videoWriter.isOpened() && (FLAGS_limit == 0 || framesCounter <= FLAGS_limit)) {
208-
videoWriter.write(prevFrame);
209-
}
202+
videoWriter.write(prevFrame);
210203

211204
int delay = std::max(1, static_cast<int>(msrate - timer["total"].getLastCallDuration()));
212205
if (!FLAGS_no_show) {

0 commit comments

Comments
 (0)