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

2h 9m 6s logged

MIRA — Day 2: Dataset Capture Pipeline

What I Built

Wrote capture_frame.py from scratch using only the OpenCV docs, no tutorials for the code, no AI-generated code. The script opens a live webcam feed and lets me build my own labeled image dataset by pressing keystroke shortcuts to save frames directly into categorized folders.

Evolution of the Capture Logic

In my first draft, I managed file naming with a manual integer counter. It worked, but had a critical flaw: every time the script restarted, the counter reset to 1 and began overwriting existing files. Also through out this project I have always specified the Data type because i got advice from a friend to do that.

Initial counter approach:

i = int(1)
if key == ord('1'):
    filepath = os.path.join(glass_folder, f'frame_{i}.jpg')
    cv2.imwrite(filepath, frame)
    i += 1

I replaced this with timestamp-based naming using datetime.now().strftime("%Y%m%d_%H%M%S"), which ensures every file has a unique name regardless of how many times the script restarts:

glass_20260625_143022.jpg
glass_20260625_143022.jpg

Make it more efficent to scale

My first working draft had four separate folder variables and a long if/elif chain, one block per material class. Functional, but not scalable because this would mean: adding a fifth class would mean copy-pasting another entire block.

First draft, repetitive structure:

glass_folder  = "../data/glass/"
metal_folder  = "../data/metal/"


if key == ord('1'):
    filepath = os.path.join(glass_folder, f'glass_{timestamp}.jpg')
    cv2.imwrite(filepath, frame)
elif key == ord('2'):
    filepath = os.path.join(metal_folder, f'metal_{timestamp}.jpg')
    cv2.imwrite(filepath, frame)

Refactored:

CLASSES = {
    '1': ('glass',   '../data/glass/'),
    '2': ('metal',   '../data/metal/'),
}

key_char = chr(key)
if key_char in CLASSES:
    label, folder = CLASSES[key_char]
    filepath = os.path.join(folder, f'{label}_{timestamp}.jpg')
    cv2.imwrite(filepath, frame)

Adding a fifth class now takes one line in the dictionary. The save logic doesn’t change at all which is pretty neat this was quite difficult because I never worked with classes like this but I managed to do it after googling and asking AI how to set up Classes but not providing it any context so I can write it myself.

Coming from C/C++

One thing I found difficult to utilise where f strings. I dont know if a equalivent exists in C/C++ but I never used it so it was quite weird utilising them but they’re very convient and this is another positive aspect of the high abstraction.

What’s Next

The dataset capture pipeline is working. Next session I will write visualize_dataset.py, a script that loads random samples from each class folder, displays them in a grid, and prints the class distribution as a count per folder. This will then be the basis for my CNN pipelane


Resources & Documentation

0
1

Comments 0

No comments yet. Be the first!