xml2.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. """
  2. # File : xml2.py
  3. # Time :2024-04-04 12:16
  4. # Author :FEANGYANG
  5. # version :python 3.7
  6. # Contact :1071082183@qq.com
  7. # Description:
  8. """
  9. import glob
  10. import os
  11. import cv2
  12. import numpy as np
  13. def read_xml(file):
  14. # actual parsing
  15. in_file = open(file)
  16. tree = ET.parse(in_file)
  17. root = tree.getroot()
  18. bboxes_list = []
  19. for obj in root.iter('object'):
  20. current = list()
  21. name = obj.find('name').text
  22. xmlbox = obj.find('bndbox')
  23. xn = xmlbox.find('xmin').text
  24. xx1 = xmlbox.find('xmax').text
  25. yn = xmlbox.find('ymin').text
  26. yx1 = xmlbox.find('ymax').text
  27. bboxes_list.append([xn, yn, xx1, yx1])
  28. return bboxes_list
  29. def get_xmls_path_list(xmls_path):
  30. xmls_path_list = []
  31. for path in os.listdir(xmls_path):
  32. xml_path = '/'.join([xmls_path, path])
  33. xmls_path_list.append(xml_path)
  34. # bboxes_list = read_xml(xml_path)
  35. return xmls_path_list
  36. def get_images_path_list(images_path):
  37. images_path_list = []
  38. for path in os.listdir(images_path):
  39. image_path = '/'.join([images_path, path])
  40. images_path_list.append(image_path)
  41. return images_path_list
  42. def test_xml2images_path(images_path, xmls_path_list):
  43. test_images_path_list = []
  44. test_xmls_path_list = []
  45. tmp_images_path = os.listdir(images_path)
  46. if len(tmp_images_path) > 3:
  47. suffex = os.path.splitext(tmp_images_path[2])[-1]
  48. else:
  49. raise ValueError('图片数量太少')
  50. for xml_path in xmls_path_list:
  51. xml_name = os.path.split(xml_path)[-1]
  52. image_path = '/'.join([images_path, os.path.splitext(xml_name)[0]+suffex])
  53. if os.path.exists(image_path):
  54. test_xmls_path_list.append(xml_path)
  55. test_images_path_list.append(image_path)
  56. return test_images_path_list, test_xmls_path_list
  57. # def small_images(image_path, pixes,pic_target = './result/'):
  58. # if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹
  59. # os.makedirs(pic_target)
  60. # picture = cv2.imread(image_path)
  61. # name = os.path.split(image_path)[-1]
  62. #
  63. # (width, length, depth) = picture.shape
  64. # w, h, _ = picture.shape
  65. # # _w = w % pixes
  66. # # _h = h % pixes
  67. #
  68. # cut_width = 64
  69. # cut_length = 64
  70. #
  71. # # 预处理生成0矩阵
  72. # pic = np.zeros((cut_width, cut_length, depth))
  73. # # 计算可以划分的横纵的个数
  74. # num_width = int(width / cut_width)
  75. # num_length = int(length / cut_length)
  76. # # for循环迭代生成
  77. # lines_list = []
  78. # for i in range(0, num_width):
  79. # for j in range(0, num_length):
  80. # pic = picture[i * cut_width: (i + 1) * cut_width, j * cut_length: (j + 1) * cut_length, :]
  81. # result_path = pic_target + os.path.splitext(name)[0] +'_{}_{}.jpg'.format(i + 1, j + 1)
  82. # cv2.imwrite(result_path, pic)
  83. def small_images(image_path, pixes,pic_target = './result/'):
  84. if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹
  85. os.makedirs(pic_target)
  86. picture = cv2.imread(image_path)
  87. name = os.path.split(image_path)[-1]
  88. (length, width, depth) = picture.shape
  89. w, h, _ = picture.shape
  90. # _w = w % pixes
  91. # _h = h % pixes
  92. cut_width = 128
  93. cut_length = 128
  94. # 预处理生成0矩阵
  95. # pic = np.zeros((cut_width, cut_length, depth))
  96. # 计算可以划分的横纵的个数
  97. num_width = int(width / cut_width)
  98. num_length = int(length / cut_length)
  99. print(num_width, num_length)
  100. pixes += num_width * num_length
  101. print(pixes)
  102. # for循环迭代生成
  103. lines_list = []
  104. for i in range(0, num_width+1):
  105. lines_list.append([[i * cut_width, 0], [i * cut_width, w]])
  106. for j in range(0, num_length+1):
  107. lines_list.append([(0, j * cut_length), (h, j * cut_length)])
  108. point_color = (0, 0, 255) # BGR
  109. thickness = 1
  110. lineType = 8
  111. for line in lines_list:
  112. cv2.line(picture, tuple(line[0]), tuple(line[1]), point_color, thickness, lineType)
  113. result_path = pic_target + os.path.splitext(name)[0] + '.jpg'
  114. cv2.imwrite(result_path, picture)
  115. return pixes
  116. # def small_images(image_path, pixes, pic_target = './result/'):
  117. # if not os.path.exists(pic_target): # 判断是否存在文件夹如果不存在则创建为文件夹
  118. # os.makedirs(pic_target)
  119. # crop_w = pixes # 裁剪图像宽度
  120. # crop_h = pixes
  121. # img = cv2.imread(image_path)
  122. # name = os.path.split(image_path)[-1]
  123. # old_size = img.shape[0:2] # 原图尺寸
  124. # # print(old_size[0],type(old_size[0]))
  125. # ######
  126. # if old_size[0] % crop_h != 0 and old_size[1] % crop_w != 0:
  127. #
  128. # h_num = int(old_size[0] / crop_h) + 1 # 取整后加1
  129. # w_num = int(old_size[1] / crop_w) + 1
  130. #
  131. # new_height = (h_num) * crop_h # 小图像尺寸整倍数的大图像
  132. # new_width = (w_num) * crop_w
  133. # # print(new_height,new_width)
  134. # # #
  135. # pad_h = new_height - old_size[0] # 计算自动需要填充的像素数目(图像的高这一维度上)
  136. # pad_w = new_width - old_size[1] # 计算需要填充的像素数目(图像的宽这一维度上)
  137. # # print(pad_w,pad_h)
  138. #
  139. # # top, bottom = pad_h // 2, pad_h - (pad_h // 2)
  140. # # left, right = pad_w // 2, pad_w - (pad_w // 2)
  141. # top, bottom = 0, pad_h
  142. # left, right = 0, pad_w
  143. # # print(top, bottom, left, right)
  144. # img_new = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, None, (0, 0, 0))
  145. #
  146. # else:
  147. # h_num = int(old_size[0] / crop_h)
  148. # w_num = int(old_size[1] / crop_w)
  149. # img_new = cv2.imread(image_path)
  150. #
  151. # for i in range(h_num):
  152. # for j in range(w_num):
  153. # # print(i,j)
  154. # x = int(i * crop_h) # 高度上裁剪图像个数
  155. # y = int(j * crop_w)
  156. # # print(x,y)
  157. # img_crop = img_new[x: x + crop_h, y: y + crop_w]
  158. # print(x, y, x + crop_h, y + crop_w)
  159. # # print(z)
  160. # saveName = os.path.splitext(name)[0] + '-' + str(i) + '-' + str(j) + ".png" # 小图像名称,内含小图像的顺序
  161. # cv2.imwrite(pic_target + saveName, img_crop)
  162. if __name__ == '__main__':
  163. images_path1 = './data/day_time_wildfire/images'
  164. xmls_path1 = './data/day_time_wildfire/annotations/xmls'
  165. images_path2 = './data/FlameVision/Detection/images'
  166. xmls_path2 = './data/FlameVision/Detection/annotations'
  167. images_path = images_path2
  168. xmls_path = xmls_path2
  169. xmls_path_list = get_xmls_path_list(xmls_path)
  170. images_path_list = get_images_path_list(images_path)
  171. # test_images_path_list, test_xmls_path_list = test_xml2images_path(images_path, xmls_path_list)
  172. sum = 0
  173. for image_path in images_path_list:
  174. sum = small_images(image_path, sum)
  175. pass