labelFile.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. from base64 import b64encode, b64decode
  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 = 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 = ((s['label'], s['points'], s['line_color'], s['fill_color'])\
  50. for s in data['shapes'])
  51. # Only replace data after everything is loaded.
  52. self.shapes = shapes
  53. self.imagePath = data['imagePath']
  54. self.imageData = imageData
  55. self.lineColor = lineColor
  56. self.fillColor = fillColor
  57. self.filename = filename
  58. except Exception as e:
  59. raise LabelFileError(e)
  60. def save(self, filename, shapes, imagePath, imageData=None,
  61. lineColor=None, fillColor=None):
  62. if imageData is not None:
  63. imageData = b64encode(imageData).decode('utf-8')
  64. data = dict(
  65. shapes=shapes,
  66. lineColor=lineColor,
  67. fillColor=fillColor,
  68. imagePath=imagePath,
  69. imageData=imageData,
  70. )
  71. try:
  72. with open(filename, 'wb' if PY2 else 'w') as f:
  73. json.dump(data, f, ensure_ascii=True, indent=2)
  74. self.filename = filename
  75. except Exception as e:
  76. raise LabelFileError(e)
  77. @staticmethod
  78. def isLabelFile(filename):
  79. return os.path.splitext(filename)[1].lower() == LabelFile.suffix