Found 0 Defected examples, But In the directory there are 10 images | Found 0 Perfect examples |
I am trying to read images from a directory. There are some images, but the code is saying 0 images.
I checked with os.listdir(r'C:\Users\hafizurr\Documents\classifier\dataset\Perfect'), which is showing all of the images. But my code is now showing. I am putting my codes below. Please take a look and help me to figure out the issue. Hoping for your help:
class_names = ['Perfect', 'Defected']
root_dir = r'C:\Users\hafizurr\Documents\classifier\dataset'
source_dirs = ['Perfect', 'Defected']`

`if os.path.isdir(os.path.join(root_dir, source_dirs[1])):
    os.mkdir(os.path.join(root_dir, 'test'))`

    for i, d in enumerate(source_dirs):
        os.rename(os.path.join(root_dir, d), os.path.join(root_dir, class_names[i]))

    for c in class_names:
        os.mkdir(os.path.join(root_dir, 'test', c))

    for c in class_names:
        images = [x for x in os.listdir(os.path.join(root_dir, c)) if x.lower().endswith('.JPG')]
        selected_images = random.sample(images, 0)
        for image in selected_images:
            source_path = os.path.join(root_dir, c, image)
            target_path = os.path.join(root_dir, 'test', c, image)
            shutil.move(source_path, target_path)


`class ImgDataset(torch.utils.data.Dataset):
    def __init__(self, image_dirs, transform):
        def get_images(class_name):
            images = [x for x in os.listdir(image_dirs[class_name]) if x.lower().endswith('.JPG')]
            print(f'Found {len(images)} {class_name} examples')
            return images
        self.images = {}
        self.class_names = ['Perfect', 'Defected']
        for class_name in self.class_names:
            self.images[class_name] = get_images(class_name)  
        self.image_dirs = image_dirs
        self.transform = transform`
        
    
    def __len__(self):
        return sum([len(self.images[class_name]) for class_name in self.class_names])
    
    
    def __getitem__(self, index):
        class_name = random.choice(self.class_names)
        index = index % len(self.images[class_name])
        image_name = self.images[class_name][index]
        image_path = os.path.join(self.image_dirs[class_name], image_name)
        image = Image.open(image_path).convert('RGB')
        return self.transform(image), self.class_names.index(class_name)

train_transform = torchvision.transforms.Compose([
    torchvision.transforms.Resize(size=(224, 224)),
    torchvision.transforms.RandomHorizontalFlip(),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

test_transform = torchvision.transforms.Compose([
    torchvision.transforms.Resize(size=(224, 224)),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

train_dirs = {
    'Perfect': f'{root_dir}/Perfect',
    'Defected': f'{root_dir}/Defected'    
}

train_dataset = ImgDataset(train_dirs, train_transform)


That's because x.lower().endswith('.JPG') will always be false!
You could do x.lower().endswith('.jpg') instead.
Alternatively, use imghdr.what to check the image type.
