cut_roi_save.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import argparse
  2. import datetime
  3. from tqdm import tqdm
  4. import json
  5. import os
  6. import cv2
  7. import uuid
  8. def read_jsonfile(self, path):
  9. with open(path, "r", encoding='utf-8') as f:
  10. return json.load(f)
  11. def image_cut_save(path, left, upper, right, lower, save_path, pix):
  12. """
  13. 所截区域图片保存
  14. :param pix: 将图片height缩放的pix
  15. :param path: 图片路径
  16. :param left: 区块左上角位置的像素点离图片左边界的距离
  17. :param upper:区块左上角位置的像素点离图片上边界的距离
  18. :param right:区块右下角位置的像素点离图片左边界的距离
  19. :param lower:区块右下角位置的像素点离图片上边界的距离
  20. 故需满足:lower > upper、right > left
  21. :param save_path: 所截图片保存位置
  22. """
  23. img = cv2.imread(path) # 打开图像
  24. cropped = img[upper:lower, left:right]
  25. if pix != 0:
  26. height, width = cropped.shape[:2] # 原始分辨率
  27. if height != 0 and width != 0:
  28. while height < pix or width < pix:
  29. if height <= width:
  30. # 等比例缩放到pix
  31. scale = pix / height
  32. # 缩放后分辨率
  33. height = pix
  34. width = int(width * scale)
  35. else:
  36. # 等比例缩放到pix
  37. scale = pix / width
  38. # 缩放后分辨率
  39. width = pix
  40. height = int(height * scale)
  41. img = cv2.resize(cropped, (width, height))
  42. # 保存截取的图片
  43. cv2.imwrite(save_path, img)
  44. else:
  45. cv2.imwrite(save_path, cropped)
  46. def single_img_cut(json_path, label, img_save_path, cnt, pix):
  47. with open(json_path, "r", encoding='utf-8') as f:
  48. cnt1 = 1
  49. jf = json.load(f)
  50. shapes = jf["shapes"]
  51. for shape in shapes:
  52. if shape["label"] == label:
  53. time = datetime.datetime.now()
  54. t = str(time.month).zfill(2) + str(time.day).zfill(2) + str(time.hour).zfill(2) + str(
  55. time.minute).zfill(2) + \
  56. str(time.second).zfill(2)
  57. image_cut_save(json_path.replace('json', 'jpg'), int(shape["points"][0][0]), int(shape["points"][0][1]),
  58. int(shape["points"][1][0]), int(shape["points"][1][1]),
  59. img_save_path + '/' + 'kdan' + '_' + t + '_' + str(uuid.uuid1())[0:8] + '_' + str(
  60. cnt).zfill(5) + str(cnt1).zfill(3) + '.jpg', pix)
  61. cnt1 += 1
  62. f.close()
  63. if __name__ == '__main__':
  64. parser = argparse.ArgumentParser()
  65. parser.add_argument('--input_dirs', type=str, nargs="+", default=['./'])
  66. parser.add_argument('--label', type=str, default='Table')
  67. parser.add_argument('--save_dir', type=str, default='./imgs')
  68. parser.add_argument('--size', type=int, default='0')
  69. args = parser.parse_args()
  70. cnt = 1
  71. input_list = args.input_dirs
  72. # 文件夹列表
  73. json_list = []
  74. # 遍历input_list
  75. for jp in input_list:
  76. json_img_list = os.listdir(jp)
  77. for path in json_img_list:
  78. if path.endswith('.json'):
  79. path = os.path.join(jp, path)
  80. json_list.append(path)
  81. if not os.path.exists(args.save_dir):
  82. os.makedirs(args.save_dir)
  83. for path in tqdm(json_list):
  84. single_img_cut(path, args.label, args.save_dir, cnt, args.size)
  85. cnt += 1