network.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. import tarfile
  17. import requests
  18. from tqdm import tqdm
  19. from ppocr.utils.logging import get_logger
  20. def download_with_progressbar(url, save_path):
  21. logger = get_logger()
  22. response = requests.get(url, stream=True)
  23. if response.status_code == 200:
  24. total_size_in_bytes = int(response.headers.get('content-length', 1))
  25. block_size = 1024 # 1 Kibibyte
  26. progress_bar = tqdm(
  27. total=total_size_in_bytes, unit='iB', unit_scale=True)
  28. with open(save_path, 'wb') as file:
  29. for data in response.iter_content(block_size):
  30. progress_bar.update(len(data))
  31. file.write(data)
  32. progress_bar.close()
  33. else:
  34. logger.error("Something went wrong while downloading models")
  35. sys.exit(0)
  36. def maybe_download(model_storage_directory, url):
  37. # using custom model
  38. tar_file_name_list = ['.pdiparams', '.pdiparams.info', '.pdmodel']
  39. if not os.path.exists(
  40. os.path.join(model_storage_directory, 'inference.pdiparams')
  41. ) or not os.path.exists(
  42. os.path.join(model_storage_directory, 'inference.pdmodel')):
  43. assert url.endswith('.tar'), 'Only supports tar compressed package'
  44. tmp_path = os.path.join(model_storage_directory, url.split('/')[-1])
  45. print('download {} to {}'.format(url, tmp_path))
  46. os.makedirs(model_storage_directory, exist_ok=True)
  47. download_with_progressbar(url, tmp_path)
  48. with tarfile.open(tmp_path, 'r') as tarObj:
  49. for member in tarObj.getmembers():
  50. filename = None
  51. for tar_file_name in tar_file_name_list:
  52. if member.name.endswith(tar_file_name):
  53. filename = 'inference' + tar_file_name
  54. if filename is None:
  55. continue
  56. file = tarObj.extractfile(member)
  57. with open(
  58. os.path.join(model_storage_directory, filename),
  59. 'wb') as f:
  60. f.write(file.read())
  61. os.remove(tmp_path)
  62. def is_link(s):
  63. return s is not None and s.startswith('http')
  64. def confirm_model_dir_url(model_dir, default_model_dir, default_url):
  65. url = default_url
  66. if model_dir is None or is_link(model_dir):
  67. if is_link(model_dir):
  68. url = model_dir
  69. file_name = url.split('/')[-1][:-4]
  70. model_dir = default_model_dir
  71. model_dir = os.path.join(model_dir, file_name)
  72. return model_dir, url