Agregado de ExtracionTag

This commit is contained in:
Miguel 2024-06-04 22:27:35 +02:00
parent 0410c87e93
commit 84e7ac1c28
8 changed files with 309 additions and 19 deletions

View File

@ -68,9 +68,11 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.0" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.WPF" Version="2.0.0-rc2" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.77" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.122" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Ookii.Dialogs.Wpf" Version="5.0.1" />
<PackageReference Include="Tesseract" Version="5.2.0" />
<PackageReference Include="Tesseract.Drawing" Version="5.2.0" />
</ItemGroup>
<ItemGroup>
@ -116,6 +118,15 @@
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<None Update="Tesseract\eng.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tesseract\ita.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Tesseract\spa.traineddata">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
<UserControl x:Class="CtrEditor.ObjetosSim.Extraccion_Datos.ucExtraccionTag"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:vm="clr-namespace:CtrEditor.ObjetosSim.Extraccion_Datos"
mc:Ignorable="d">
<UserControl.DataContext>
<vm:osExtraccionTag/>
</UserControl.DataContext>
<Canvas>
<Rectangle x:Name="Transporte"
Width="{Binding Ancho, Converter={StaticResource MeterToPixelConverter}}"
Height="{Binding Alto, Converter={StaticResource MeterToPixelConverter}}" Opacity="0.1" Fill="Green"
Stroke="Black"
>
<Rectangle.RenderTransform>
<RotateTransform Angle="{Binding Angulo}"/>
</Rectangle.RenderTransform>
</Rectangle>
</Canvas>
</UserControl>

View File

@ -0,0 +1,247 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using CtrEditor.Siemens;
using CtrEditor.Simulacion;
using System.Windows.Input;
using System.IO;
using System.Windows.Media.Imaging;
using Tesseract;
namespace CtrEditor.ObjetosSim.Extraccion_Datos
{
/// <summary>
/// Interaction logic for ucExtraccionTag.xaml
/// </summary>
///
public partial class osExtraccionTag : osBase, IosBase
{
private osBase _osMotor = null;
private simTransporte SimGeometria;
public static string NombreClase()
{
return "Extraccion Tags";
}
private string nombre = NombreClase();
public override string Nombre
{
get => nombre;
set => SetProperty(ref nombre, value);
}
[ObservableProperty]
bool extraer;
public override void TopChanged(float value)
{
base.TopChanged(value);
ResetTimer();
}
public override void LeftChanged(float value)
{
base.LeftChanged(value);
ResetTimer();
}
partial void OnExtraerChanged(bool value)
{
ResetTimer();
}
[ObservableProperty]
public float ancho;
partial void OnAnchoChanged(float value)
{
ResetTimer();
}
[ObservableProperty]
public float alto;
partial void OnAltoChanged(float value)
{
ResetTimer();
}
public override void OnTimerAfterMovement() {
if (Extraer)
CaptureImageAreaAndDoOCR();
}
[ObservableProperty]
public int n_Repeat_X;
[ObservableProperty]
public int n_Repeat_Y;
[ObservableProperty]
public float offset_Repeat_X;
[ObservableProperty]
public float offset_Repeat_Y;
[ObservableProperty]
public float angulo;
[ObservableProperty]
string tag_extract;
[ObservableProperty]
string clase;
[ObservableProperty]
string tag_name;
public osExtraccionTag()
{
Ancho = 1;
Alto = 1;
Angulo = 0;
}
private void ShowPreviewWindow(Stream imageStream)
{
// Create a new window for preview
Window previewWindow = new Window
{
Title = "Preview Captured Image",
Width = 500,
Height = 500,
Content = new Image
{
Source = BitmapFrame.Create(imageStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad),
Stretch = Stretch.Uniform
}
};
previewWindow.ShowDialog();
}
private void CaptureImageAreaAndDoOCR()
{
if (_mainViewModel?.MainCanvas.Children[0] is Image imagenDeFondo)
{
// Asegurarse de que la imagen origen está disponible
if (imagenDeFondo.Source is BitmapSource bitmapSource)
{
// Obtener los DPI de la imagen original
float originalDpiX = (float) bitmapSource.DpiX;
float originalDpiY = (float) bitmapSource.DpiY;
// Estándar DPI en el que el Canvas renderiza la imagen
float canvasDpiX = 96; // WPF usually renders at 96 DPI
float canvasDpiY = 96;
// Calcular el ratio de escala entre el Canvas y la imagen original
float scaleFactorX = originalDpiX / canvasDpiX;
float scaleFactorY = originalDpiY / canvasDpiY;
// Ajustar las coordenadas de recorte en función del ratio de escala
int x = (int)MeterToPixels(Left * scaleFactorX);
int y = (int)MeterToPixels(Top * scaleFactorY);
int width = (int)MeterToPixels(Ancho * scaleFactorX);
int height = (int)MeterToPixels(Alto * scaleFactorY);
// Validar y ajustar el tamaño del recorte para que se mantenga dentro de los límites de la imagen
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x + width > bitmapSource.PixelWidth) width = bitmapSource.PixelWidth - x;
if (y + height > bitmapSource.PixelHeight) height = bitmapSource.PixelHeight - y;
// Recortar el área deseada utilizando las coordenadas ajustadas
CroppedBitmap croppedBitmap = new CroppedBitmap(bitmapSource, new Int32Rect(x, y, width, height));
// Codificar el bitmap recortado a un MemoryStream
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(croppedBitmap));
using (MemoryStream memoryStream = new MemoryStream())
{
encoder.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
// Mostrar la imagen recortada en una ventana de previsualización
// ShowPreviewWindow(memoryStream);
// Cargar la imagen en Tesseract desde el MemoryStream
using (var img = Pix.LoadFromMemory(memoryStream.ToArray()))
using (var engine = new TesseractEngine(@"./Tesseract", "eng", EngineMode.Default))
{
var result = engine.Process(img);
Tag_extract = result.GetText();
}
}
}
}
}
public override void ucLoaded()
{
// El UserControl ya se ha cargado y podemos obtener las coordenadas para
// crear el objeto de simulacion
base.ucLoaded();
}
}
public partial class ucExtraccionTag : UserControl, IDataContainer
{
public osBase? Datos { get; set; }
public ucExtraccionTag()
{
InitializeComponent();
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
Datos?.ucLoaded();
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
Datos?.ucUnLoaded();
}
public void Resize(float width, float height)
{
if (Datos is osExtraccionTag datos)
{
datos.Ancho = PixelToMeter.Instance.calc.PixelsToMeters(width);
datos.Alto = PixelToMeter.Instance.calc.PixelsToMeters(height);
}
}
public void Move(float LeftPixels, float TopPixels)
{
if (Datos != null)
{
Datos.Left = PixelToMeter.Instance.calc.PixelsToMeters(LeftPixels);
Datos.Top = PixelToMeter.Instance.calc.PixelsToMeters(TopPixels);
}
}
public void Rotate(float Angle)
{
if (Datos != null)
if (Datos is osExtraccionTag datos)
datos.Angulo = Angle;
}
public void Highlight(bool State) { }
public int ZIndex()
{
return 1;
}
}
}

