Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

100 linhas
4.1KB

  1. # This file contains google utils: https://cloud.google.com/storage/docs/reference/libraries
  2. # pip install --upgrade google-cloud-storage
  3. # from google.cloud import storage
  4. import os
  5. import time
  6. from pathlib import Path
  7. def attempt_download(weights):
  8. # Attempt to download pretrained weights if not found locally
  9. weights = weights.strip().replace("'", '')
  10. msg = weights + ' missing, try downloading from https://drive.google.com/drive/folders/1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J'
  11. r = 1 # return
  12. if len(weights) > 0 and not os.path.isfile(weights):
  13. d = {'yolov3-spp.pt': '1mM67oNw4fZoIOL1c8M3hHmj66d8e-ni_', # yolov3-spp.yaml
  14. 'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO', # yolov5s.yaml
  15. 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr', # yolov5m.yaml
  16. 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV', # yolov5l.yaml
  17. 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS', # yolov5x.yaml
  18. }
  19. file = Path(weights).name
  20. if file in d:
  21. r = gdrive_download(id=d[file], name=weights)
  22. if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
  23. os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
  24. s = "curl -L -o %s 'storage.googleapis.com/ultralytics/yolov5/ckpt/%s'" % (weights, file)
  25. r = os.system(s) # execute, capture return values
  26. # Error check
  27. if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
  28. os.remove(weights) if os.path.exists(weights) else None # remove partial downloads
  29. raise Exception(msg)
  30. def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'):
  31. # Downloads a file from Google Drive, accepting presented query
  32. # from utils.google_utils import *; gdrive_download()
  33. t = time.time()
  34. print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
  35. os.remove(name) if os.path.exists(name) else None # remove existing
  36. os.remove('cookie') if os.path.exists('cookie') else None
  37. # Attempt file download
  38. os.system("curl -c ./cookie -s -L \"drive.google.com/uc?export=download&id=%s\" > /dev/null" % id)
  39. if os.path.exists('cookie'): # large file
  40. s = "curl -Lb ./cookie \"drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=%s\" -o %s" % (
  41. id, name)
  42. else: # small file
  43. s = "curl -s -L -o %s 'drive.google.com/uc?export=download&id=%s'" % (name, id)
  44. r = os.system(s) # execute, capture return values
  45. os.remove('cookie') if os.path.exists('cookie') else None
  46. # Error check
  47. if r != 0:
  48. os.remove(name) if os.path.exists(name) else None # remove partial
  49. print('Download error ') # raise Exception('Download error')
  50. return r
  51. # Unzip if archive
  52. if name.endswith('.zip'):
  53. print('unzipping... ', end='')
  54. os.system('unzip -q %s' % name) # unzip
  55. os.remove(name) # remove zip to free space
  56. print('Done (%.1fs)' % (time.time() - t))
  57. return r
  58. # def upload_blob(bucket_name, source_file_name, destination_blob_name):
  59. # # Uploads a file to a bucket
  60. # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
  61. #
  62. # storage_client = storage.Client()
  63. # bucket = storage_client.get_bucket(bucket_name)
  64. # blob = bucket.blob(destination_blob_name)
  65. #
  66. # blob.upload_from_filename(source_file_name)
  67. #
  68. # print('File {} uploaded to {}.'.format(
  69. # source_file_name,
  70. # destination_blob_name))
  71. #
  72. #
  73. # def download_blob(bucket_name, source_blob_name, destination_file_name):
  74. # # Uploads a blob from a bucket
  75. # storage_client = storage.Client()
  76. # bucket = storage_client.get_bucket(bucket_name)
  77. # blob = bucket.blob(source_blob_name)
  78. #
  79. # blob.download_to_filename(destination_file_name)
  80. #
  81. # print('Blob {} downloaded to {}.'.format(
  82. # source_blob_name,
  83. # destination_file_name))