more_specimens.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. from copy import deepcopy
  2. import glog
  3. import os, cv2, json
  4. import numpy as np
  5. def traverse_folder(folder_path, file_list, end=".jpg", all_floder = False):
  6. for item in os.listdir(folder_path):
  7. item_path = os.path.join(folder_path, item)
  8. if os.path.isfile(item_path):
  9. if item_path.endswith(".jpg") or item_path.endswith(".png"):
  10. file_list.append(item_path)
  11. elif os.path.isdir(item_path):
  12. if all_floder is True:
  13. traverse_folder(item_path, file_list)
  14. def mergeNewImage(img0, etc0, img1, etc1, img2, etc2, img3, etc3):
  15. img = np.zeros((960, 1280, 3), dtype=np.uint8)
  16. img[0: 480, 0: 640, 0:3] = img0
  17. img[0: 480, 640: 1280, :] = img1
  18. img[480: 960, 0: 640, :] = img2
  19. img[480: 960, 640: 1280, :] = img3
  20. if not etc1 is None:
  21. for point in etc1["points"]:
  22. point[0] = point[0] + 640
  23. if not etc2 is None:
  24. for point in etc2["points"]:
  25. point[1] = point[1] + 480
  26. if not etc3 is None:
  27. for point in etc3["points"]:
  28. point[0] = point[0] + 640
  29. point[1] = point[1] + 480
  30. return img, etc0, etc1, etc2, etc3
  31. def convertImg(file, output_path):
  32. etc = {}
  33. ext = os.path.splitext(file) # 将文件名路径与后缀名分开
  34. file_name = ext[0].split("/")[-1]
  35. save_file = output_path + file_name
  36. json_file = ext[0] + ".json"
  37. if os.path.isfile(json_file):
  38. f = open(json_file, 'r')
  39. content = f.read()
  40. etc = json.loads(content)
  41. else:
  42. return
  43. img = cv2.imread(file)
  44. img_lf = img[0: 480, 0: 640, 0:]
  45. img_rf = img[0: 480, 640: 1280, :]
  46. img_lr = img[480: 960, 0: 640, :]
  47. img_rr = img[480: 960, 640: 1280, :]
  48. etc_sig_old = [None, None, None, None]
  49. for points in etc['shapes']:
  50. index = int(points["points"][0][0] // 640) | int(int(points["points"][0][1] // 480) << 1)
  51. for point in points["points"]:
  52. point[0] = point[0] % 640
  53. point[1] = point[1] % 480
  54. etc_sig_old[index] = points
  55. etc['shapes'].clear()
  56. etc_sig = deepcopy(etc_sig_old)
  57. etc_sig_last = [None, None, None, None]
  58. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_rf, etc_sig[1],
  59. img_lf, etc_sig[0],
  60. img_lr, etc_sig[2],
  61. img_rr, etc_sig[3])
  62. for last_etc in etc_sig_last:
  63. if not last_etc is None:
  64. etc['shapes'].append(last_etc)
  65. etc['imagePath'] = file_name + "_convert_test_a.jpg"
  66. cv2.imwrite(save_file + "_convert_test_a.jpg", merge_img)
  67. with open(save_file + "_convert_test_a.json", "w") as w_f:
  68. json.dump(etc, w_f, indent=4)
  69. etc['shapes'].clear()
  70. etc_sig = deepcopy(etc_sig_old)
  71. etc_sig_last = [None, None, None, None]
  72. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_lr, etc_sig[2],
  73. img_rf, etc_sig[1],
  74. img_lf, etc_sig[0],
  75. img_rr, etc_sig[3])
  76. for last_etc in etc_sig_last:
  77. if not last_etc is None:
  78. etc['shapes'].append(last_etc)
  79. etc['imagePath'] = file_name + "_convert_test_b.jpg"
  80. cv2.imwrite(save_file + "_convert_test_b.jpg", merge_img)
  81. with open(save_file + "_convert_test_b.json", "w") as w_f:
  82. json.dump(etc, w_f, indent=4)
  83. etc['shapes'].clear()
  84. etc_sig = deepcopy(etc_sig_old)
  85. etc_sig_last = [None, None, None, None]
  86. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_rr, etc_sig[3],
  87. img_rf, etc_sig[1],
  88. img_lr, etc_sig[2],
  89. img_lf, etc_sig[0])
  90. for last_etc in etc_sig_last:
  91. if not last_etc is None:
  92. etc['shapes'].append(last_etc)
  93. etc['imagePath'] = file_name + "_convert_test_c.jpg"
  94. cv2.imwrite(save_file + "_convert_test_c.jpg", merge_img)
  95. with open(save_file + "_convert_test_c.json", "w") as w_f:
  96. json.dump(etc, w_f, indent=4)
  97. etc['shapes'].clear()
  98. etc_sig = deepcopy(etc_sig_old)
  99. etc_sig_last = [None, None, None, None]
  100. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_lf, etc_sig[0],
  101. img_lr, etc_sig[2],
  102. img_rf, etc_sig[1],
  103. img_rr, etc_sig[3])
  104. for last_etc in etc_sig_last:
  105. if not last_etc is None:
  106. etc['shapes'].append(last_etc)
  107. etc['imagePath'] = file_name + "_convert_test_d.jpg"
  108. cv2.imwrite(save_file + "_convert_test_d.jpg", merge_img)
  109. with open(save_file + "_convert_test_d.json", "w") as w_f:
  110. json.dump(etc, w_f, indent=4)
  111. etc['shapes'].clear()
  112. etc_sig = deepcopy(etc_sig_old)
  113. etc_sig_last = [None, None, None, None]
  114. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_lf, etc_sig[0],
  115. img_rr, etc_sig[3],
  116. img_lr, etc_sig[2],
  117. img_rf, etc_sig[1])
  118. for last_etc in etc_sig_last:
  119. if not last_etc is None:
  120. etc['shapes'].append(last_etc)
  121. etc['imagePath'] = file_name + "_convert_test_e.jpg"
  122. cv2.imwrite(save_file + "_convert_test_e.jpg", merge_img)
  123. with open(save_file + "_convert_test_e.json", "w") as w_f:
  124. json.dump(etc, w_f, indent=4)
  125. etc['shapes'].clear()
  126. etc_sig = deepcopy(etc_sig_old)
  127. etc_sig_last = [None, None, None, None]
  128. merge_img, etc_sig_last[0], etc_sig_last[1], etc_sig_last[2], etc_sig_last[3] = mergeNewImage(img_lf, etc_sig[0],
  129. img_rf, etc_sig[1],
  130. img_rr, etc_sig[3],
  131. img_lr, etc_sig[2])
  132. for last_etc in etc_sig_last:
  133. if not last_etc is None:
  134. etc['shapes'].append(last_etc)
  135. etc['imagePath'] = file_name + "_convert_test_f.jpg"
  136. cv2.imwrite(save_file + "_convert_test_f.jpg", merge_img)
  137. with open(save_file + "_convert_test_f.json", "w") as w_f:
  138. json.dump(etc, w_f, indent=4)
  139. if __name__ == '__main__':
  140. path = "/home/zx/DataAcquisition-Python/Modules/moreSpecimens/image"
  141. output_path = path + "/output/"
  142. if not os.path.exists(output_path):
  143. os.mkdir(output_path)
  144. file_list = []
  145. # 加载配置
  146. if os.path.isdir(path):
  147. traverse_folder(path, file_list)
  148. else:
  149. print("it's a special file(socket,FIFO,device file)")
  150. exit(0)
  151. for file in file_list:
  152. print(file)
  153. if file.endswith(".png"):
  154. mat = cv2.imread(file)
  155. # os.remove(file)
  156. ext = os.path.splitext(file)
  157. file = ext[0] + ".jpg"
  158. cv2.imwrite(file, mat)
  159. convertImg(file, output_path)