tiff_change_pic_reshape.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import os
  2. import json
  3. from itertools import combinations
  4. import numpy as np
  5. import skimage
  6. from scipy.ndimage import zoom
  7. from tqdm import tqdm
  8. import imageio.v3 as iio
  9. def to_int(x):
  10. return tuple(map(int, x))
  11. def convert_labelme_to_custom_json(labelme_json_path):
  12. try:
  13. with open(labelme_json_path, 'r', encoding='utf-8') as f:
  14. labelme_data = json.load(f)
  15. if 'imageHeight' not in labelme_data:
  16. raise KeyError("Missing 'imageHeight' in JSON")
  17. if 'imageWidth' not in labelme_data:
  18. raise KeyError("Missing 'imageWidth' in JSON")
  19. image_height = labelme_data['imageHeight']
  20. image_width = labelme_data['imageWidth']
  21. lines = []
  22. for shape in labelme_data.get('shapes', []):
  23. if shape.get('label') == 'dseam1':
  24. if len(shape.get('points', [])) != 2:
  25. print(f"Warning: Skipping invalid line in {labelme_json_path}")
  26. continue
  27. start_point = shape['points'][0]
  28. end_point = shape['points'][1]
  29. lines.append([start_point, end_point])
  30. if not lines:
  31. raise ValueError("No line segments found in the JSON")
  32. custom_json_data = {
  33. "image_id": os.path.splitext(os.path.basename(labelme_json_path))[0],
  34. "lines": lines
  35. }
  36. return custom_json_data
  37. except Exception as e:
  38. print(f"Error reading {labelme_json_path}: {e}")
  39. return None
  40. def find_image_file(json_path, image_extensions=('.jpg', '.jpeg', '.png', '.bmp', '.tiff')):
  41. base_name = os.path.splitext(json_path)[0]
  42. for ext in image_extensions:
  43. image_path = f"{base_name}{ext}"
  44. if os.path.exists(image_path):
  45. return image_path
  46. return None
  47. def generate_heatmap_data(image, lines):
  48. try:
  49. im_rescale = (512, 512)
  50. heatmap_scale = (128, 128)
  51. fy, fx = heatmap_scale[1] / image.shape[0], heatmap_scale[0] / image.shape[1]
  52. jmap = np.zeros((1,) + heatmap_scale, dtype=np.float32)
  53. joff = np.zeros((1, 2) + heatmap_scale, dtype=np.float32)
  54. lmap = np.zeros(heatmap_scale, dtype=np.float32)
  55. lines[:, :, 0] = np.clip(lines[:, :, 0] * fx, 0, heatmap_scale[0] - 1e-4)
  56. lines[:, :, 1] = np.clip(lines[:, :, 1] * fy, 0, heatmap_scale[1] - 1e-4)
  57. lines = lines[:, :, ::-1]
  58. junc = []
  59. jids = {}
  60. def jid(jun):
  61. jun = tuple(jun[:2])
  62. if jun in jids:
  63. return jids[jun]
  64. jids[jun] = len(junc)
  65. junc.append(np.array(jun + (0,)))
  66. return len(junc) - 1
  67. lnid = []
  68. lpos, lneg = [], []
  69. for v0, v1 in lines:
  70. lnid.append((jid(v0), jid(v1)))
  71. lpos.append([junc[jid(v0)], junc[jid(v1)]])
  72. vint0, vint1 = to_int(v0), to_int(v1)
  73. jmap[0][vint0] = 1
  74. jmap[0][vint1] = 1
  75. rr, cc, value = skimage.draw.line_aa(*to_int(v0), *to_int(v1))
  76. lmap[rr, cc] = np.maximum(lmap[rr, cc], value)
  77. for v in junc:
  78. vint = to_int(v[:2])
  79. joff[0, :, vint[0], vint[1]] = v[:2] - vint - 0.5
  80. llmap = zoom(lmap, [0.5, 0.5])
  81. lineset = set([frozenset(l) for l in lnid])
  82. for i0, i1 in combinations(range(len(junc)), 2):
  83. if frozenset([i0, i1]) not in lineset:
  84. v0, v1 = junc[i0], junc[i1]
  85. vint0, vint1 = to_int(v0[:2] / 2), to_int(v1[:2] / 2)
  86. rr, cc, value = skimage.draw.line_aa(*vint0, *vint1)
  87. lneg.append([v0, v1, i0, i1, np.average(np.minimum(value, llmap[rr, cc]))])
  88. if len(lneg) == 0:
  89. for i0, i1 in combinations(range(len(junc)), 2):
  90. v0, v1 = junc[i0], junc[i1]
  91. vint0, vint1 = to_int(v0[:2] / 2), to_int(v1[:2] / 2)
  92. rr, cc, value = skimage.draw.line_aa(*vint0, *vint1)
  93. simulated_value = np.random.uniform(0.01, 0.1)
  94. lneg.append([v0, v1, i0, i1, simulated_value])
  95. lneg.sort(key=lambda l: -l[-1])
  96. junc = np.array(junc, dtype=np.float32)
  97. Lpos = np.array(lnid, dtype=np.int32)
  98. Lneg = np.array([l[2:4] for l in lneg][:4000], dtype=np.int32)
  99. lpos = np.array(lpos, dtype=np.float32)
  100. lneg = np.array([l[:2] for l in lneg[:2000]], dtype=np.float32)
  101. return {
  102. "junc_map": {"name": "junc_map", "shape": list(jmap.shape), "content": jmap.tolist()},
  103. "junc_offset": {"name": "junc_offset", "shape": list(joff.shape), "content": joff.tolist()},
  104. "line_map": {"name": "line_map", "shape": list(lmap.shape), "content": lmap.tolist()},
  105. "junc_coords": {"name": "junc_coords", "shape": list(junc.shape), "content": junc.tolist()},
  106. "line_pos_idx": {"name": "line_pos_idx", "shape": list(Lpos.shape), "content": Lpos.tolist()},
  107. "line_neg_idx": {"name": "line_neg_idx", "shape": list(Lneg.shape), "content": Lneg.tolist()},
  108. "line_pos_coords": {"name": "line_pos_coords", "shape": list(lpos.shape), "content": lpos.tolist()},
  109. "line_neg_coords": {"name": "line_neg_coords", "shape": list(lneg.shape), "content": lneg.tolist()}
  110. }
  111. except Exception as e:
  112. print(f"Error generating heatmap data: {e}")
  113. raise
  114. def batch_process(labelme_json_dir, output_dir):
  115. json_files = [f for f in os.listdir(labelme_json_dir) if f.endswith('.json')]
  116. os.makedirs(output_dir, exist_ok=True)
  117. for filename in tqdm(json_files, desc="Processing JSON files"):
  118. labelme_json_path = os.path.join(labelme_json_dir, filename)
  119. try:
  120. custom_json_data = convert_labelme_to_custom_json(labelme_json_path)
  121. if custom_json_data is None:
  122. continue
  123. image_path = find_image_file(labelme_json_path)
  124. if not image_path or not os.path.exists(image_path):
  125. print(f"Image not found: {labelme_json_path}")
  126. continue
  127. image = iio.imread(image_path)
  128. if image.ndim == 2:
  129. image = image[:, :, np.newaxis]
  130. elif image.ndim == 3 and image.shape[2] == 4:
  131. image = image[:, :, :3] # Drop alpha if needed
  132. # 保存缩放后 TIFF(保持原始位深度)
  133. zoom_factors = [512 / image.shape[0], 512 / image.shape[1], 1]
  134. resized_image = zoom(image, zoom_factors, order=0)
  135. resized_image = resized_image.astype(image.dtype)
  136. image_save_path = os.path.join(output_dir, os.path.splitext(filename)[0] + '.tiff')
  137. iio.imwrite(image_save_path, resized_image)
  138. # heatmap 处理
  139. lines = np.array(custom_json_data['lines']).reshape(-1, 2, 2)
  140. wires_data = generate_heatmap_data(image, lines)
  141. # 输出 JSON 文件
  142. custom_json_data.pop("lines", None)
  143. custom_json_data.update({
  144. "boxes": [],
  145. "segmentations": [],
  146. "wires": [wires_data]
  147. })
  148. output_json_path = os.path.join(output_dir, os.path.splitext(filename)[0] + '.json')
  149. with open(output_json_path, 'w', encoding='utf-8') as f:
  150. json.dump(custom_json_data, f, separators=(',', ':'))
  151. except Exception as e:
  152. print(f"Error processing {labelme_json_path}: {e}")
  153. if __name__ == "__main__":
  154. labelme_json_dir = r"\\192.168.50.222\share\zyh\512\init\paomo"
  155. custom_json_output_dir = r"\\192.168.50.222\share\zyh\512\init\target"
  156. batch_process(labelme_json_dir, custom_json_output_dir)