Guest User

Untitled

a guest
Mar 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. def label2xml(list_path, category_id, ANN_DIR='data/Annotations/', IMG_DIR='data/images/', SET_DIR='data/ImageSets/',
  2. train=True):
  3. set_file_list = [SET_DIR + 'train.txt', SET_DIR + 'minival.txt', SET_DIR + 'testdev.txt', SET_DIR + 'test.txt']
  4. TYPEPREFIX = 'train' if train else 'val'
  5. lines = read_lines(list_path)
  6. line_i = 0
  7. last_name = ''
  8. E = None
  9. img_annotation = None
  10. image_file = None
  11. while line_i < len(lines):
  12. line = lines[line_i]
  13. infos = line.split()
  14. image_path = infos[0]
  15. box = infos[1:]
  16. imagename = str(int(image_path.split('.')[0].split('/')[-1]))
  17.  
  18. print('last_path: %s, image_path: %s' % (last_name, image_path))
  19. if last_name != imagename:
  20. if train:
  21. write_line(set_file_list[0], "%s.jpg %s.xml" % (imagename, imagename))
  22. else:
  23. for i in range(1, 4):
  24. write_line(set_file_list[i], "%s.jpg %s.xml" % (imagename, imagename))
  25. if last_name != '':
  26. xml_pretty = etree.tostring(img_annotation, pretty_print=True)
  27. with open(ANN_DIR + last_name + ".xml", 'wb') as ann_file:
  28. ann_file.write(xml_pretty)
  29. image_file = imread(image_path)
  30. copyfile(image_path, IMG_DIR + '%d.jpg' % int(imagename))
  31. if path.exists(ANN_DIR + imagename + ".xml"):
  32. E = objectify.ElementMaker(annotate=False)
  33. img_annotation = objectify.fromstring(read_content(ANN_DIR + imagename + ".xml"))
  34. else:
  35. E = objectify.ElementMaker(annotate=False)
  36. img_annotation = E.annotation(
  37. E.folder(TYPEPREFIX),
  38. E.filename(imagename),
  39. E.source(
  40. E.database('coco_cattle'),
  41. ),
  42. E.size(
  43. E.width(image_file.shape[1]),
  44. E.height(image_file.shape[0]),
  45. E.depth(3),
  46. ),
  47. E.segmented(0)
  48. )
  49.  
  50. last_name = imagename
  51. objectNode = E.object(
  52. E.name(str(category_id)),
  53. E.pose("Unspecified"),
  54. E.truncated("0"),
  55. E.difficult("0"),
  56. E.bndbox(
  57. E.xmin(str(int(float(box[0]) * image_file.shape[1]))),
  58. E.ymin(str(int(float(box[1]) * image_file.shape[0]))),
  59. E.xmax(str(int(float(box[2]) * image_file.shape[1]))),
  60. E.ymax(str(int(float(box[3]) * image_file.shape[0]))),
  61. ),
  62. )
  63. img_annotation.append(objectNode)
  64. line_i += 1
Add Comment
Please, Sign In to add comment