Add automatic online download

This commit is contained in:
tunm
2023-02-27 22:33:52 +08:00
parent 58ad4ef736
commit 554a25edaa
6 changed files with 52 additions and 32 deletions
+29 -7
View File
@@ -2,7 +2,22 @@ import requests
from tqdm import tqdm
import zipfile
import os
from .settings import _DEFAULT_FOLDER_, _MODEL_VERSION_, _ONLINE_URL_
from .settings import _DEFAULT_FOLDER_, _MODEL_VERSION_, _ONLINE_URL_, _REMOTE_URL_, onnx_model_maps, onnx_runtime_config
def down_model_file(url, save_path):
resp = requests.get(url, stream=True)
total = int(resp.headers.get('content-length', 0))
with open(save_path, 'wb') as file, tqdm(
desc="Pull",
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in resp.iter_content(chunk_size=1024):
size = file.write(data)
bar.update(size)
def down_model_zip(url, save_path, is_unzip=False):
@@ -10,7 +25,7 @@ def down_model_zip(url, save_path, is_unzip=False):
total = int(resp.headers.get('content-length', 0))
name = os.path.join(save_path, os.path.basename(url))
with open(name, 'wb') as file, tqdm(
desc="pull",
desc="Pull",
total=total,
unit='iB',
unit_scale=True,
@@ -28,9 +43,16 @@ def down_model_zip(url, save_path, is_unzip=False):
def initialization(re_download=False):
os.makedirs(_DEFAULT_FOLDER_, exist_ok=True)
models_dir = os.path.join(_DEFAULT_FOLDER_, _MODEL_VERSION_)
models_dir = os.path.join(_DEFAULT_FOLDER_, _MODEL_VERSION_, "onnx")
os.makedirs(models_dir, exist_ok=True)
for model_key in onnx_model_maps:
save_path = onnx_runtime_config[model_key]
basename = os.path.basename(save_path)
remote_url = os.path.join(_REMOTE_URL_, basename + "?raw=true")
down_path = os.path.join(models_dir, basename)
if not os.path.exists(down_path) or re_download:
down_model_file(remote_url, down_path)
# print(models_dir)
if not os.path.exists(models_dir) or re_download:
target_url = os.path.join(_ONLINE_URL_, _MODEL_VERSION_) + '.zip'
down_model_zip(target_url, _DEFAULT_FOLDER_, True)
# if not os.path.exists(models_dir) or re_download:
# target_url = os.path.join(_ONLINE_URL_, _MODEL_VERSION_) + '.zip'
# down_model_zip(target_url, _DEFAULT_FOLDER_, True)
+4 -1
View File
@@ -1,7 +1,7 @@
import os
import sys
_MODEL_VERSION_ = "20230228"
_MODEL_VERSION_ = "20230229"
if 'win32' in sys.platform:
_DEFAULT_FOLDER_ = os.path.join(os.environ['HOMEPATH'], ".hyperlpr3")
@@ -17,3 +17,6 @@ onnx_runtime_config = dict(
cls_model_path=os.path.join(_MODEL_VERSION_, "onnx", "litemodel_cls_96x_r1.onnx"),
)
onnx_model_maps = ["det_model_path_320x", "det_model_path_640x", "rec_model_path", "cls_model_path"]
_REMOTE_URL_ = "https://github.com/szad670401/HyperLPR/blob/master/resource/models/onnx/"