Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pbcquoc committed Dec 3, 2022
1 parent 088b0c7 commit 04a8f34
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions vietocr/tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@
import numpy as np
import uuid
import requests
import wget
import tempfile

def download_weights(id_or_url, cached=None, md5=None, quiet=False):
if id_or_url.startswith('http'):
url = id_or_url
return gdown.cached_download(url=url, path=cached, md5=md5, quiet=quiet)
elif id_or_url.endswith('.pth'):
return id_or_url
else:
url = 'https://drive.google.com/uc?id={}'.format(id_or_url)
return gdown.cached_download(url=url, path=cached, md5=md5, quiet=quiet)
def download_weights(uri, cached=None, md5=None, quiet=False):
if uri.startswith('http'):
return download(url=uri)
return uri

def download(url):
tmp_dir = tempfile.gettempdir()
filename = url.split('/')[-1]
full_path = os.path.join(tmp_dir, filename)

if os.path.exists(full_path):
print('Model weight {} exsits. Ignore download!'.format(full_path))
return full_path

with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(full_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
return full_path

def download_config(id):
url = 'https://raw.githubusercontent.com/pbcquoc/vietocr/master/config/{}'.format(id)
url = 'https://vocr.vn/data/vietocr/config/{}'.format(id)
r = requests.get(url)
config = yaml.safe_load(r.text)
return config
Expand Down

0 comments on commit 04a8f34

Please sign in to comment.