View File

@ -13,6 +13,8 @@ using System.Diagnostics;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.ComponentModel;
using System.Configuration;
using System.Windows.Threading;
namespace CtrEditor.ObjetosSim
{
@ -59,7 +61,8 @@ namespace CtrEditor.ObjetosSim
public virtual string Nombre { get; set; } = "osBase";
[JsonIgnore]
private Storyboard _storyboard;
private System.Threading.Timer timer = null;
[ObservableProperty]
private float left;
@ -96,6 +99,26 @@ namespace CtrEditor.ObjetosSim
public virtual void TopChanged(float value) { }
public virtual void TopChanging(float oldValue, float newValue) { }
private async void TimerCallback(object state)
{
await Task.Delay(500); // Esperar 0.5 segundos antes de ejecutar la función
Application.Current.Dispatcher.Invoke(() =>
{
// Realiza tus cambios en la interfaz de usuario aquí
OnTimerAfterMovement();
});
}
public void ResetTimer()
{
if (timer == null)
timer = new System.Threading.Timer(TimerCallback, null, Timeout.Infinite, Timeout.Infinite);
timer.Change(500, Timeout.Infinite);
}
public virtual void OnTimerAfterMovement() { }
/// <summary>
/// Usado para saber cuando un objeto fue creado manualmente o por algun generador
/// Mas adelante la idea es poder eliminar o tratar de manera diferente a estos objetos

View File

@ -7,23 +7,6 @@
xmlns:vm="clr-namespace:CtrEditor.ObjetosSim"
mc:Ignorable="d">
<UserControl.Resources>
<!-- Define the VisualBrush for the conveyor belt pattern -->
<VisualBrush x:Key="BeltBrush" TileMode="Tile" Viewport="0,0,20,10" ViewportUnits="Absolute" Viewbox="0,0,20,10" ViewboxUnits="Absolute">
<VisualBrush.Transform>
<TransformGroup>
<TranslateTransform/>
</TransformGroup>
</VisualBrush.Transform>
<VisualBrush.Visual>
<Canvas>
<Rectangle Fill="Gray" Width="10" Height="10"/>
<Rectangle Fill="DarkGray" Width="10" Height="10" Canvas.Left="10"/>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</UserControl.Resources>
<UserControl.DataContext>
<vm:osBasicExample Ancho="2"/>
</UserControl.DataContext>

BIN
Tesseract/eng.traineddata Normal file

Binary file not shown.

BIN
Tesseract/ita.traineddata Normal file

Binary file not shown.

BIN
Tesseract/spa.traineddata Normal file

Binary file not shown.