TaskConflictImageViewer.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using AIPlatform.Protocol.Entities;
  2. using AIPlatform.Protocol.LabelData;
  3. using AIPlatform.Protocol.Model;
  4. using AIPlatform.Protocol.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media.Imaging;
  15. namespace aipdev
  16. {
  17. /// <summary>
  18. /// Interaction logic for FolderImageViewer.xaml
  19. /// </summary>
  20. public partial class TaskConflictImageViewer : Window
  21. {
  22. #region
  23. private List<ImageLabelResultData> _imageLabelResultDatas = new List<ImageLabelResultData>();
  24. private string _imageContent;
  25. private IList<int> _imageIndexList = new List<int>();
  26. private readonly ObservableCollection<ConflictLabelConclusion> _conclusions = new ObservableCollection<ConflictLabelConclusion>();
  27. private readonly long _testFolderId;
  28. private ArchivedImage _currentarchivedImage;
  29. private int _currentImageIndex;
  30. private int _colorIndex = 0;
  31. private int _currentColorIndex;
  32. #endregion
  33. public TaskConflictImageViewer(string content, long testFolderId)
  34. {
  35. _imageContent = content;
  36. _testFolderId = testFolderId;
  37. InitializeComponent();
  38. Conclusions.ItemsSource = _conclusions;
  39. Loaded += OnLoaded;
  40. }
  41. public TaskConflictImageViewer(List<ImageLabelResultData> imageLabelResultDatas, long testFolderId, int imageIndex)
  42. {
  43. InitializeComponent();
  44. _imageLabelResultDatas = imageLabelResultDatas;
  45. _testFolderId = testFolderId;
  46. _currentImageIndex = imageIndex;
  47. Conclusions.ItemsSource = _conclusions;
  48. Loaded += OnLoadedImage;
  49. }
  50. #region init image
  51. private async Task LoadImageDataAsync(int imageIndex)
  52. {
  53. Waiting.StartWaiting();
  54. _conclusions.Clear();
  55. _currentColorIndex = -1;
  56. _colorIndex = 0;
  57. var index = _imageIndexList.IndexOf(imageIndex);
  58. CurrentCaseImageIndex.Text = (index + 1).ToString();
  59. try
  60. {
  61. //获取原始图像
  62. await GetImageData(imageIndex);
  63. byte[] imageData = _currentarchivedImage?.ImageData;
  64. //获取图片标注信息并标注
  65. var imageLabelResultData = _imageLabelResultDatas.Where(x => x.ImageIndex == imageIndex).FirstOrDefault();
  66. if (imageLabelResultData != null)
  67. {
  68. foreach (var imageLabelResult in imageLabelResultData.ImageLabelResults)
  69. {
  70. var annotator = imageLabelResult.Annotator;
  71. var imageContent = string.Empty;
  72. var roiContent = string.Empty;
  73. var labeledResult = imageLabelResult.LabeledResult;
  74. if (labeledResult != null)
  75. {
  76. foreach (var imageResult in labeledResult.ImageResults)
  77. {
  78. imageContent += $"({imageResult.Index + 1}) {imageResult.Conclusion.Title} ";
  79. }
  80. foreach (var roi in labeledResult.Rois)
  81. {
  82. if (roi.Conclusion != null)
  83. {
  84. roiContent += $"({roi.Index + 1}) {roi.Conclusion.Title} ";
  85. }
  86. }
  87. if (!string.IsNullOrEmpty(imageContent))
  88. {
  89. imageContent = $"图像结论:{imageContent}";
  90. }
  91. if (!string.IsNullOrEmpty(roiContent))
  92. {
  93. roiContent = $"ROI结论:{roiContent}";
  94. }
  95. _conclusions.Add(new ConflictLabelConclusion
  96. {
  97. Index = _colorIndex,
  98. Annotator = annotator,
  99. ImageContent = imageContent,
  100. RoiContent = roiContent,
  101. LabeledResult = labeledResult,
  102. IsRoi = labeledResult.Rois.Count > 0,
  103. });
  104. _colorIndex += 1;
  105. }
  106. }
  107. imageData = SkiaSharpHelper.GetMultiRectangelLabeledImageData(_currentarchivedImage.ImageData, _currentarchivedImage.Width, _currentarchivedImage.Height, _conclusions);
  108. if (imageData != null)
  109. {
  110. var bitmapImage = new BitmapImage();
  111. bitmapImage.BeginInit();
  112. bitmapImage.StreamSource = new MemoryStream(imageData);
  113. bitmapImage.EndInit();
  114. Image.Source = bitmapImage;
  115. }
  116. }
  117. }
  118. catch (Exception ex)
  119. {
  120. MessageBox.Show(this, $"加载图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  121. }
  122. finally
  123. {
  124. PreviousImage.IsEnabled = index > 0;
  125. NextImage.IsEnabled = index < _imageIndexList.Count - 1;
  126. Waiting.StopWaiting();
  127. }
  128. }
  129. /// <summary>
  130. /// Get image according to the index
  131. /// </summary>
  132. /// <param name="imageIndex"></param>
  133. /// <returns></returns>
  134. private async Task GetImageData(int imageIndex)
  135. {
  136. try
  137. {
  138. //获取developer folder image
  139. var developerFolderImage = await DeveloperManager.Shared.GetDeveloperFolderFilebyIndexAsync(_testFolderId, imageIndex);
  140. if (developerFolderImage == null)
  141. {
  142. MessageBox.Show(this, $"获取测试文件夹图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  143. return;
  144. }
  145. var developerLabeledUltrasoundFile = await DeveloperManager.Shared.GetDeveloperLabeledUltrasoundFileAsync(developerFolderImage.DeveloperLabeledUltrasoundInfoList[0].DeveloperLabeledUltrasoundFileId);
  146. if (developerLabeledUltrasoundFile == null)
  147. {
  148. MessageBox.Show(this, $"获取测试文件夹标注图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  149. return;
  150. }
  151. //CombinationTagIdTextBox.Text = $"标签Id:{labeledUltrasoundFile.CombinationTag.Id}";
  152. //CombinationTagNameTextBox.Text = $"标签名称:{labeledUltrasoundFile.CombinationTag.Name}";
  153. _currentarchivedImage = await DeveloperManager.Shared.GetArchivedImageAsync(developerLabeledUltrasoundFile.ArchivedImageId);
  154. if (_currentarchivedImage == null)
  155. {
  156. MessageBox.Show(this, $"获取测试文件夹原始图像失败,Index:{imageIndex}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  157. return;
  158. }
  159. ImageNameTextBox.Text = $"图像名称:{developerLabeledUltrasoundFile.Source.FileName}";
  160. ImageIdTextBox.Text = $"图像Id:{developerLabeledUltrasoundFile.Id}";
  161. }
  162. catch (Exception ex)
  163. {
  164. MessageBox.Show(this, $"获取原始图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  165. }
  166. }
  167. #endregion
  168. #region OnLoaded
  169. private async void OnLoaded(object sender, RoutedEventArgs e)
  170. {
  171. Waiting.StartWaiting();
  172. _imageLabelResultDatas = await GetDeserializeImageLabelResult(_imageContent);
  173. if (_imageLabelResultDatas == null && _imageLabelResultDatas.Count == 0)
  174. {
  175. MessageBox.Show(Application.Current.MainWindow, $"没有可供查看的图像", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  176. return;
  177. }
  178. _imageIndexList = _imageLabelResultDatas.Select(x => x.ImageIndex).ToList();
  179. _currentImageIndex = _imageIndexList.FirstOrDefault();
  180. TotalCaseImageCount.Text = _imageIndexList.Count.ToString();
  181. await LoadImageDataAsync(_currentImageIndex);
  182. }
  183. private async void OnLoadedImage(object sender, RoutedEventArgs e)
  184. {
  185. if (_imageLabelResultDatas == null && _imageLabelResultDatas.Count == 0)
  186. {
  187. MessageBox.Show(Application.Current.MainWindow, $"没有可供查看的图像", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
  188. return;
  189. }
  190. _imageIndexList = _imageLabelResultDatas.Select(x => x.ImageIndex).ToList();
  191. TotalCaseImageCount.Text = _imageIndexList.Count.ToString();
  192. await LoadImageDataAsync(_currentImageIndex);
  193. }
  194. /// <summary>
  195. /// Deserialize the image content
  196. /// </summary>
  197. /// <param name="content"></param>
  198. /// <returns></returns>
  199. private async Task<List<ImageLabelResultData>> GetDeserializeImageLabelResult(string content)
  200. {
  201. List<ImageLabelResultData> imageLabelResultData = await Task.Run(() =>
  202. {
  203. return Newtonsoft.Json.JsonConvert.DeserializeObject<List<ImageLabelResultData>>(content);
  204. });
  205. return imageLabelResultData;
  206. }
  207. #endregion
  208. private async void OnPreviousImageClick(object sender, RoutedEventArgs e)
  209. {
  210. int index = _imageIndexList.IndexOf(_currentImageIndex);
  211. int imageIndex = _imageIndexList[index - 1];
  212. await LoadImageDataAsync(imageIndex);
  213. _currentImageIndex = imageIndex;
  214. }
  215. private async void OnNextImageClick(object sender, RoutedEventArgs e)
  216. {
  217. int index = _imageIndexList.IndexOf(_currentImageIndex);
  218. int imageIndex = _imageIndexList[index + 1];
  219. await LoadImageDataAsync(imageIndex);
  220. _currentImageIndex = imageIndex;
  221. }
  222. private async void LabeledImageIndexTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
  223. {
  224. if (e.Key == Key.Enter)
  225. {
  226. if (int.TryParse(LabeledImageIndexTextBox.Text, out var imageIndex))
  227. {
  228. imageIndex = imageIndex - 1;
  229. if (imageIndex >= _imageIndexList.Count || imageIndex < 0)
  230. {
  231. MessageBox.Show(this, $"跳转图像页数超出索引", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  232. return;
  233. }
  234. var index = _imageIndexList[imageIndex];
  235. await LoadImageDataAsync(index);
  236. _currentImageIndex = imageIndex;
  237. }
  238. }
  239. }
  240. private void OnResultMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  241. {
  242. Waiting.StartWaiting();
  243. try
  244. {
  245. var box = (GroupBox)sender;
  246. if (box.DataContext is ConflictLabelConclusion conclusion)
  247. {
  248. if (conclusion.Index != _currentColorIndex && conclusion.IsRoi)
  249. {
  250. _currentColorIndex = conclusion.Index;
  251. //重新绘制图像
  252. if (conclusion != null && conclusion.LabeledResult != null)
  253. {
  254. var imageData = SkiaSharpHelper.GetSingleRectangelLabeledImageData(_currentarchivedImage.ImageData, _currentarchivedImage.Width, _currentarchivedImage.Height, conclusion.LabeledResult, _currentColorIndex);
  255. if (imageData != null)
  256. {
  257. var bitmapImage = new BitmapImage();
  258. bitmapImage.BeginInit();
  259. bitmapImage.StreamSource = new MemoryStream(imageData);
  260. bitmapImage.EndInit();
  261. Image.Source = bitmapImage;
  262. }
  263. }
  264. }
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. MessageBox.Show(this, $"加载图像失败:{ex.Translate()}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  270. }
  271. finally
  272. {
  273. Waiting.StopWaiting();
  274. }
  275. }
  276. }
  277. }