简体   繁体   中英

Python: Writing a code that recognizes an object and counts it

I am writing a python program that counts and recognizes the object in the image by using the colab, when I got an error in the following code,

drive_url = 'https://drive.google.com/uc?export=download&confirm=jH_3&id=' + DATASET_DRIVEID
file_name = DATASET_DRIVEID + '.zip'

urllib.request.urlretrieve(drive_url, file_name)
print('Download completed!')

os.makedirs(DATASET_DIR, exist_ok=True)
with zipfile.ZipFile(file_name, 'r') as zip_ref:
  zip_ref.extractall(DATASET_DIR)
os.remove(file_name)
print('Extract completed!')

the file download was done successfully but I that the error showed in the extraction part as the following error:

Download completed!
---------------------------------------------------------------------------
BadZipFile                                Traceback (most recent call last)
<ipython-input-5-6646b94bdb6f> in <module>()
      6 
      7 os.makedirs(DATASET_DIR, exist_ok=True)
----> 8 with zipfile.ZipFile(file_name, 'r') as zip_ref:
      9   zip_ref.extractall(DATASET_DIR)
     10 os.remove(file_name)

/usr/lib/python3.6/zipfile.py in __init__(self, file, mode, compression, allowZip64)
   1129         try:
   1130             if mode == 'r':
-> 1131                 self._RealGetContents()
   1132             elif mode in ('w', 'x'):
   1133                 # set the modified flag so central directory gets written

/usr/lib/python3.6/zipfile.py in _RealGetContents(self)
   1196             raise BadZipFile("File is not a zip file")
   1197         if not endrec:
-> 1198             raise BadZipFile("File is not a zip file")
   1199         if self.debug > 1:
   1200             print(endrec)

BadZipFile: File is not a zip file

Any suggestions?

Your Python code looks correct except for the value of drive_url .

The error message indicates that the file named file_name is not a ZIP file. urlretrieve may have downloaded something else, such as an error page or a file of a different format. Maybe your drive_url is incorrect (ie it's not a download link use by your browser, but a web page link), or the uploader has deleted the file from Google Drive in the meantime, or you haven't specified the necessary cookies for the download.

Download drive_url using your web browser, and look at the downloaded file (eg use file command on Linux).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM