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.2KB

  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()
  10. msg = weights + ' missing, try downloading from https://drive.google.com/drive/folders/1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J'
  11. r = 1
  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 'https://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='1HaXkef9z6y5l4vUnCYgdmEAj61c6bfWO', name='coco.zip'):
  31. # https://gist.github.com/tanaikech/f0f2d122e05bf5f971611258c22c110f
  32. # Downloads a file from Google Drive, accepting presented query
  33. # from utils.google_utils import *; gdrive_download()
  34. t = time.time()
  35. print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='')
  36. os.remove(name) if os.path.exists(name) else None # remove existing
  37. os.remove('cookie') if os.path.exists('cookie') else None
  38. # Attempt file download
  39. os.system("curl -c ./cookie -s -L \"https://drive.google.com/uc?export=download&id=%s\" > /dev/null" % id)
  40. if os.path.exists('cookie'): # large file
  41. s = "curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=%s\" -o %s" % (
  42. id, name)
  43. else: # small file
  44. s = "curl -s -L -o %s 'https://drive.google.com/uc?export=download&id=%s'" % (name, id)
  45. r = os.system(s) # execute, capture return values
  46. os.remove('cookie') if os.path.exists('cookie') else None
  47. # Error check
  48. if r != 0:
  49. os.remove(name) if os.path.exists(name) else None # remove partial
  50. print('Download error ') # raise Exception('Download error')
  51. return r
  52. # Unzip if archive
  53. if name.endswith('.zip'):
  54. print('unzipping... ', end='')
  55. os.system('unzip -q %s' % name) # unzip
  56. os.remove(name) # remove zip to free space
  57. print('Done (%.1fs)' % (time.time() - t))
  58. return r
  59. # def upload_blob(bucket_name, source_file_name, destination_blob_name):
  60. # # Uploads a file to a bucket
  61. # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
  62. #
  63. # storage_client = storage.Client()
  64. # bucket = storage_client.get_bucket(bucket_name)
  65. # blob = bucket.blob(destination_blob_name)
  66. #
  67. # blob.upload_from_filename(source_file_name)
  68. #
  69. # print('File {} uploaded to {}.'.format(
  70. # source_file_name,
  71. # destination_blob_name))
  72. #
  73. #
  74. # def download_blob(bucket_name, source_blob_name, destination_file_name):
  75. # # Uploads a blob from a bucket
  76. # storage_client = storage.Client()
  77. # bucket = storage_client.get_bucket(bucket_name)
  78. # blob = bucket.blob(source_blob_name)
  79. #
  80. # blob.download_to_filename(destination_file_name)
  81. #
  82. # print('Blob {} downloaded to {}.'.format(
  83. # source_blob_name,
  84. # destination_file_name))