101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
using OpenCvSharp;
|
|
using System.Drawing;
|
|
using Ninject;
|
|
using VisionBuilder.UI.Common.RecipeProcessing;
|
|
|
|
namespace Hawkeye.VisionBuilder.UI.Sources.Emulation
|
|
{
|
|
public class EmulationCameraImageSource:IImageSource, IInitializable
|
|
{
|
|
private readonly EmulationSettings _settings;
|
|
private readonly string _name;
|
|
private string _currentFolder;
|
|
private string[] _selectedFiles;
|
|
|
|
public EmulationCameraImageSource(EmulationSettings settings)
|
|
{
|
|
_settings = settings;
|
|
}
|
|
|
|
public string GetName()
|
|
{
|
|
return _name;
|
|
}
|
|
|
|
public async Task<Mat> GetImage(CancellationToken token)
|
|
{
|
|
if (_selectedFiles.Length == 0) return null;
|
|
var id = (_index) % _selectedFiles.Length;
|
|
if (id == 0)
|
|
{
|
|
if (_index > 0)
|
|
OnLoopOver();
|
|
if (_settings.SingleRun && _index > 0)
|
|
{
|
|
// infinite sleep
|
|
await Task.Delay(Timeout.Infinite, token);
|
|
}
|
|
Console.WriteLine("starting new emulation cycle");
|
|
await Task.Delay(_settings.CycleDelay, token);
|
|
}
|
|
else
|
|
{
|
|
await Task.Delay(_settings.PerImageDelay, token);
|
|
}
|
|
|
|
var path = _selectedFiles[id];
|
|
_index++;
|
|
|
|
|
|
|
|
Console.WriteLine($"Getting image {path}");
|
|
|
|
return Cv2.ImRead(path);
|
|
}
|
|
public void SetFolder(string imagesFolder)
|
|
{
|
|
try
|
|
{
|
|
_currentFolder = imagesFolder;
|
|
var path = Path.Combine(@"..\Data\Emulation", imagesFolder);
|
|
_selectedFiles = Directory.GetFiles(path, "*.bmp")
|
|
.Concat(Directory.GetFiles(path, "*.png"))
|
|
.Concat(Directory.GetFiles(path, "*.jpg"))
|
|
.Where(x => !Path.GetFileNameWithoutExtension(x).Contains("_analysis"))
|
|
.ToArray();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
public void SetFolderAbsolute(string imagesFolder)
|
|
{
|
|
try
|
|
{
|
|
_currentFolder = imagesFolder;
|
|
var path = imagesFolder;
|
|
_selectedFiles = Directory.GetFiles(path, "*.bmp")
|
|
.Concat(Directory.GetFiles(path, "*.png"))
|
|
.Concat(Directory.GetFiles(path, "*.jpg"))
|
|
.Where(x => !Path.GetFileNameWithoutExtension(x).Contains("_analysis"))
|
|
.ToArray();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
public event Action OnLoopOver = delegate { };
|
|
private int _index = 0;
|
|
|
|
|
|
public void Initialize()
|
|
{
|
|
if(string.IsNullOrEmpty(_settings.EmulationPath)) return;
|
|
SetFolderAbsolute(_settings.EmulationPath);
|
|
}
|
|
}
|
|
}
|