forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
169 lines (154 loc) · 6.79 KB
/
Extensions.cs
File metadata and controls
169 lines (154 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TensorStack.Common.Tensor;
using TensorStack.Common.Video;
namespace TensorStack.Video
{
public static class Extensions
{
/// <summary>
/// Saves the video.
/// </summary>
/// <param name="imageFrames">The image frames.</param>
/// <param name="videoFile">The video file.</param>
/// <param name="framerate">The framerate.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="videoCodec">The video codec.</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
public static async Task SaveAync(this IAsyncEnumerable<ImageTensor> imageFrames, string videoFile, float framerate, string videoCodec = "mp4v", CancellationToken cancellationToken = default)
{
var fourcc = VideoWriter.FourCC(videoCodec);
var firstFrame = await imageFrames.FirstAsync(cancellationToken);
var fullSequence = VideoService.CacheFirstAndIterate(firstFrame, imageFrames, cancellationToken);
var frameSize = new Size(firstFrame.Width, firstFrame.Height);
await VideoService.WriteVideoFramesAsync(fullSequence, videoFile, frameSize, framerate, fourcc, cancellationToken);
}
/// <summary>
/// Saves the video.
/// </summary>
/// <param name="imageFrames">The image frames.</param>
/// <param name="videoFile">The video file.</param>
/// <param name="framerate">The framerate.</param>
/// <param name="videoCodec">The video codec.</param>
/// <param name="cancellationToken">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
public static async Task SaveAync(this IAsyncEnumerable<VideoFrame> imageFrames, string videoFile, float? framerate = default, string videoCodec = "mp4v", CancellationToken cancellationToken = default)
{
var fourcc = VideoWriter.FourCC(videoCodec);
var firstFrame = await imageFrames.FirstAsync(cancellationToken);
var fullSequence = VideoService.CacheFirstAndIterate(firstFrame, imageFrames, cancellationToken);
var frameSize = new Size(firstFrame.Width, firstFrame.Height);
var outframeRate = framerate ?? firstFrame.SourceFrameRate;
await VideoService.WriteVideoFramesAsync(fullSequence, videoFile, frameSize, outframeRate, fourcc, cancellationToken);
}
/// <summary>
/// Converts Mat to Tensor.
/// </summary>
/// <param name="mat">The mat.</param>
/// <returns>Tensor<System.Single>.</returns>
internal static ImageTensor ToTensor(this Mat mat, Size cropSize = default)
{
var cropX = 0;
var cropY = 0;
var height = mat.Rows;
var width = mat.Cols;
if (width == cropSize.Width)
{
cropY = (height - cropSize.Height) / 2;
height = cropSize.Height;
}
else if (height == cropSize.Height)
{
cropX = (width - cropSize.Width) / 2;
width = cropSize.Width;
}
var tensor = new ImageTensor([1, 4, height, width]);
unsafe
{
byte* dataPtr = mat.DataPointer;
for (int y = 0; y < tensor.Height; y++)
{
for (int x = 0; x < tensor.Width; x++)
{
int pixelIndex = ((y + cropY) * mat.Cols + (x + cropX)) * 3;
tensor[0, 0, y, x] = GetFloatValue(dataPtr[pixelIndex + 2]); // R
tensor[0, 1, y, x] = GetFloatValue(dataPtr[pixelIndex + 1]); // G
tensor[0, 2, y, x] = GetFloatValue(dataPtr[pixelIndex + 0]); // B
tensor[0, 3, y, x] = GetFloatValue(byte.MaxValue); // A
}
}
}
return tensor;
}
/// <summary>
/// Converts Tensor to Mat.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <returns>Mat.</returns>
internal static Mat ToMat(this Tensor<float> tensor)
{
var channels = tensor.Dimensions[1];
var height = tensor.Dimensions[2];
var width = tensor.Dimensions[3];
var mat = new Mat(height, width, MatType.CV_8UC3);
unsafe
{
byte* dataPtr = mat.DataPointer;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int pixelIndex = (y * width + x) * 3;
if (channels == 1)
{
var grayscale = GetByteValue(tensor[0, 0, y, x]);
dataPtr[pixelIndex + 2] = grayscale; // R
dataPtr[pixelIndex + 1] = grayscale; // G
dataPtr[pixelIndex + 0] = grayscale; // B
}
else
{
dataPtr[pixelIndex + 2] = GetByteValue(tensor[0, 0, y, x]); // R
dataPtr[pixelIndex + 1] = GetByteValue(tensor[0, 1, y, x]); // G
dataPtr[pixelIndex + 0] = GetByteValue(tensor[0, 2, y, x]); // B
}
}
}
}
return mat;
}
/// <summary>
/// Gets the normalized byte value.
/// </summary>
/// <param name="value">The value.</param>
internal static byte GetByteValue(this float value)
{
return (byte)Math.Clamp((value + 1.0f) * 0.5f * 255f, 0, 255);
}
/// <summary>
/// Gets the normalized float value.
/// </summary>
/// <param name="value">The value.</param>
internal static float GetFloatValue(this byte value)
{
return (value / 255f) * 2.0f - 1.0f;
}
/// <summary>
/// Null if zero.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.Nullable<System.Int32>.</returns>
internal static int? NullIfZero(this int? value)
{
if (value.HasValue && value.Value == 0)
return null;
return value;
}
}
}