settings.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) <2015-Present> Tzutalin
  2. # Copyright (C) 2013 MIT, Computer Science and Artificial Intelligence Laboratory. Bryan Russell, Antonio Torralba,
  3. # William T. Freeman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. # associated documentation files (the "Software"), to deal in the Software without restriction, including without
  5. # limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
  6. # Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  7. # The above copyright notice and this permission notice shall be included in all copies or substantial portions of
  8. # the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  9. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  10. # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  12. # THE SOFTWARE.
  13. import pickle
  14. import os
  15. import sys
  16. class Settings(object):
  17. def __init__(self):
  18. # Be default, the home will be in the same folder as labelImg
  19. home = os.path.expanduser("~")
  20. self.data = {}
  21. # self.path = os.path.join(home, '.labelImgSettings.pkl')
  22. self.path = os.path.join(home, '.autoOCRSettings.pkl')
  23. def __setitem__(self, key, value):
  24. self.data[key] = value
  25. def __getitem__(self, key):
  26. return self.data[key]
  27. def get(self, key, default=None):
  28. if key in self.data:
  29. return self.data[key]
  30. return default
  31. def save(self):
  32. if self.path:
  33. with open(self.path, 'wb') as f:
  34. pickle.dump(self.data, f, pickle.HIGHEST_PROTOCOL)
  35. return True
  36. return False
  37. def load(self):
  38. try:
  39. if os.path.exists(self.path):
  40. with open(self.path, 'rb') as f:
  41. self.data = pickle.load(f)
  42. return True
  43. except:
  44. print('Loading setting failed')
  45. return False
  46. def reset(self):
  47. if os.path.exists(self.path):
  48. os.remove(self.path)
  49. print('Remove setting pkl file ${0}'.format(self.path))
  50. self.data = {}
  51. self.path = None