车位角点检测代码
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.

123 lines
4.8KB

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