You are browsing as a guest. Sign up (or log in) to start making projects!

55m 52s logged

MIRA — Day 3 visualise data set

What I Built Today

Today I wrote visualize_dataset.py from scratch using the Python os, random, and Matplotlib documentation. The script counts images per class folder, samples up to 5 random images per class, and displays them in a labeled 4×5 grid. This is a sanity check before the model training you cannot train reliably on a dataset you have never inspected.

import os
import random
import cv2
import matplotlib.pyplot as plt

DATA_DIR = "../data/"
CLASSES = ['glass', 'metal', 'paper', 'plastic']
SAMPLES = 5

total_files = 0
for class_name in CLASSES:
    folder = os.path.join(DATA_DIR, class_name)
    num_files = len([f for f in os.listdir(folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))])
    print(f"{class_name:<10}: {num_files} images")
    total_files += num_files
print(f"{'Total':<10}: {total_files} images")

fig, axes = plt.subplots(len(CLASSES), SAMPLES, figsize=(15, 10))

for row, class_name in enumerate(CLASSES):
    folder = os.path.join(DATA_DIR, class_name)
    files = [f for f in os.listdir(folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
    sampled = random.sample(files, min(SAMPLES, len(files)))

    for col, filename in enumerate(sampled):
        img = cv2.imread(os.path.join(folder, filename))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        axes[row, col].imshow(img)
        axes[row, col].axis('off')
        if col == 0:
            axes[row, col].set_ylabel(class_name, fontsize=12)

plt.tight_layout()
plt.show()

How It Works

The script has two distinct parts. First, the counter loop iterates over each class folder using os.listdir(), filters for image file extensions, and accumulates a total. Second, the grid section uses plt.subplots() to create a fixed axes array indexed by axes[row, col], each cell addressed independently by class row and sample column. random.sample(files, min(SAMPLES, len(files))) guards against crashes when a folder has fewer than 5 images by capping k to whatever is available.
So if I only got three it just going to use three and not crash like it would normaly do for this part I ran my Code through ki afterwards and he told me to add it

The one conversion that matters: cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before passing to Matplotlib. OpenCV loads images in BGR channel order; Matplotlib expects RGB. Without this, all reds appear blue. I forgot this but when the pictures came out weird i remeberd the tutorial by Tech with Tim where he mentions it mutipile times I would advise everybody to watch his openCV turorial

Difficulty

Straightforward session. Coming from C/C++, the structure of iterating over a list of strings and using them as both display labels and filesystem paths felt natural immediately. plt.subplots() returning a 2D array of axes objects is the same concept as a 2D array in C — index by row and column. No significant blockers.

Resources used:

  • Matplotlib docs: pyplot.subplots, pyplot.imshow, Axes.set_ylabel
  • Python docs: os.path.join, os.listdir
  • OpenCV docs: cvtColor color conversion codes

Dataset Status

glass : 32 images
metal : 31 images
paper : 30 images
plastic : 33 images
Total : 126 images

Also all this images are not in the Github at the moment because these aren’t high quality and good images on which i can train my model

What’s Next

Dataset is captured and verified. Next is the first CNN training pipeline in TensorFlow/Keras, train_baseline.py. This means learning ml and taking my first steps into it . The target for the first run is a accuracy above 75%.

0
1

Comments 0

No comments yet. Be the first!