Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add C++ LazyVideoWriter on_limit callback, introduce throttling
  • Loading branch information
sivanov-work committed Oct 27, 2023
commit b191b116c6aab0db625f98a8b028dff0d13883d8
29 changes: 24 additions & 5 deletions demos/common/cpp/utils/include/utils/ocv_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,39 @@ class InputTransform {
cv::Scalar stdScales;
};

using OnLimitCallbackType = std::function<void(const cv::Mat&, unsigned, unsigned)>;
static void on_limit_reached_default(const cv::Mat&, unsigned frame_index, unsigned limit) {

static const unsigned throttle_interval_frames_count = 500;
if ((frame_index - limit) % throttle_interval_frames_count == 0) {
slog::warn << "VideoWriter will skip writing further frames due to frame limit applied: "
<< limit << "\nIf you want to turn off this limitation please set `-limit 0` as the demo parameter\n"
<< slog::endl;
}
}

class LazyVideoWriter {
cv::VideoWriter writer;
unsigned nwritten;
unsigned nframes;
public:
const std::string filenames;
const double fps;
const unsigned lim;
OnLimitCallbackType on_limit_callback;

LazyVideoWriter(const std::string& filenames, double fps, unsigned lim) :
nwritten{1}, filenames{filenames}, fps{fps}, lim{lim} {}
LazyVideoWriter(const std::string& filenames, double fps, unsigned lim,
OnLimitCallbackType callback = on_limit_reached_default) :
nwritten{1}, nframes{1}, filenames{filenames}, fps{fps}, lim{lim}, on_limit_callback{callback} {}
void write(const cv::Mat& im) {
if (writer.isOpened() && (nwritten < lim || 0 == lim)) {
writer.write(im);
++nwritten;
if (writer.isOpened()) {
if((nwritten < lim || 0 == lim)) {
writer.write(im);
++nwritten;
} else {
on_limit_callback(im, nframes, lim);
}
++nframes;
return;
}
if (!writer.isOpened() && !filenames.empty()) {
Expand Down
13 changes: 5 additions & 8 deletions demos/common/python/video_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
"""

from cv2 import VideoWriter
import logging as log

default_limit_callback_suppressed = False
def on_limit_reached_default(frame, number, limit):
global default_limit_callback_suppressed

if not default_limit_callback_suppressed:
print("WARNING:\n\mVideoWriter will skip writing next frame due to frame limit configured: {0}.".format(limit))
print("If you want to turn off this limitation please set `-limit 0`")
default_limit_callback_suppressed = True
DEFAULT_LIMIT_WARN_THROTTLE_INTERVAL_FRAMES = 500

def on_limit_reached_default(frame, number, limit):
if (number - limit) % DEFAULT_LIMIT_WARN_THROTTLE_INTERVAL_FRAMES == 1:
log.warning("VideoWriter will skip writing next frame due to frame limit applied: {0}.\nIf you want to turn off this limitation please set `-limit 0`".format(limit))

class LazyVideoWriter(VideoWriter):
def __init__(self):
Expand Down