mv_train_img.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import os
  2. os.environ['CUDA_VISIBLE_DEVICES'] = '4'
  3. import os.path as osp
  4. import re
  5. import cv2
  6. import xml.etree.ElementTree as ET
  7. import paddlex as pdx
  8. file_list = 'dataset/train_list.txt'
  9. save_dir = './visualize/train'
  10. data_dir = 'dataset/'
  11. if not os.path.exists(save_dir):
  12. os.makedirs(save_dir)
  13. with open(file_list, 'r') as fr:
  14. while True:
  15. line = fr.readline()
  16. if not line:
  17. break
  18. img_file, xml_file = [osp.join(data_dir, x) \
  19. for x in line.strip().split()[:2]]
  20. if 'budaodian' not in img_file and 'cahua' not in img_file and 'loudi' not in img_file and 'zangdian' not in img_file:
  21. continue
  22. tree = ET.parse(xml_file)
  23. pattern = re.compile('<object>', re.IGNORECASE)
  24. obj_match = pattern.findall(str(ET.tostringlist(tree.getroot())))
  25. if len(obj_match) == 0:
  26. continue
  27. obj_tag = obj_match[0][1:-1]
  28. objs = tree.findall(obj_tag)
  29. pattern = re.compile('<size>', re.IGNORECASE)
  30. size_tag = pattern.findall(str(ET.tostringlist(tree.getroot())))[0][1:
  31. -1]
  32. size_element = tree.find(size_tag)
  33. pattern = re.compile('<width>', re.IGNORECASE)
  34. width_tag = pattern.findall(str(ET.tostringlist(size_element)))[0][1:
  35. -1]
  36. im_w = float(size_element.find(width_tag).text)
  37. pattern = re.compile('<height>', re.IGNORECASE)
  38. height_tag = pattern.findall(str(ET.tostringlist(size_element)))[0][1:
  39. -1]
  40. im_h = float(size_element.find(height_tag).text)
  41. gt_bbox = []
  42. gt_class = []
  43. for i, obj in enumerate(objs):
  44. pattern = re.compile('<name>', re.IGNORECASE)
  45. name_tag = pattern.findall(str(ET.tostringlist(obj)))[0][1:-1]
  46. cname = obj.find(name_tag).text.strip()
  47. gt_class.append(cname)
  48. pattern = re.compile('<difficult>', re.IGNORECASE)
  49. diff_tag = pattern.findall(str(ET.tostringlist(obj)))[0][1:-1]
  50. try:
  51. _difficult = int(obj.find(diff_tag).text)
  52. except Exception:
  53. _difficult = 0
  54. pattern = re.compile('<bndbox>', re.IGNORECASE)
  55. box_tag = pattern.findall(str(ET.tostringlist(obj)))[0][1:-1]
  56. box_element = obj.find(box_tag)
  57. pattern = re.compile('<xmin>', re.IGNORECASE)
  58. xmin_tag = pattern.findall(str(ET.tostringlist(box_element)))[0][
  59. 1:-1]
  60. x1 = float(box_element.find(xmin_tag).text)
  61. pattern = re.compile('<ymin>', re.IGNORECASE)
  62. ymin_tag = pattern.findall(str(ET.tostringlist(box_element)))[0][
  63. 1:-1]
  64. y1 = float(box_element.find(ymin_tag).text)
  65. pattern = re.compile('<xmax>', re.IGNORECASE)
  66. xmax_tag = pattern.findall(str(ET.tostringlist(box_element)))[0][
  67. 1:-1]
  68. x2 = float(box_element.find(xmax_tag).text)
  69. pattern = re.compile('<ymax>', re.IGNORECASE)
  70. ymax_tag = pattern.findall(str(ET.tostringlist(box_element)))[0][
  71. 1:-1]
  72. y2 = float(box_element.find(ymax_tag).text)
  73. x1 = max(0, x1)
  74. y1 = max(0, y1)
  75. if im_w > 0.5 and im_h > 0.5:
  76. x2 = min(im_w - 1, x2)
  77. y2 = min(im_h - 1, y2)
  78. gt_bbox.append([x1, y1, x2, y2])
  79. gts = []
  80. for bbox, name in zip(gt_bbox, gt_class):
  81. x1, y1, x2, y2 = bbox
  82. w = x2 - x1 + 1
  83. h = y2 - y1 + 1
  84. gt = {
  85. 'category_id': 0,
  86. 'category': name,
  87. 'bbox': [x1, y1, w, h],
  88. 'score': 1
  89. }
  90. gts.append(gt)
  91. gt_vis = pdx.det.visualize(img_file, gts, threshold=0.1, save_dir=None)
  92. cv2.imwrite(
  93. os.path.join(save_dir, os.path.split(img_file)[-1]), gt_vis)