I am working to build a binary file containing multiple images with each image having an associated label.
The format I need is a slight variation of this (please see The CIFAR-10 dataset > Dataset layout > Binary version).
My dir structure is:
[main_folder] --- [ant]
- img1
- img2
- imgN
--- [bat]
- img1
- img2
- imgN
--- [cat]
- img1
- img2
- imgN
--- [dog]
- img1
- img2
- imgN
Each image is grayscale TIFF 32 x 32 pixels.
According to the reference above, the binary file should be sequentially:
<1 x label><256 x pixel>
<1 x label><256 x pixel>
etc...
Where the first byte is the label of the first image, which is a number in the range 0-3. The next 1024 bytes are the values of the pixels of the image.
My initial attempt allows me to loop across all images in the directory and extract their labels and pixels in lists.
import os
import cv2
from ddx_utils import *
src_dir = '../main_folder/'
categories = ['ant', 'bat', 'cat', 'dog']
pixels = []
labels = []
for dirpath, diames, files in os.walk(src_dir):
for f in files:
if not f.startswith('.'):
filepath = dirpath + os.sep + f
pixel = cv2.imread(filepath, 0)
segs = dirpath.rpartition('/')
label = categories.index(segs[-1])
pixels.append(pixel)
labels.append(label)
# combine labels and pixels into single binary file with format above
Do you have any suggestion on how I should tackle building the binary file?
