Skip to content

Commit

Permalink
Ensure frame size is cached per subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
glopesdev committed Jul 14, 2023
1 parent 177e84a commit 5eb4cd3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 4 additions & 6 deletions Bonsai.Vision/VideoWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ namespace Bonsai.Vision
[Description("Writes a sequence of images into a compressed AVI file.")]
public class VideoWriter : FileSink<IplImage, VideoWriterDisposable>
{
Size writerFrameSize;
static readonly object SyncRoot = new object();
static readonly object SyncRoot = new();

/// <summary>
/// Gets or sets a value specifying the four-character code of the codec
Expand Down Expand Up @@ -54,11 +53,10 @@ protected override VideoWriterDisposable CreateWriter(string fileName, IplImage
var frameSize = FrameSize.Width > 0 && FrameSize.Height > 0 ? FrameSize : input.Size;
var fourCCText = FourCC;
var fourCC = fourCCText.Length != 4 ? 0 : OpenCV.Net.VideoWriter.FourCC(fourCCText[0], fourCCText[1], fourCCText[2], fourCCText[3]);
writerFrameSize = frameSize;
lock (SyncRoot)
{
var writer = new OpenCV.Net.VideoWriter(fileName, fourCC, FrameRate, frameSize, input.Channels > 1);
return new VideoWriterDisposable(writer, Disposable.Create(() =>
return new VideoWriterDisposable(writer, frameSize, Disposable.Create(() =>
{
lock (SyncRoot)
{
Expand All @@ -79,9 +77,9 @@ protected override VideoWriterDisposable CreateWriter(string fileName, IplImage
/// </param>
protected override void Write(VideoWriterDisposable writer, IplImage input)
{
if (input.Width != writerFrameSize.Width || input.Height != writerFrameSize.Height)
if (input.Width != writer.FrameSize.Width || input.Height != writer.FrameSize.Height)
{
var resized = new IplImage(new Size(writerFrameSize.Width, writerFrameSize.Height), input.Depth, input.Channels);
var resized = new IplImage(writer.FrameSize, input.Depth, input.Channels);
CV.Resize(input, resized, ResizeInterpolation);
input = resized;
}
Expand Down
11 changes: 9 additions & 2 deletions Bonsai.Vision/VideoWriterDisposable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reactive.Disposables;
using System.Threading;
using OpenCV.Net;

namespace Bonsai.Vision
{
Expand All @@ -11,16 +12,22 @@ public sealed class VideoWriterDisposable : ICancelable, IDisposable
{
IDisposable resource;

internal VideoWriterDisposable(OpenCV.Net.VideoWriter writer, IDisposable disposable)
internal VideoWriterDisposable(OpenCV.Net.VideoWriter writer, Size frameSize, IDisposable disposable)
{
Writer = writer ?? throw new ArgumentNullException(nameof(writer));
FrameSize = frameSize;
resource = disposable;
}

/// <summary>
/// Gets the reference to the disposable video writer instance.
/// </summary>
public OpenCV.Net.VideoWriter Writer { get; private set; }
public OpenCV.Net.VideoWriter Writer { get; }

/// <summary>
/// Gets the size of individual video frames.
/// </summary>
public Size FrameSize { get; }

/// <summary>
/// Gets a value indicating whether the video writer has been disposed.
Expand Down

0 comments on commit 5eb4cd3

Please sign in to comment.