落水人员检测
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

google_utils.py 5.0KB

10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Google utils: https://cloud.google.com/storage/docs/reference/libraries
  2. import os
  3. import platform
  4. import subprocess
  5. import time
  6. from pathlib import Path
  7. import requests
  8. import torch
  9. def gsutil_getsize(url=''):
  10. # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
  11. s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
  12. return eval(s.split(' ')[0]) if len(s) else 0 # bytes
  13. def attempt_download(file, repo='ultralytics/yolov5'):
  14. # Attempt file download if does not exist
  15. file = Path(str(file).strip().replace("'", ''))
  16. if not file.exists():
  17. file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required)
  18. try:
  19. response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
  20. assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
  21. tag = response['tag_name'] # i.e. 'v1.0'
  22. except: # fallback plan
  23. assets = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt',
  24. 'yolov5s6.pt', 'yolov5m6.pt', 'yolov5l6.pt', 'yolov5x6.pt']
  25. try:
  26. tag = subprocess.check_output('git tag', shell=True, stderr=subprocess.STDOUT).decode().split()[-1]
  27. except:
  28. tag = 'v5.0' # current release
  29. name = file.name
  30. if name in assets:
  31. msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
  32. redundant = False # second download option
  33. try: # GitHub
  34. url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
  35. print(f'Downloading {url} to {file}...')
  36. torch.hub.download_url_to_file(url, file)
  37. assert file.exists() and file.stat().st_size > 1E6 # check
  38. except Exception as e: # GCP
  39. print(f'Download error: {e}')
  40. assert redundant, 'No secondary mirror'
  41. url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
  42. print(f'Downloading {url} to {file}...')
  43. os.system(f"curl -L '{url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail
  44. finally:
  45. if not file.exists() or file.stat().st_size < 1E6: # check
  46. file.unlink(missing_ok=True) # remove partial downloads
  47. print(f'ERROR: Download failure: {msg}')
  48. print('')
  49. return
  50. def gdrive_download(id='16TiPfZj7htmTyhntwcZyEEAejOUxuT6m', file='tmp.zip'):
  51. # Downloads a file from Google Drive. from yolov5.utils.google_utils import *; gdrive_download()
  52. t = time.time()
  53. file = Path(file)
  54. cookie = Path('cookie') # gdrive cookie
  55. print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
  56. file.unlink(missing_ok=True) # remove existing file
  57. cookie.unlink(missing_ok=True) # remove existing cookie
  58. # Attempt file download
  59. out = "NUL" if platform.system() == "Windows" else "/dev/null"
  60. os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
  61. if os.path.exists('cookie'): # large file
  62. s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
  63. else: # small file
  64. s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
  65. r = os.system(s) # execute, capture return
  66. cookie.unlink(missing_ok=True) # remove existing cookie
  67. # Error check
  68. if r != 0:
  69. file.unlink(missing_ok=True) # remove partial
  70. print('Download error ') # raise Exception('Download error')
  71. return r
  72. # Unzip if archive
  73. if file.suffix == '.zip':
  74. print('unzipping... ', end='')
  75. os.system(f'unzip -q {file}') # unzip
  76. file.unlink() # remove zip to free space
  77. print(f'Done ({time.time() - t:.1f}s)')
  78. return r
  79. def get_token(cookie="./cookie"):
  80. with open(cookie) as f:
  81. for line in f:
  82. if "download" in line:
  83. return line.split()[-1]
  84. return ""
  85. # def upload_blob(bucket_name, source_file_name, destination_blob_name):
  86. # # Uploads a file to a bucket
  87. # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
  88. #
  89. # storage_client = storage.Client()
  90. # bucket = storage_client.get_bucket(bucket_name)
  91. # blob = bucket.blob(destination_blob_name)
  92. #
  93. # blob.upload_from_filename(source_file_name)
  94. #
  95. # print('File {} uploaded to {}.'.format(
  96. # source_file_name,
  97. # destination_blob_name))
  98. #
  99. #
  100. # def download_blob(bucket_name, source_blob_name, destination_file_name):
  101. # # Uploads a blob from a bucket
  102. # storage_client = storage.Client()
  103. # bucket = storage_client.get_bucket(bucket_name)
  104. # blob = bucket.blob(source_blob_name)
  105. #
  106. # blob.download_to_filename(destination_file_name)
  107. #
  108. # print('Blob {} downloaded to {}.'.format(
  109. # source_blob_name,
  110. # destination_file_name))