123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using WatchDog.WPF;
- namespace YoloV5ImageLableWpfApp
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private static FileSelectedVM _fileSelectedVM = new FileSelectedVM();
- public FileSelected fileSelected = new FileSelected(_fileSelectedVM);
- public MainWindow()
- {
- InitializeComponent();
- this.Closed += MainWindow_Closed;
- //this.image.Source = new ImageSource();
- image.Source = new BitmapImage(new Uri(@"C:\Users\77411\Desktop\images2\进线区_2FA南侧_球机-18-17-57.bmp", UriKind.Absolute));
- this.canvas.MouseEnter += Canvas_MouseEnter;
- this.canvas.MouseMove += Canvas_MouseMove;
- this.canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
- this.canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
- this.KeyDown += MainWindow_KeyDown;
-
- }
- private void MainWindow_KeyDown(object sender, KeyEventArgs e)
- {
- if (Keyboard.IsKeyDown(Key.LeftCtrl) &&Keyboard.IsKeyDown(Key.Z) )
- {
- //只要当下同时按下的键中包含LeftCtrl、H和C,就会进入
- canvas.Children.Remove(rct);
- isStarted = false; isComplete = false;
- }
- }
- private void Canvas_MouseEnter(object sender, MouseEventArgs e)
- {
- }
- private void Canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (isStarted && !isComplete)
- {
- endPoint = e.GetPosition(canvas);
- if (endPoint.X - startPoint.X>0)
- {
- rct.Width = endPoint.X - startPoint.X;
- }
- else
- {
- rct.Width =Math.Abs( endPoint.X - startPoint.X);
- Canvas.SetLeft(rct, endPoint.X);
- }
- if (endPoint.Y - startPoint.Y>0)
- {
- rct.Height = Math.Abs(endPoint.Y - startPoint.Y);
- }
- else
- {
- rct.Height = Math.Abs(endPoint.Y - startPoint.Y);
- Canvas.SetTop(rct, endPoint.Y);
- }
- }
-
- }
- Rectangle rct ;
- Point startPoint;
- Point endPoint;
- bool isComplete = false;
- bool isStarted = false;
- private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- isComplete = true;
- isStarted = false;
- }
- private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- isStarted = true;
- startPoint = e.GetPosition(canvas);
- rct = new Rectangle();
- Canvas.SetLeft(rct, startPoint.X);
- Canvas.SetTop(rct, startPoint.Y);
- rct.StrokeThickness = 1;
- rct.Stroke = new SolidColorBrush(Color.FromRgb(255,255,255));
- this.canvas.Children.Add(rct);
- }
- private void MainWindow_Closed(object? sender, EventArgs e)
- {
- System.Environment.Exit(0);
- }
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- fileSelected.ShowDialog();
- this.filePath.Text = _fileSelectedVM.CurrentDir;
- var dirInfos = Directory.GetFiles(_fileSelectedVM.CurrentDir);
- this.imageList.Items.Clear();
- foreach (var item in dirInfos)
- {
- Regex reg = new Regex("(bmp|jpg|png|tif|gif|pcx|tga|exif|fpx|svg|psd|cdr|pcd|dxf|ufo|eps|ai|raw|WMF|webp|jpeg)");//这是搜索匹配0-9的数字
- if (reg.IsMatch(item))
- {
-
- this.imageList.Items.Add(new MyFile(System.IO.Path.GetFileName(item), item));
- }
- }
- }
- private void Button_Click_1(object sender, RoutedEventArgs e)
- {
- isComplete = false;
- try
- {
-
- var w = rct.ActualWidth / canvas.ActualWidth;
- var h = rct.ActualWidth / canvas.ActualHeight;
- var x = Canvas.GetLeft(rct) / canvas.ActualWidth + (w/ 2);
- var y = Canvas.GetTop(rct) / canvas.ActualHeight +(h/2);
- using (StreamWriter sw = new StreamWriter(_fileSelectedVM.CurrentDir+ "\\"+System.IO.Path.GetFileNameWithoutExtension( this.filePath.Text)+".txt"))
- {
- sw.WriteLine($"{this.lableName.Text} {x} {y} {w} {h}");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- private void imageList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- try
- {
- canvas.Children.Remove(rct);
- ListBox listBox = sender as ListBox;
- MyFile myFile = listBox.SelectedItem as MyFile;
- image.Source = new BitmapImage(new Uri(myFile.Path, UriKind.Absolute));
- filePath.Text = myFile.Path;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
-
- }
- }
- }
|