22// Licensed under the Apache 2.0 License.
33using OpenCvSharp ;
44using System ;
5- using System . Collections . Generic ;
6- using System . Linq ;
7- using System . Runtime . CompilerServices ;
8- using System . Threading ;
9- using System . Threading . Tasks ;
105using TensorStack . Common . Tensor ;
11- using TensorStack . Common . Video ;
126
137namespace TensorStack . Video
148{
159 public static class Extensions
1610 {
1711 /// <summary>
18- /// Saves the video .
12+ /// Converts Matrix to Tensor .
1913 /// </summary>
20- /// <param name="imageFrames">The image frames.</param>
21- /// <param name="videoFile">The video file.</param>
22- /// <param name="framerate">The framerate.</param>
23- /// <param name="width">The width.</param>
24- /// <param name="height">The height.</param>
25- /// <param name="videoCodec">The video codec.</param>
26- /// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
27- public static async Task SaveAync ( this IAsyncEnumerable < ImageTensor > imageFrames , string videoFile , float framerate , string videoCodec = "mp4v" , int ? widthOverride = null , int ? heightOverride = null , CancellationToken cancellationToken = default )
28- {
29- var videoFrames = imageFrames . AsVideoFrames ( framerate , cancellationToken ) ;
30- await VideoService . WriteVideoStreamAsync ( videoFile , videoFrames , videoCodec , widthOverride , heightOverride , framerate , cancellationToken ) ;
31- }
32-
33-
34- /// <summary>
35- /// Saves the video.
36- /// </summary>
37- /// <param name="imageFrames">The image frames.</param>
38- /// <param name="videoFile">The video file.</param>
39- /// <param name="framerate">The framerate.</param>
40- /// <param name="videoCodec">The video codec.</param>
41- /// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
42- public static async Task SaveAync ( this IAsyncEnumerable < VideoFrame > videoFrames , string videoFile , string videoCodec = "mp4v" , int ? widthOverride = null , int ? heightOverride = null , float ? frameRateOverride = null , CancellationToken cancellationToken = default )
43- {
44- await VideoService . WriteVideoStreamAsync ( videoFile , videoFrames , videoCodec , widthOverride , heightOverride , frameRateOverride , cancellationToken ) ;
45- }
46-
47-
48- /// <summary>
49- /// Saves the video frames processing each frame [Read -> Process -> Write].
50- /// Reads an writes are buffered allowing higher processing thoughput
51- /// </summary>
52- /// <param name="videoInput">The VideoInputStream.</param>
53- /// <param name="videoFile">The video file.</param>
54- /// <param name="frameProcessor">The frame processor.</param>
55- /// <param name="readBuffer">The read buffer (frames).</param>
56- /// <param name="writeBuffer">The write buffer (frames).</param>
57- /// <param name="widthOverride">The output width override.</param>
58- /// <param name="heightOverride">The output height override.</param>
59- /// <param name="frameRateOverride">The output frame rate override.</param>
60- /// <param name="videoCodec">The video codec.</param>
61- /// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
62- public static async Task < VideoInputStream > SaveAync ( this VideoInputStream videoInput , string videoFile , Func < VideoFrame , Task < VideoFrame > > frameProcessor , int readBuffer = 16 , int writeBuffer = 16 , string videoCodec = "mp4v" , int ? widthOverride = null , int ? heightOverride = null , float ? frameRateOverride = null , CancellationToken cancellationToken = default )
63- {
64- var videoFrames = videoInput . GetAsync ( cancellationToken : cancellationToken ) ;
65- await VideoService . WriteVideoStreamAsync ( videoFile , videoFrames , frameProcessor , readBuffer , writeBuffer , videoCodec , widthOverride , heightOverride , frameRateOverride , cancellationToken ) ;
66- return await VideoInputStream . CreateAsync ( videoFile ) ;
67- }
68-
69-
70- /// <summary>
71- /// Converts Mat to Tensor.
72- /// </summary>
73- /// <param name="mat">The mat.</param>
14+ /// <param name="matrix">The matrix.</param>
7415 /// <returns>Tensor<System.Single>.</returns>
75- internal static unsafe ImageTensor ToTensor ( this Mat mat , Size cropSize = default )
16+ internal static unsafe ImageTensor ToTensor ( this Mat matrix , Size cropSize = default )
7617 {
7718 int cropX = 0 ;
7819 int cropY = 0 ;
79- int height = mat . Rows ;
80- int width = mat . Cols ;
20+ int height = matrix . Rows ;
21+ int width = matrix . Cols ;
8122
8223 if ( cropSize != default )
8324 {
@@ -98,14 +39,14 @@ internal static unsafe ImageTensor ToTensor(this Mat mat, Size cropSize = defaul
9839
9940 unsafe
10041 {
101- var source = mat . DataPointer ;
102- int srcStride = mat . Cols * 3 ;
42+ var source = matrix . DataPointer ;
43+ int srcStride = matrix . Cols * 3 ;
10344 int dstStride = height * width ;
10445 for ( int y = 0 ; y < height ; y ++ )
10546 {
10647 for ( int x = 0 ; x < width ; x ++ )
10748 {
108- int srcIndex = ( ( y + cropY ) * mat . Cols + ( x + cropX ) ) * 3 ;
49+ int srcIndex = ( ( y + cropY ) * matrix . Cols + ( x + cropX ) ) * 3 ;
10950 int dstIndex = y * width + x ;
11051
11152 destination [ 0 * dstStride + dstIndex ] = GetFloatValue ( source [ srcIndex + 2 ] ) ; // R
@@ -193,14 +134,5 @@ internal static float GetFloatValue(this byte value)
193134 return value ;
194135 }
195136
196-
197- internal static async IAsyncEnumerable < VideoFrame > AsVideoFrames ( this IAsyncEnumerable < ImageTensor > videoFrames , float frameRate , [ EnumeratorCancellation ] CancellationToken cancellationToken = default )
198- {
199- var frameIndex = 0 ;
200- await foreach ( var videoFrame in videoFrames . WithCancellation ( cancellationToken ) )
201- {
202- yield return new VideoFrame ( frameIndex ++ , videoFrame , frameRate ) ;
203- }
204- }
205137 }
206138}
0 commit comments