predict.py 4.1 KB

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