CoolFace
Apppublic

chanelisa/objectdetectionhw

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
google_utils.py127 linesDownload Raw Back to utils
1# Google utils: https://cloud.google.com/storage/docs/reference/libraries2 3import os4import platform5import subprocess6import time7from pathlib import Path8 9import requests10import torch11 12 13def gsutil_getsize(url=''):14    # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du15    s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')16    return eval(s.split(' ')[0]) if len(s) else 0  # bytes17 18 19def attempt_download(file, repo='WongKinYiu/yolov7'):20    # Attempt file download if does not exist21    file = Path(str(file).strip().replace("'", '').lower())22 23    if not file.exists():24        try:25            response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json()  # github api26            assets = [x['name'] for x in response['assets']]  # release assets27            tag = response['tag_name']  # i.e. 'v1.0'28        except:  # fallback plan29            assets = ['yolov7.pt', 'yolov7-tiny.pt', 'yolov7x.pt', 'yolov7-d6.pt', 'yolov7-e6.pt', 30                      'yolov7-e6e.pt', 'yolov7-w6.pt']31            try:32                tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]33            except IndexError:34                tag = 'default'35                36        name = file.name37        if name in assets:38            msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'39            redundant = False  # second download option40            try:  # GitHub41                url = f'https://github.com/{repo}/releases/download/{tag}/{name}'42                print(f'Downloading {url} to {file}...')43                torch.hub.download_url_to_file(url, file)44                assert file.exists() and file.stat().st_size > 1E6  # check45            except Exception as e:  # GCP46                print(f'Download error: {e}')47                assert redundant, 'No secondary mirror'48                url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'49                print(f'Downloading {url} to {file}...')50                os.system(f'curl -L {url} -o {file}')  # torch.hub.download_url_to_file(url, weights)51            finally:52                if not file.exists() or file.stat().st_size < 1E6:  # check53                    file.unlink(missing_ok=True)  # remove partial downloads54                    print(f'ERROR: Download failure: {msg}')55                print('')56                return57 58 59def gdrive_download(id='', file='tmp.zip'):60    # Downloads a file from Google Drive. from yolov7.utils.google_utils import *; gdrive_download()61    t = time.time()62    file = Path(file)63    cookie = Path('cookie')  # gdrive cookie64    print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')65    file.unlink(missing_ok=True)  # remove existing file66    cookie.unlink(missing_ok=True)  # remove existing cookie67 68    # Attempt file download69    out = "NUL" if platform.system() == "Windows" else "/dev/null"70    os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')71    if os.path.exists('cookie'):  # large file72        s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'73    else:  # small file74        s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'75    r = os.system(s)  # execute, capture return76    cookie.unlink(missing_ok=True)  # remove existing cookie77 78    # Error check79    if r != 0:80        file.unlink(missing_ok=True)  # remove partial81        print('Download error ')  # raise Exception('Download error')82        return r83 84    # Unzip if archive85    if file.suffix == '.zip':86        print('unzipping... ', end='')87        os.system(f'unzip -q {file}')  # unzip88        file.unlink()  # remove zip to free space89 90    print(f'Done ({time.time() - t:.1f}s)')91    return r92 93 94def get_token(cookie="./cookie"):95    with open(cookie) as f:96        for line in f:97            if "download" in line:98                return line.split()[-1]99    return ""100 101# def upload_blob(bucket_name, source_file_name, destination_blob_name):102#     # Uploads a file to a bucket103#     # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python104#105#     storage_client = storage.Client()106#     bucket = storage_client.get_bucket(bucket_name)107#     blob = bucket.blob(destination_blob_name)108#109#     blob.upload_from_filename(source_file_name)110#111#     print('File {} uploaded to {}.'.format(112#         source_file_name,113#         destination_blob_name))114#115#116# def download_blob(bucket_name, source_blob_name, destination_file_name):117#     # Uploads a blob from a bucket118#     storage_client = storage.Client()119#     bucket = storage_client.get_bucket(bucket_name)120#     blob = bucket.blob(source_blob_name)121#122#     blob.download_to_filename(destination_file_name)123#124#     print('Blob {} downloaded to {}.'.format(125#         source_blob_name,126#         destination_file_name))127