forked from m986883511/PaddleOCR-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mv download func to ppocr/utils/network.py
- Loading branch information
Showing
2 changed files
with
106 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import sys | ||
import tarfile | ||
import requests | ||
from tqdm import tqdm | ||
|
||
from ppocr.utils.logging import get_logger | ||
|
||
def download_with_progressbar(url, save_path): | ||
logger = get_logger() | ||
response = requests.get(url, stream=True) | ||
total_size_in_bytes = int(response.headers.get('content-length', 0)) | ||
block_size = 1024 # 1 Kibibyte | ||
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) | ||
with open(save_path, 'wb') as file: | ||
for data in response.iter_content(block_size): | ||
progress_bar.update(len(data)) | ||
file.write(data) | ||
progress_bar.close() | ||
if total_size_in_bytes == 0 or progress_bar.n != total_size_in_bytes: | ||
logger.error("Something went wrong while downloading models") | ||
sys.exit(0) | ||
|
||
|
||
def maybe_download(model_storage_directory, url): | ||
# using custom model | ||
tar_file_name_list = [ | ||
'inference.pdiparams', 'inference.pdiparams.info', 'inference.pdmodel' | ||
] | ||
if not os.path.exists( | ||
os.path.join(model_storage_directory, 'inference.pdiparams') | ||
) or not os.path.exists( | ||
os.path.join(model_storage_directory, 'inference.pdmodel')): | ||
tmp_path = os.path.join(model_storage_directory, url.split('/')[-1]) | ||
print('download {} to {}'.format(url, tmp_path)) | ||
os.makedirs(model_storage_directory, exist_ok=True) | ||
download_with_progressbar(url, tmp_path) | ||
with tarfile.open(tmp_path, 'r') as tarObj: | ||
for member in tarObj.getmembers(): | ||
filename = None | ||
for tar_file_name in tar_file_name_list: | ||
if tar_file_name in member.name: | ||
filename = tar_file_name | ||
if filename is None: | ||
continue | ||
file = tarObj.extractfile(member) | ||
with open( | ||
os.path.join(model_storage_directory, filename), | ||
'wb') as f: | ||
f.write(file.read()) | ||
os.remove(tmp_path) | ||
|