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
Next Next commit
Add python video wrap
  • Loading branch information
sivanov-work committed Oct 25, 2023
commit 9754c9c77f3771da9931b242fb150c799e9dfcdf
11 changes: 5 additions & 6 deletions demos/classification_demo/python/classification_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import monitors
from images_capture import open_images_capture
from helpers import resolution, log_latency_per_stage
from video_writer import LazyVideoWriter

log.basicConfig(format='[ %(levelname)s ] %(message)s', level=log.DEBUG, stream=sys.stdout)

Expand Down Expand Up @@ -187,7 +188,7 @@ def main():
render_metrics = PerformanceMetrics()
presenter = None
output_transform = None
video_writer = cv2.VideoWriter()
video_writer = LazyVideoWriter()
ESC_KEY = 27
key = -1
while True:
Expand All @@ -209,8 +210,7 @@ def main():
render_metrics.update(rendering_start_time)
metrics.update(start_time, frame)

if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
video_writer.write(frame)
next_frame_id_to_show += 1

if not args.no_show:
Expand Down Expand Up @@ -239,7 +239,7 @@ def main():
presenter = monitors.Presenter(args.utilization_monitors, 55,
(round(output_resolution[0] / 4), round(output_resolution[1] / 8)))
if args.output and not video_writer.open(args.output, cv2.VideoWriter_fourcc(*'MJPG'),
cap.fps(), output_resolution):
cap.fps(), args.output_limit, output_resolution):
raise RuntimeError("Can't open video writer")
# Submit for inference
async_pipeline.submit_data(frame, next_frame_id, {'frame': frame, 'start_time': start_time})
Expand Down Expand Up @@ -270,8 +270,7 @@ def main():
render_metrics.update(rendering_start_time)
metrics.update(start_time, frame)

if video_writer.isOpened() and (args.output_limit <= 0 or next_frame_id_to_show <= args.output_limit-1):
video_writer.write(frame)
video_writer.write(frame)

if not args.no_show:
cv2.imshow('Classification Results', frame)
Expand Down
49 changes: 49 additions & 0 deletions demos/common/python/video_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Copyright (C) 2020-2023 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from cv2 import VideoWriter

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


class LazyVideoWriter(VideoWriter):
def __init__(self):
VideoWriter.__init__(self)

def open(self, output, fourcc, fps, output_limit, output_resolution, on_limit_callback = on_limit_reached_default):
super().open(output, fourcc, fps, output_resolution)

self.output_limit = output_limit
self.frames_processed = 0
self.on_limit_callback = on_limit_callback

def write(self, frame):
if super().isOpened():
if self.output_limit <= 0 or self.frames_processed <= self.output_limit:
super().write(frame)
else:
self.on_limit_callback(frame, self.frames_processed, self.output_limit)

self.frames_processed += 1