base.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import json
  17. import chardet
  18. import numpy as np
  19. class MyEncoder(json.JSONEncoder):
  20. def default(self, obj):
  21. if isinstance(obj, np.integer):
  22. return int(obj)
  23. elif isinstance(obj, np.floating):
  24. return float(obj)
  25. elif isinstance(obj, np.ndarray):
  26. return obj.tolist()
  27. else:
  28. return super(MyEncoder, self).default(obj)
  29. def is_pic(img_name):
  30. valid_suffix = ["JPEG", "jpeg", "JPG", "jpg", "BMP", "bmp", "PNG", "png"]
  31. suffix = img_name.split(".")[-1]
  32. if suffix not in valid_suffix:
  33. return False
  34. return True
  35. def get_encoding(path):
  36. f = open(path, 'rb')
  37. data = f.read()
  38. file_encoding = chardet.detect(data).get('encoding')
  39. f.close()
  40. return file_encoding