CsharpTestTxtGenerate.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # D:/workplace/python
  2. # -*- coding: utf-8 -*-
  3. # @File :CsharpTestTxtGenerate.py
  4. # @Author:Guido LuXiaohao
  5. # @Date :2021/11/8
  6. # @Software:PyCharm
  7. """
  8. C# AITestDemo 测试所需txt,输入格式是AI训练平台上的json格式
  9. """
  10. import os
  11. import json
  12. import cv2
  13. import shutil
  14. import numpy as np
  15. def ClassTxtGenerate(source_path, output_path, EnumDetectedObjectType, output_label_dict):
  16. # 包含图片和对应label的txt文件路径(其中txt格式为AI平台训练的json格式)
  17. for i in os.listdir(source_path):
  18. if ".txt" in i:
  19. source_name = i[:-4]
  20. # print(source_name)
  21. txt_path = os.path.join(source_path, source_name + ".txt")
  22. # 防止覆写
  23. if os.path.exists(os.path.join(output_path, source_name + ".txt")):
  24. print("File already exist!")
  25. return
  26. output_txt_file = open(os.path.join(output_path, source_name + ".txt"), "a", encoding="utf-8")
  27. output_info_list = []
  28. with open(txt_path, encoding="utf-8") as f:
  29. output_info = {'EnumDetectedObjectType': None,
  30. 'Label': 0,
  31. 'Confidence': 1.0,
  32. 'BoundingBox': None,
  33. 'Contour': []}
  34. image = cv2.imdecode(np.fromfile(os.path.join(source_path, source_name + ".jpg"), dtype=np.uint8), -1)
  35. image_height, image_width = image.shape[0], image.shape[1]
  36. print(image.shape[0], image.shape[1])
  37. BoundingBox = {'Left': 0, 'Top': 0, 'Right': image_width, 'Bottom': image_height, 'Width': image_width, 'Height': image_height}
  38. label_info = json.loads(f.readline())
  39. # print(label_info)
  40. for result_index in range(len(label_info["ImageResults"])):
  41. labelname = label_info["ImageResults"][result_index]["Conclusion"]["Title"]
  42. # print(labelname)
  43. if labelname not in output_label_dict.keys():
  44. print(labelname)
  45. else:
  46. output_info['EnumDetectedObjectType'] = EnumDetectedObjectType
  47. output_info['Label'] = output_label_dict[labelname]
  48. output_info['BoundingBox'] = BoundingBox
  49. output_info_list.append(output_info)
  50. shutil.copy(os.path.join(source_path, source_name + ".jpg"),
  51. os.path.join(output_path, source_name + ".jpg"))
  52. output_txt_file.write('{}'.format(json.dumps(output_info_list, ensure_ascii=False)))
  53. output_txt_file.close()
  54. def SemanticSegTxtGenerate(source_path, output_path, EnumDetectedObjectType,
  55. crop_organ_label_dict, output_label_dict):
  56. # 包含图片和对应label的txt文件路径(其中txt格式为AI平台训练的json格式)
  57. for source_index, source in enumerate(os.listdir(source_path)):
  58. if ".txt" in source:
  59. source_name = source[:-4]
  60. txt_path = os.path.join(source_path, source_name + ".txt")
  61. # 防止覆写
  62. if os.path.exists(os.path.join(output_path, source_name + ".txt")):
  63. print("File already exist!")
  64. return
  65. output_txt_file = open(os.path.join(output_path, source_name + ".txt"), "a", encoding="utf-8")
  66. output_info_list = []
  67. with open(txt_path, encoding="utf-8") as f:
  68. image = cv2.imdecode(np.fromfile(os.path.join(source_path, source_name + ".jpg"), dtype=np.uint8), -1)
  69. image_height, image_width = image.shape[0], image.shape[1]
  70. label_info = json.loads(f.readline())
  71. max_roi_area = 0
  72. crop_output_info = {'EnumDetectedObjectType': None,
  73. 'Label': -1,
  74. 'Confidence': 1.0,
  75. 'BoundingBox': {'Left': 0, 'Top': 0, 'Right': image_width, 'Bottom': image_height, 'Width': image_width, 'Height': image_height},
  76. 'Contour': [{'x': 0, 'y': 0}, {'x': image_width, 'y': 0}, {'x': image_width, 'y': image_height}, {'x': 0, 'y': image_height}]}
  77. for roi_index, each_roi_label in enumerate(label_info["Rois"]):
  78. output_info = {'EnumDetectedObjectType': None,
  79. 'Label': 0,
  80. 'Confidence': 1.0,
  81. 'BoundingBox': None,
  82. 'Contour': []}
  83. labelname = each_roi_label["Conclusion"]["Title"]
  84. output_info['EnumDetectedObjectType'] = EnumDetectedObjectType
  85. if labelname in output_label_dict.keys():
  86. roi_points = each_roi_label["Points"]
  87. x, y = [], []
  88. roi_contour = []
  89. for point in roi_points:
  90. x.append(int(point.split(",")[0]))
  91. y.append(int(point.split(",")[1]))
  92. roi_contour.append({'x': int(point.split(",")[0]),
  93. 'y': int(point.split(",")[1])})
  94. left, right = min(x), max(x)
  95. top, bottom = min(y), max(y)
  96. roi_boundingbox = {'Left': left, 'Top': top, 'Right': right, 'Bottom': bottom,
  97. 'Width': right - left, 'Height': bottom - top}
  98. # 赋值
  99. output_info['Label'] = output_label_dict[labelname]
  100. output_info['BoundingBox'] = roi_boundingbox
  101. output_info['Contour'] = roi_contour
  102. output_info_list.append(output_info)
  103. elif labelname in crop_organ_label_dict.keys():
  104. roi_points = each_roi_label["Points"]
  105. x, y = [], []
  106. roi_contour = []
  107. for point in roi_points:
  108. x.append(int(point.split(",")[0]))
  109. y.append(int(point.split(",")[1]))
  110. roi_contour.append({'x': int(point.split(",")[0]),
  111. 'y': int(point.split(",")[1])})
  112. left, right = min(x), max(x)
  113. top, bottom = min(y), max(y)
  114. roi_boundingbox = {'Left': left, 'Top': top, 'Right': right, 'Bottom': bottom,
  115. 'Width': right - left, 'Height': bottom - top}
  116. roi_area = (right - left) * (bottom - top)
  117. if roi_area > max_roi_area:
  118. crop_output_info['Label'] = crop_organ_label_dict[labelname]
  119. crop_output_info['BoundingBox'] = roi_boundingbox
  120. crop_output_info['Contour'] = roi_contour
  121. max_roi_area = roi_area
  122. if crop_organ_label_dict != {}:
  123. crop_output_info['EnumDetectedObjectType'] = EnumDetectedObjectType
  124. # 裁切部位放第一位
  125. output_info_list.insert(0, crop_output_info)
  126. shutil.copy(os.path.join(source_path, source_name + ".jpg"),
  127. os.path.join(output_path, source_name + ".jpg"))
  128. output_txt_file.write('{}'.format(json.dumps(output_info_list, ensure_ascii=False)))
  129. output_txt_file.close()
  130. if __name__ == '__main__':
  131. # #######分类用######
  132. # source_path = r"D:\Vinno_Guido\Code\文件\训练格式\TestImages"
  133. # output_path = r"D:\Vinno_Guido\Code\文件\训练格式\output"
  134. # EnumDetectedObjectType = "Scanpart"
  135. # output_label_dict = {"腹部B模式_vinno": 0,
  136. # "乳腺B模式_vinno": 1,
  137. # "超声背景图": 3, "产科B模式_vinno": 3,
  138. # "甲状腺B模式_vinno": 4}
  139. # ClassTxtGenerate(source_path, output_path, EnumDetectedObjectType, output_label_dict)
  140. # #######分割用(无需裁切部位)######
  141. # source_path = r"E:\Vinno\数据\AnnotatedLiverDatas_20210611\本地调试1"
  142. # output_path = r"C:\Users\VINNO\Desktop\Csharptxt"
  143. # EnumDetectedObjectType = r"Organ"
  144. # crop_organ_label_dict = {}
  145. # output_label_dict = {"肝": 1,
  146. # "胆囊胆道": 2,
  147. # "肾脏": 3,
  148. # "脾脏": 4}
  149. # SemanticSegTxtGenerate(source_path, output_path, EnumDetectedObjectType,
  150. # crop_organ_label_dict, output_label_dict)
  151. # #######分割用(需裁切部位)######
  152. # source_path = r"E:\Vinno\数据\AnnotatedLiverDatas_20210611\本地调试1"
  153. # output_path = r"C:\Users\VINNO\Desktop\Csharptxt"
  154. # EnumDetectedObjectType = r"Lesion"
  155. # crop_organ_label_dict = {"肝脏未见明显异常": -1,
  156. # "肝脏有局灶性疾病/区域": -1,
  157. # "脂肪肝声像图改变": -1,
  158. # "肝脏弥漫性病变声像图改变": -1,
  159. # "肝硬化声像图改变": -1,
  160. # "多囊肝声像图改变": -1}
  161. # output_label_dict = {"肝内强回声灶": 1,
  162. # "肝血管瘤声像图改变": 2,
  163. # "肝囊肿": 3,
  164. # "肝癌可能": 4}
  165. # SemanticSegTxtGenerate(source_path, output_path, EnumDetectedObjectType,
  166. # crop_organ_label_dict, output_label_dict)
  167. pass