您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

95 行
3.7KB

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