forked from TensorStack-AI/TensorStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoTensor.cs
More file actions
121 lines (103 loc) · 3.81 KB
/
VideoTensor.cs
File metadata and controls
121 lines (103 loc) · 3.81 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
// Copyright (c) TensorStack. All rights reserved.
// Licensed under the Apache 2.0 License.
using System;
using System.Collections.Generic;
namespace TensorStack.Common.Tensor
{
/// <summary>
/// Class to handle video in Tensor format
/// Implements the <see cref="Tensor{float}" />
/// </summary>
/// <seealso cref="Tensor{float}" />
public class VideoTensor : Tensor<float>
{
protected float _frameRate;
protected TimeSpan _duration;
/// <summary>
/// Initializes a new instance of the <see cref="VideoTensor"/> class.
/// </summary>
/// <param name="tensor">The tensor.</param>
/// <param name="frameRate">The source video frame rate.</param>
public VideoTensor(Tensor<float> tensor, float frameRate)
: base(tensor.Memory, tensor.Dimensions)
{
_frameRate = frameRate;
_duration = TimeSpan.FromSeconds(Frames / FrameRate);
ThrowIfInvalid();
}
/// <summary>
/// Gets the frame rate.
/// </summary>
public float FrameRate => _frameRate;
/// <summary>
/// Gets the number of video frames.
/// </summary>
public int Frames => Dimensions[0];
/// <summary>
/// Gets the video channel count (RGB, RGBA etc).
/// </summary>
public int Channels => Dimensions[1];
/// <summary>
/// Gets the video height.
/// </summary>
public int Height => Dimensions[2];
/// <summary>
/// Gets the video width.
/// </summary>
public int Width => Dimensions[3];
/// <summary>
/// Gets the duration.
/// </summary>
public TimeSpan Duration => _duration;
/// <summary>
/// Normalizes the tensor values from range -1 to 1 to 0 to 1.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
public void NormalizeZeroToOne()
{
this.NormalizeOneOneToZeroOne();
}
/// <summary>
/// Normalizes the tensor values from range 0 to 1 to -1 to 1.
/// </summary>
/// <param name="imageTensor">The image tensor.</param>
public void NormalizeOneToOne()
{
this.NormalizeZeroOneToOneOne();
}
/// <summary>
/// Gets the frames.
/// </summary>
/// <returns>IEnumerable<ImageTensor>.</returns>
public IEnumerable<ImageTensor> GetFrames()
{
foreach (var imageTensor in this.Split())
{
yield return new ImageTensor(imageTensor);
}
}
/// <summary>
/// Called when Tensor data has changed
/// </summary>
protected override void OnTensorDataChanged()
{
_duration = TimeSpan.FromSeconds(Frames / FrameRate);
base.OnTensorDataChanged();
}
/// <summary>
/// Throws if Dimensions are invalid.
/// </summary>
/// <param name="dimensions">The dimensions.</param>
/// <param name="framerate">The framerate.</param>
protected void ThrowIfInvalid()
{
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(FrameRate, 0, nameof(FrameRate));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Frames, 1, nameof(Frames));
ArgumentOutOfRangeException.ThrowIfEqual(Channels, 2, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfLessThan(Channels, 1, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfGreaterThan(Channels, 4, nameof(Channels));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Height, 0, nameof(Height));
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(Width, 0, nameof(Width));
}
}
}