| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using NLog; |
| | | 7 | | using ReFlex.Core.Common.Components; |
| | | 8 | | using ReFlex.Core.Common.Util; |
| | | 9 | | using ReFlex.Core.Networking.Util; |
| | | 10 | | using ReFlex.Core.Tracking.Interfaces; |
| | | 11 | | using ReFlex.Core.Tracking.Util; |
| | | 12 | | using SixLabors.ImageSharp; |
| | | 13 | | using SixLabors.ImageSharp.PixelFormats; |
| | | 14 | | |
| | | 15 | | namespace ReFlex.Sensor.EmulatorModule |
| | | 16 | | { |
| | | 17 | | public class DepthStreamReplayCamera : IDepthCamera |
| | | 18 | | { |
| | 0 | 19 | | private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); |
| | | 20 | | private bool _isStarted; |
| | 22 | 21 | | private int _numConfigs = 0; |
| | | 22 | | private bool _stopRequested; |
| | | 23 | | |
| | | 24 | | private EmulatedPointCloud _pointCloud; |
| | | 25 | | |
| | 0 | 26 | | public string Id => "ReplayCamera"; |
| | 22 | 27 | | public CameraType CameraType { get; } = CameraType.Emulator; |
| | | 28 | | |
| | 0 | 29 | | public string ModelDescription => Id; |
| | | 30 | | |
| | | 31 | | public DepthCameraState State => |
| | 88 | 32 | | _numConfigs == 0 |
| | 88 | 33 | | ? DepthCameraState.Disconnected |
| | 88 | 34 | | : _isStarted |
| | 88 | 35 | | ? DepthCameraState.Streaming |
| | 88 | 36 | | : DepthCameraState.Connected; |
| | | 37 | | |
| | 154 | 38 | | public StreamParameter StreamParameter { get; private set; } |
| | | 39 | | |
| | | 40 | | public event EventHandler<DepthCameraState> StateChanged; |
| | | 41 | | public event EventHandler<DepthCameraFrame> FrameReady; |
| | | 42 | | public event EventHandler<ImageByteArray> DepthImageReady; |
| | | 43 | | |
| | | 44 | | public void Initialize() |
| | 0 | 45 | | { |
| | | 46 | | |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | public void EnableStream(StreamParameter parameter) |
| | 22 | 50 | | { |
| | 22 | 51 | | if (_isStarted) |
| | 0 | 52 | | StopStream(); |
| | | 53 | | |
| | 22 | 54 | | var param = new EmulatorParameters |
| | 22 | 55 | | { |
| | 22 | 56 | | HeightInMeters = 1.6f, |
| | 22 | 57 | | WidthInMeters = 2.4f, |
| | 22 | 58 | | MaxDepthInMeter = 2.1f, |
| | 22 | 59 | | MinDepthInMeter = 0.8f, |
| | 22 | 60 | | PlaneDistanceInMeter = 1.45f, |
| | 22 | 61 | | Radius = (int)(parameter.Width * 0.4) |
| | 22 | 62 | | }; |
| | | 63 | | |
| | 22 | 64 | | StreamParameter = parameter; |
| | | 65 | | |
| | 22 | 66 | | _pointCloud = new EmulatedPointCloud(param); |
| | 22 | 67 | | _pointCloud.InitializePointCloud(parameter); |
| | 22 | 68 | | } |
| | | 69 | | |
| | | 70 | | public IList<StreamParameter> GetPossibleConfigurations() |
| | 22 | 71 | | { |
| | 22 | 72 | | var result = RecordingUtils.RetrieveConfigurations().ToList(); |
| | | 73 | | |
| | 22 | 74 | | _numConfigs = result.Count; |
| | | 75 | | |
| | 22 | 76 | | return result; |
| | 22 | 77 | | } |
| | | 78 | | |
| | | 79 | | public void StartStream() |
| | 22 | 80 | | { |
| | 22 | 81 | | if (StreamParameter == null || State != DepthCameraState.Connected) |
| | 0 | 82 | | { |
| | 0 | 83 | | Logger.Log(LogLevel.Error, $"Cannot start {GetType().Name}: incorrect configuration."); |
| | 0 | 84 | | return; |
| | | 85 | | } |
| | | 86 | | |
| | 22 | 87 | | if (_isStarted) |
| | 0 | 88 | | { |
| | 0 | 89 | | StopStream(); |
| | 0 | 90 | | Task.Delay(TimeSpan.FromMilliseconds(500)); |
| | 0 | 91 | | } |
| | | 92 | | |
| | 22 | 93 | | var delay = 1000 / StreamParameter.Framerate; |
| | | 94 | | |
| | 22 | 95 | | _isStarted = true; |
| | | 96 | | |
| | 22 | 97 | | StateChanged?.Invoke(this, State); |
| | | 98 | | |
| | 44 | 99 | | Task.Run(() => Stream(RecordingUtils.GetFrames(StreamParameter.Name), delay, StreamParameter.Width, StreamPa |
| | 22 | 100 | | } |
| | | 101 | | |
| | | 102 | | public void StopStream() |
| | 24 | 103 | | { |
| | 24 | 104 | | if (!_isStarted) |
| | 2 | 105 | | { |
| | 2 | 106 | | StateChanged?.Invoke(this, State); |
| | 2 | 107 | | return; |
| | | 108 | | } |
| | | 109 | | |
| | 22 | 110 | | _stopRequested = true; |
| | 24 | 111 | | } |
| | | 112 | | |
| | | 113 | | private async Task Stream(IReadOnlyList<string> frames, int delay, int width, int height, DepthImageFormat forma |
| | 22 | 114 | | { |
| | 22 | 115 | | var i = 0; |
| | | 116 | | |
| | 50 | 117 | | while (i < frames?.Count && !_stopRequested) |
| | 28 | 118 | | { |
| | 28 | 119 | | switch (format) |
| | | 120 | | { |
| | | 121 | | case DepthImageFormat.Greyccale48bpp: |
| | 16 | 122 | | ConvertImage<Rgb48>(frames[i], width, height, format); |
| | 16 | 123 | | break; |
| | | 124 | | case DepthImageFormat.Greyscale8bpp: |
| | 4 | 125 | | ConvertImage<L8>(frames[i], width, height, format); |
| | 4 | 126 | | break; |
| | | 127 | | case DepthImageFormat.Rgb24bpp: |
| | | 128 | | default: |
| | 8 | 129 | | ConvertImage<Rgb24>(frames[i], width, height, format); |
| | 8 | 130 | | break; |
| | | 131 | | } |
| | | 132 | | |
| | 28 | 133 | | await Task.Delay(TimeSpan.FromMilliseconds(delay)); |
| | | 134 | | |
| | 28 | 135 | | i = i < frames?.Count - 1 ? i + 1 : 0; |
| | | 136 | | |
| | 28 | 137 | | } |
| | | 138 | | |
| | 22 | 139 | | _isStarted = false; |
| | | 140 | | |
| | 22 | 141 | | StateChanged?.Invoke(this, State); |
| | | 142 | | |
| | 22 | 143 | | _stopRequested = false; |
| | 22 | 144 | | } |
| | | 145 | | |
| | 28 | 146 | | private void ConvertImage<T>(string file, int width, int height, DepthImageFormat format) where T : unmanaged, I |
| | | 147 | | |
| | 28 | 148 | | using var img = Image.Load<T>(file); |
| | | 149 | | |
| | 28 | 150 | | var size = GetSizePerPixel(format); |
| | | 151 | | |
| | 28 | 152 | | var data = new byte[img.Width * img.Height * size]; |
| | 28 | 153 | | img.CopyPixelDataTo(data); |
| | | 154 | | |
| | 28 | 155 | | var args = new ImageByteArray(data, width, height, DepthImageFormatTools.BytesPerChannel(format), DepthImage |
| | | 156 | | |
| | 28 | 157 | | _pointCloud.Reset(); |
| | 28 | 158 | | _pointCloud.UpdateFromGreyScaleImage(args); |
| | | 159 | | |
| | 28 | 160 | | DepthImageReady?.Invoke(this, args); |
| | 28 | 161 | | FrameReady?.Invoke(this, new DepthCameraFrame { Depth = _pointCloud.Points }); |
| | | 162 | | |
| | 56 | 163 | | } |
| | | 164 | | |
| | | 165 | | private static int GetSizePerPixel(DepthImageFormat format) |
| | 28 | 166 | | { |
| | 28 | 167 | | switch (format) |
| | | 168 | | { |
| | | 169 | | case DepthImageFormat.Greyccale48bpp: |
| | 16 | 170 | | return Unsafe.SizeOf<Rgb48>(); |
| | | 171 | | case DepthImageFormat.Rgb24bpp: |
| | 8 | 172 | | return Unsafe.SizeOf<Rgb24>(); |
| | | 173 | | case DepthImageFormat.Greyscale8bpp: |
| | 4 | 174 | | return Unsafe.SizeOf<L8>(); |
| | | 175 | | default: |
| | 0 | 176 | | return Unsafe.SizeOf<Rgba32>(); |
| | | 177 | | } |
| | 28 | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |