using System.CodeDom; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using Hawkeye.VisionBuilder.Workflow; using Hawkeye.VisionBuilder.Workflow.Datatypes; using Hawkeye.VisionBuilder.Workflow.Operations; using OpenCvSharp; using Serilog; using VisionBuilder.UI.Common.Commands; using VisionBuilder.UI.Common.Processing; using VisionBuilder.UI.Common.ViewModel.Classes; namespace VisionBuilder.UI.Recipes.HawkeyeRecipe { public class HawkeyeRecognitionControl: BaseRecognitionControl { private readonly IImageSource _imageSource; private readonly HawkeyeRecognitionSettings _recognitionConfiguration; private readonly ILoadingService _loadingService; private CancellationTokenSource _cts; private Task _loopTask; public HawkeyeRecognitionControl(IImageSource imageSource, HawkeyeRecognitionSettings recognitionConfiguration, ILoadingService loadingService): base(recognitionConfiguration,loadingService) { _imageSource = imageSource; _recognitionConfiguration = recognitionConfiguration; } public override List GetRecipesData() { _loadingService.StartLoading($"Loading recipes for {_recognitionConfiguration.CameraName}..."); try { var files = WorkflowRecipeHelper.ListRecipeFiles(_recognitionConfiguration.RecipeNameFilter); List recipes = new List(); foreach (var file in files) { var workFlow = WorkflowList.LoadJSONFromFile(file); var recipe = new RecipeData { RecipeName = Path.GetFileNameWithoutExtension(file), Image = workFlow.RecipeImage }; recipes.Add(recipe); } return recipes; } finally { _loadingService.StopLoading($"Loading recipes for {_recognitionConfiguration.CameraName}..."); } } private WorkflowList _workflow; protected override void Initialize(RecipeData currentRecipe) { _workflow = WorkflowList.LoadJSONFromFile("..\\Data\\Recipes\\" + currentRecipe.RecipeName + ".jhrcp"); } protected override void WarmUp() { _workflow.ImageSource = new EmptyImageSource(); _workflow.Context = new Context(); _workflow.Context.CancellationToken = CancellationToken.None; _workflow.Execute(); } protected override (Mat originalImage, Mat analysisImage,TimeSpan processingTime, TimeSpan acquisitionTime, string[] errorNames)? ProcessImage(CancellationToken token) { var sw = Stopwatch.StartNew(); _workflow.Context.CancellationToken = token; _workflow.ImageSource = _imageSource; try { _workflow.Execute(); } catch (Exception e) when (token.IsCancellationRequested) { sw.Stop(); return null; } sw.Stop(); var annotated = _workflow.Context.ActiveImage.ImageData.Clone(); var canvas = new OpenCVCanvas(annotated); _workflow.Context.GraphicsElements.ForEach(x => x.Draw(canvas)); var originalImage = _workflow.Context.LastCameraImage.ImageData; var analysisImage = _workflow.Context.ActiveImage.ImageData; var cameraOperation = _workflow.Operations.FirstOrDefault(x => x is GetImageOperation); TimeSpan cameraTime = TimeSpan.FromMilliseconds(0); if (cameraOperation != null) { cameraTime = cameraOperation.ExecutionTime; } string[] errorNames = _workflow.Operations.Where(x => !x.Result).Select(x => x.Label).ToArray(); return (originalImage, analysisImage, sw.Elapsed, cameraTime, errorNames); } } }