Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
4 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 torch
  8. def gsutil_getsize(url=''):
  9. # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
  10. s = subprocess.check_output('gsutil du %s' % url, shell=True).decode('utf-8')
  11. return eval(s.split(' ')[0]) if len(s) else 0 # bytes
  12. def attempt_download(weights):
  13. # Attempt to download pretrained weights if not found locally
  14. weights = str(weights).strip().replace("'", '')
  15. file = Path(weights).name.lower()
  16. msg = weights + ' missing, try downloading from https://github.com/ultralytics/yolov5/releases/'
  17. models = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt'] # available models
  18. redundant = False # offer second download option
  19. if file in models and not os.path.isfile(weights):
  20. # Google Drive
  21. # d = {'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO',
  22. # 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr',
  23. # 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV',
  24. # 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS'}
  25. # r = gdrive_download(id=d[file], name=weights) if file in d else 1
  26. # if r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6: # check
  27. # return
  28. try: # GitHub
  29. url = 'https://github.com/ultralytics/yolov5/releases/download/v3.1/' + file
  30. print('Downloading %s to %s...' % (url, weights))
  31. torch.hub.download_url_to_file(url, weights)
  32. assert os.path.exists(weights) and os.path.getsize(weights) > 1E6 # check
  33. except Exception as e: # GCP
  34. print('Download error: %s' % e)
  35. assert redundant, 'No secondary mirror'
  36. url = 'https://storage.googleapis.com/ultralytics/yolov5/ckpt/' + file
  37. print('Downloading %s to %s...' % (url, weights))
  38. r = os.system('curl -L %s -o %s' % (url, weights)) # torch.hub.download_url_to_file(url, weights)
  39. finally:
  40. if not (os.path.exists(weights) and os.path.getsize(weights) > 1E6): # check
  41. os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
  42. print('ERROR: Download failure: %s' % msg)
  43. print('')
  44. return
  45. def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
  46. # Downloads a file from Google Drive. from utils.google_utils import *; gdrive_download()
  47. t = time.time()
  48. print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
  49. os.remove(name) if os.path.exists(name) else None # remove existing
  50. os.remove('cookie') if os.path.exists('cookie') else None
  51. # Attempt file download
  52. out = "NUL" if platform.system() == "Windows" else "/dev/null"
  53. os.system('curl -c ./cookie -s -L "drive.google.com/uc?export=download&id=%s" > %s ' % (id, out))
  54. if os.path.exists('cookie'): # large file
  55. s = 'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm=%s&id=%s" -o %s' % (get_token(), id, name)
  56. else: # small file
  57. s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id)
  58. r = os.system(s) # execute, capture return
  59. os.remove('cookie') if os.path.exists('cookie') else None
  60. # Error check
  61. if r != 0:
  62. os.remove(name) if os.path.exists(name) else None # remove partial
  63. print('Download error ') # raise Exception('Download error')
  64. return r
  65. # Unzip if archive
  66. if name.endswith('.zip'):
  67. print('unzipping... ', end='')
  68. os.system('unzip -q %s' % name) # unzip
  69. os.remove(name) # remove zip to free space
  70. print('Done (%.1fs)' % (time.time() - t))
  71. return r
  72. def get_token(cookie="./cookie"):
  73. with open(cookie) as f:
  74. for line in f:
  75. if "download" in line:
  76. return line.split()[-1]
  77. return ""
  78. # def upload_blob(bucket_name, source_file_name, destination_blob_name):
  79. # # Uploads a file to a bucket
  80. # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
  81. #
  82. # storage_client = storage.Client()
  83. # bucket = storage_client.get_bucket(bucket_name)
  84. # blob = bucket.blob(destination_blob_name)
  85. #
  86. # blob.upload_from_filename(source_file_name)
  87. #
  88. # print('File {} uploaded to {}.'.format(
  89. # source_file_name,
  90. # destination_blob_name))
  91. #
  92. #
  93. # def download_blob(bucket_name, source_blob_name, destination_file_name):
  94. # # Uploads a blob from a bucket
  95. # storage_client = storage.Client()
  96. # bucket = storage_client.get_bucket(bucket_name)
  97. # blob = bucket.blob(source_blob_name)
  98. #
  99. # blob.download_to_filename(destination_file_name)
  100. #
  101. # print('Blob {} downloaded to {}.'.format(
  102. # source_blob_name,
  103. # destination_file_name))