labelFile.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #
  2. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  3. #
  4. # This file is part of Labelme.
  5. #
  6. # Labelme is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Labelme is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import base64
  20. import json
  21. import os.path
  22. import sys
  23. PY2 = sys.version_info[0] == 2
  24. class LabelFileError(Exception):
  25. pass
  26. class LabelFile(object):
  27. suffix = '.json'
  28. def __init__(self, filename=None):
  29. self.shapes = ()
  30. self.imagePath = None
  31. self.imageData = None
  32. if filename is not None:
  33. self.load(filename)
  34. self.filename = filename
  35. def load(self, filename):
  36. try:
  37. with open(filename, 'rb' if PY2 else 'r') as f:
  38. data = json.load(f)
  39. if data['imageData'] is not None:
  40. imageData = base64.b64decode(data['imageData'])
  41. else:
  42. # relative path from label file to relative path from cwd
  43. imagePath = os.path.join(os.path.dirname(filename),
  44. data['imagePath'])
  45. with open(imagePath, 'rb') as f:
  46. imageData = f.read()
  47. lineColor = data['lineColor']
  48. fillColor = data['fillColor']
  49. shapes = (
  50. (s['label'], s['points'], s['line_color'], s['fill_color'])
  51. for s in data['shapes']
  52. )
  53. # Only replace data after everything is loaded.
  54. self.shapes = shapes
  55. self.imagePath = data['imagePath']
  56. self.imageData = imageData
  57. self.lineColor = lineColor
  58. self.fillColor = fillColor
  59. self.filename = filename
  60. except Exception as e:
  61. raise LabelFileError(e)
  62. def save(self, filename, shapes, imagePath, imageData=None,
  63. lineColor=None, fillColor=None):
  64. if imageData is not None:
  65. imageData = base64.b64encode(imageData).decode('utf-8')
  66. data = dict(
  67. shapes=shapes,
  68. lineColor=lineColor,
  69. fillColor=fillColor,
  70. imagePath=imagePath,
  71. imageData=imageData,
  72. )
  73. try:
  74. with open(filename, 'wb' if PY2 else 'w') as f:
  75. json.dump(data, f, ensure_ascii=True, indent=2)
  76. self.filename = filename
  77. except Exception as e:
  78. raise LabelFileError(e)
  79. @staticmethod
  80. def isLabelFile(filename):
  81. return os.path.splitext(filename)[1].lower() == LabelFile.suffix