123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using AIPlatform.Protocol.Entities;
- using AIPlatform.Protocol.LabelData;
- using AIPlatform.Protocol.Model;
- using AIPlatform.Protocol.Utilities;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- namespace aipdev
- {
- /// <summary>
- /// Interaction logic for FolderImageViewer.xaml
- /// </summary>
- public partial class TaskConflictImageViewer : Window
- {
- #region
- private List<ImageLabelResultData> _imageLabelResultDatas = new List<ImageLabelResultData>();
- private string _imageContent;
- private IList<int> _imageIndexList = new List<int>();
- private readonly ObservableCollection<ConflictLabelConclusion> _conclusions = new ObservableCollection<ConflictLabelConclusion>();
- private readonly long _testFolderId;
- private ArchivedImage _currentarchivedImage;
- private int _currentImageIndex;
- private int _colorIndex = 0;
- private int _currentColorIndex;
- #endregion
- public TaskConflictImageViewer(string content, long testFolderId)
- {
- _imageContent = content;
- _testFolderId = testFolderId;
- InitializeComponent();
- Conclusions.ItemsSource = _conclusions;
- Loaded += OnLoaded;
- }
- public TaskConflictImageViewer(List<ImageLabelResultData> imageLabelResultDatas, long testFolderId, int imageIndex)
- {
- InitializeComponent();
- _imageLabelResultDatas = imageLabelResultDatas;
- _testFolderId = testFolderId;
- _currentImageIndex = imageIndex;
- Conclusions.ItemsSource = _conclusions;
- Loaded += OnLoadedImage;
- }
- #region init image
- private async Task LoadImageDataAsync(int imageIndex)
- {
- Waiting.StartWaiting();
- _conclusions.Clear();
- _currentColorIndex = -1;
- _colorIndex = 0;
- var index = _imageIndexList.IndexOf(imageIndex);
- CurrentCaseImageIndex.Text = (index + 1).ToString();
- try
- {
- //获取原始图像
- await GetImageData(imageIndex);
- byte[] imageData = _currentarchivedImage?.ImageData;
- //获取图片标注信息并标注
- var imageLabelResultData = _imageLabelResultDatas.Where(x => x.ImageIndex == imageIndex).FirstOrDefault();
- if (imageLabelResultData != null)
- {
- foreach (var imageLabelResult in imageLabelResultData.ImageLabelResults)
- {
- var annotator = imageLabelResult.Annotator;
- var imageContent = string.Empty;
- var roiContent = string.Empty;
- var labeledResult = imageLabelResult.LabeledResult;
- if (labeledResult != null)
- {
- foreach (var imageResult in labeledResult.ImageResults)
- {
- imageContent += $"({imageResult.Index + 1}) {imageResult.Conclusion.Title} ";
- }
- foreach (var roi in labeledResult.Rois)
- {
- if (roi.Conclusion != null)
- {
- roiContent += $"({roi.Index + 1}) {roi.Conclusion.Title} ";
- }
- }
- if (!string.IsNullOrEmpty(imageContent))
- {
- imageContent = $"图像结论:{imageContent}";
- }
- if (!string.IsNullOrEmpty(roiContent))
- {
- roiContent = $"ROI结论:{roiContent}";
- }
- _conclusions.Add(new ConflictLabelConclusion
- {
- Index = _colorIndex,
- Annotator = annotator,
- ImageContent = imageContent,
- RoiContent = roiContent,
- LabeledResult = labeledResult,
- IsRoi = labeledResult.Rois.Count > 0,
- });
- _colorIndex += 1;
- }
- }
- imageData = SkiaSharpHelper.GetMultiRectangelLabeledImageData(_currentarchivedImage.ImageData, _currentarchivedImage.Width, _currentarchivedImage.Height, _conclusions);
- if (imageData != null)
- {
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = new MemoryStream(imageData);
- bitmapImage.EndInit();
- Image.Source = bitmapImage;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, $"加载图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- PreviousImage.IsEnabled = index > 0;
- NextImage.IsEnabled = index < _imageIndexList.Count - 1;
- Waiting.StopWaiting();
- }
- }
- /// <summary>
- /// Get image according to the index
- /// </summary>
- /// <param name="imageIndex"></param>
- /// <returns></returns>
- private async Task GetImageData(int imageIndex)
- {
- try
- {
- //获取developer folder image
- var developerFolderImage = await DeveloperManager.Shared.GetDeveloperFolderFilebyIndexAsync(_testFolderId, imageIndex);
- if (developerFolderImage == null)
- {
- MessageBox.Show(this, $"获取测试文件夹图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- var developerLabeledUltrasoundFile = await DeveloperManager.Shared.GetDeveloperLabeledUltrasoundFileAsync(developerFolderImage.DeveloperLabeledUltrasoundInfoList[0].DeveloperLabeledUltrasoundFileId);
- if (developerLabeledUltrasoundFile == null)
- {
- MessageBox.Show(this, $"获取测试文件夹标注图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- //CombinationTagIdTextBox.Text = $"标签Id:{labeledUltrasoundFile.CombinationTag.Id}";
- //CombinationTagNameTextBox.Text = $"标签名称:{labeledUltrasoundFile.CombinationTag.Name}";
- _currentarchivedImage = await DeveloperManager.Shared.GetArchivedImageAsync(developerLabeledUltrasoundFile.ArchivedImageId);
- if (_currentarchivedImage == null)
- {
- MessageBox.Show(this, $"获取测试文件夹原始图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- ImageNameTextBox.Text = $"图像名称:{developerLabeledUltrasoundFile.Source.FileName}";
- ImageIdTextBox.Text = $"图像Id:{developerLabeledUltrasoundFile.Id}";
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, $"获取原始图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- #endregion
- #region OnLoaded
- private async void OnLoaded(object sender, RoutedEventArgs e)
- {
- Waiting.StartWaiting();
- _imageLabelResultDatas = await GetDeserializeImageLabelResult(_imageContent);
- if (_imageLabelResultDatas == null && _imageLabelResultDatas.Count == 0)
- {
- MessageBox.Show(Application.Current.MainWindow, $"没有可供查看的图像", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
- return;
- }
- _imageIndexList = _imageLabelResultDatas.Select(x => x.ImageIndex).ToList();
- _currentImageIndex = _imageIndexList.FirstOrDefault();
- TotalCaseImageCount.Text = _imageIndexList.Count.ToString();
- await LoadImageDataAsync(_currentImageIndex);
- }
- private async void OnLoadedImage(object sender, RoutedEventArgs e)
- {
- if (_imageLabelResultDatas == null && _imageLabelResultDatas.Count == 0)
- {
- MessageBox.Show(Application.Current.MainWindow, $"没有可供查看的图像", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
- return;
- }
- _imageIndexList = _imageLabelResultDatas.Select(x => x.ImageIndex).ToList();
- TotalCaseImageCount.Text = _imageIndexList.Count.ToString();
- await LoadImageDataAsync(_currentImageIndex);
- }
- /// <summary>
- /// Deserialize the image content
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- private async Task<List<ImageLabelResultData>> GetDeserializeImageLabelResult(string content)
- {
- List<ImageLabelResultData> imageLabelResultData = await Task.Run(() =>
- {
- return Newtonsoft.Json.JsonConvert.DeserializeObject<List<ImageLabelResultData>>(content);
- });
- return imageLabelResultData;
- }
- #endregion
- private async void OnPreviousImageClick(object sender, RoutedEventArgs e)
- {
- int index = _imageIndexList.IndexOf(_currentImageIndex);
- int imageIndex = _imageIndexList[index - 1];
- await LoadImageDataAsync(imageIndex);
- _currentImageIndex = imageIndex;
- }
- private async void OnNextImageClick(object sender, RoutedEventArgs e)
- {
- int index = _imageIndexList.IndexOf(_currentImageIndex);
- int imageIndex = _imageIndexList[index + 1];
- await LoadImageDataAsync(imageIndex);
- _currentImageIndex = imageIndex;
- }
- private async void LabeledImageIndexTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- if (int.TryParse(LabeledImageIndexTextBox.Text, out var imageIndex))
- {
- imageIndex = imageIndex - 1;
- if (imageIndex >= _imageIndexList.Count || imageIndex < 0)
- {
- MessageBox.Show(this, $"跳转图像页数超出索引", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- return;
- }
- var index = _imageIndexList[imageIndex];
- await LoadImageDataAsync(index);
- _currentImageIndex = imageIndex;
- }
- }
- }
- private void OnResultMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- Waiting.StartWaiting();
- try
- {
- var box = (GroupBox)sender;
- if (box.DataContext is ConflictLabelConclusion conclusion)
- {
- if (conclusion.Index != _currentColorIndex && conclusion.IsRoi)
- {
- _currentColorIndex = conclusion.Index;
- //重新绘制图像
- if (conclusion != null && conclusion.LabeledResult != null)
- {
- var imageData = SkiaSharpHelper.GetSingleRectangelLabeledImageData(_currentarchivedImage.ImageData, _currentarchivedImage.Width, _currentarchivedImage.Height, conclusion.LabeledResult, _currentColorIndex);
- if (imageData != null)
- {
- var bitmapImage = new BitmapImage();
- bitmapImage.BeginInit();
- bitmapImage.StreamSource = new MemoryStream(imageData);
- bitmapImage.EndInit();
- Image.Source = bitmapImage;
- }
- }
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(this, $"加载图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- Waiting.StopWaiting();
- }
- }
- }
- }
|