predict_file_generate.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. """
  2. 将预测结果,装换成所需要的pred格式文件,
  3. 为了调用方便,pred格式文件和gt格式文文件一致
  4. 分为分类,检测,语义分割,三种不同的格式
  5. 后续添加实例分割的格式,直接添加函数即可
  6. """
  7. """
  8. 将所有c#返回的DetectedObject格式,根据模型类型(分类,检测,语义分割)转换成所需的格式,如下:
  9. 分类:
  10. [{'Label': 0, 'Confidence': 1.0}]
  11. []
  12. 检测:
  13. [{'Label': 1, 'Confidence': 1.0, 'BoundingBox':[2, 3, 4, 5]}]
  14. [{'Label': 1, 'Confidence': 1.0, 'BoundingBox':[2, 3, 4, 5]},{'Label': 2, 'Confidence': 1.0, 'BoundingBox':[2, 3, 4, 5]}]
  15. [{'Label': 0, 'Confidence': 1.0, 'BoundingBox':[0, 0, 0, 0]}]
  16. []
  17. 语义分割:
  18. [{'Label': 0, 'Confidence': 1.0, 'Image_size':[w, h], 'Contour': []}]
  19. [{'Label': 1, 'Confidence': 1.0, 'Image_size':[w, h], 'Contours':[ [ [1,2],[3,4] ] , [ [5,6],[7,8] ] ]}]
  20. [{'Label': 1, 'Confidence': 1.0, 'Image_size':[w, h], 'Contours':[ [ [1,2],[3,4] ] ]}]
  21. [{'Label': 1, 'Confidence': 1.0, ''Image_size':[w, h], Contours':[ [ [1,2],[3,4] ] ]},{'Label': 2, 'Confidence': 1.0, 'Contours':[ [ [1,2],[3,4] ] ]}]
  22. []
  23. """
  24. import json
  25. def predfilegenerate_classification(idetectedobject, image_size, class_id_map):
  26. """
  27. 设置所需的gt格式,返回需要的json格式
  28. 适用于: 分类模型
  29. :param idetectedobject: 模型调用结果
  30. :param image_size: 图像尺寸
  31. :param class_id_map: 用于label改变的dict
  32. :return: 返回分类所需的文件格式
  33. """
  34. output_info_list = []
  35. length = idetectedobject.Length
  36. if length == 0:
  37. output_info = {'Label': 0,
  38. 'Confidence': 1.0}
  39. output_info_list.append(output_info)
  40. if length == 1:
  41. for j in range(length):
  42. output_info = {'Label': 0,
  43. 'Confidence': 1.0}
  44. label = class_id_map[idetectedobject[j].Label]
  45. confidence = idetectedobject[j].Confidence
  46. output_info['Label'] = label
  47. output_info['Confidence'] = confidence
  48. output_info_list.append(output_info)
  49. # 保证输出的output_info_list
  50. if len(output_info_list) == 0:
  51. print('label_info为:{} 的图像,无法匹配任何所需的imageresults和rois的title,该图像作废'.format(idetectedobject))
  52. return json.dumps([], ensure_ascii=False)
  53. else:
  54. return json.dumps(output_info_list, ensure_ascii=False)
  55. def predfilegenerate_object_detection(idetectedobject, image_size, class_id_map):
  56. """
  57. 设置所需的gt格式,返回需要的json格式
  58. 适用于: 检测模型
  59. :param idetectedobject:
  60. :param image_size:
  61. :param class_id_map:
  62. :return: 返回检测所需的文件格式
  63. """
  64. output_info_list = []
  65. length = idetectedobject.Length
  66. if length == 0:
  67. output_info = {'Label': 0,
  68. 'Confidence': 1.0,
  69. 'BoundingBox': [0, 0, 0, 0]}
  70. output_info_list.append(output_info)
  71. else:
  72. for j in range(length):
  73. output_info = {'Label': 0,
  74. 'Confidence': 1.0,
  75. 'BoundingBox': [0, 0, 0, 0]}
  76. label = class_id_map[idetectedobject[j].Label]
  77. confidence = idetectedobject[j].Confidence
  78. rect_BoundingBox = idetectedobject[j].BoundingBox
  79. rect_top = rect_BoundingBox.Top
  80. rect_left = rect_BoundingBox.Left
  81. rect_right = rect_BoundingBox.Right
  82. rect_bottom = rect_BoundingBox.Bottom
  83. output_info['Label'] = label
  84. output_info['BoundingBox'] = [rect_left, rect_top, rect_right, rect_bottom]
  85. output_info['Confidence'] = confidence
  86. output_info_list.append(output_info)
  87. # 保证输出的output_info_list
  88. if len(output_info_list) == 0:
  89. print('label_info为:{} 的图像,无法匹配任何所需的imageresults和rois的title,该图像作废'.format(idetectedobject))
  90. return json.dumps([], ensure_ascii=False)
  91. else:
  92. return json.dumps(output_info_list, ensure_ascii=False)
  93. def predfilegenerate_semantic_segmentation(idetectedobject, image_size, class_id_map):
  94. """
  95. 设置所需的gt格式,返回需要的json格式
  96. 适用于: 语义分割
  97. :param label_info:
  98. :param needed_imageresults_dict:
  99. :param needed_rois_dict:
  100. :return:
  101. """
  102. output_info_list = []
  103. length = idetectedobject.Length
  104. if length == 0:
  105. output_info = {'Label': 0,
  106. 'Image_size': image_size,
  107. 'Confidence': 1.0,
  108. 'Contours': []}
  109. output_info_list.append(output_info)
  110. for j in range(length):
  111. output_info = {'Label': 0,
  112. 'Image_size': image_size,
  113. 'Confidence': 1.0,
  114. 'Contours': []}
  115. label = class_id_map[idetectedobject[j].Label]
  116. confidence = idetectedobject[j].Confidence
  117. rect_BoundingBox = idetectedobject[j].BoundingBox
  118. contours = idetectedobject[j].Contours
  119. for z in range(contours.Length):
  120. each_contour = []
  121. for zi in range(contours[z].Length):
  122. each_contour.append([contours[z][zi].X, contours[z][zi].Y])
  123. output_info['Contours'].append(each_contour)
  124. output_info['Label'] = label
  125. output_info['Confidence'] = confidence
  126. output_info_list.append(output_info)
  127. # 保证输出的output_info_list
  128. if len(output_info_list) == 0:
  129. print('label_info为:{} 的图像,无法匹配任何所需的imageresults和rois的title,该图像作废'.format(idetectedobject))
  130. return json.dumps([], ensure_ascii=False)
  131. else:
  132. return json.dumps(output_info_list, ensure_ascii=False)
  133. def predfilegenerate_semantic_segmentation_myocardial(idetectedobject, image_size, class_id_map):
  134. """
  135. 设置所需的gt格式,返回需要的json格式
  136. 适用于: 心肌分割
  137. 原因是:心肌分割只输出一个轮廓,并且label为0,自动+1,label设为1
  138. :param label_info:
  139. :param needed_imageresults_dict:
  140. :param needed_rois_dict:
  141. :return:
  142. """
  143. output_info_list = []
  144. length = idetectedobject.Length
  145. if length == 0:
  146. output_info = {'Label': 0,
  147. 'Image_size': image_size,
  148. 'Confidence': 1.0,
  149. 'Contours': []}
  150. output_info_list.append(output_info)
  151. for j in range(length):
  152. output_info = {'Label': 0,
  153. 'Image_size': image_size,
  154. 'Confidence': 1.0,
  155. 'Contours': []}
  156. label = class_id_map[idetectedobject[j].Label + 1]
  157. confidence = idetectedobject[j].Confidence
  158. rect_BoundingBox = idetectedobject[j].BoundingBox
  159. contours = idetectedobject[j].Contours
  160. for z in range(contours.Length):
  161. each_contour = []
  162. for zi in range(contours[z].Length):
  163. each_contour.append([contours[z][zi].X, contours[z][zi].Y])
  164. output_info['Contours'].append(each_contour)
  165. output_info['Label'] = label
  166. output_info['Confidence'] = confidence
  167. output_info_list.append(output_info)
  168. # 保证输出的output_info_list
  169. if len(output_info_list) == 0:
  170. print('label_info为:{} 的图像,无法匹配任何所需的imageresults和rois的title,该图像作废'.format(idetectedobject))
  171. return json.dumps([], ensure_ascii=False)
  172. else:
  173. return json.dumps(output_info_list, ensure_ascii=False)