Importing image problem for use in Python

Viewed 36

I tried to build a model for image processing, in particular is distinguishing dogs from cats.

The dataset I got online is a collection of 12,500 dogs and cats images, all in jpg format, and I can open all of them on my native computer. I then proceed to resize all of them into 300x300 images, after the resize process i can still open all of my images normally.

After that, I compressed them into a zip file, then upload to google drive so that I can access them using Google Golab. But then, my code return an error that said cannot identify image file <_io.BytesIO object at 0x7f44e46142f0>, so I made a script to determine which images were corrupted so that I can remove them. The script looks like this:

import os
import PIL
import zipfile

from os import listdir
from PIL import Image
from google.colab import drive

drive.mount('/content/drive')

dir = 'dogcatdata/train'
zip_ref = zipfile.ZipFile("/content/drive/MyDrive/test/dogcatdata.zip", 'r')
zip_ref.extractall(dir)
zip_ref.close

count = 0
for filename in listdir('/content/dogcatdata/train/dogcatdata/train/dog'):
    try:
      img = Image.open('./'+filename)
      img.verify()
    except (IOError, SyntaxError) as e:
      count = count + 1
print(count)

The output indicated that all 12,500/12,500 of my images of dogs were corrupted. But I could still open them normally on my computer.

I made my dataset public for anyone to inspect:

https://drive.google.com/file/d/1wGPG1sIkRspLFwke7e2fyxmM6Mbs_Ec5/view?usp=sharing

1 Answers

You just have wrong path in line 19. The following code works and gives 0 corrupted images:

import os
import PIL
import zipfile

from os import listdir
from PIL import Image
from google.colab import drive

drive.mount('/content/drive')

dir = 'dogcatdata/train'
zip_ref = zipfile.ZipFile("/content/drive/MyDrive/test/dogcatdata.zip", 'r')
zip_ref.extractall(dir)
zip_ref.close

count = 0
for filename in listdir('/content/dogcatdata/train/dogcatdata/train/dog'):
    try:
      img = Image.open('/content/dogcatdata/train/dogcatdata/train/dog/'+filename)
      img.verify()
    except (IOError, SyntaxError) as e:
      count = count + 1
print(count)

Output:

0
Related