Skip to content

Instantly share code, notes, and snippets.

Created November 17, 2017 03:29
What would you like to do?
Basic function to decompress SVHN dataset .mat files into raw images
import cv2
import numpy as np
import os
import as sio
def write_files(mat_input_file, output_dir):
if not os.path.exists(mat_input_file):
raise Exception('Path does not exist: ' + mat_input_file)
if not os.path.exists(output_dir):
data = sio.loadmat(mat_input_file)
counts = np.empty((data['X'].shape[3],)).astype(np.uint8)
for i in range(data['X'].shape[3]):
truth = data['y'][i, 0]
if truth == 10:
truth = 0
counts[i] = truth
img = data['X'][..., i]
cv2.imwrite(os.path.join(output_dir, str(i + 1) + '.png'), img)
np.savetxt(os.path.join(output_dir, 'labels.csv'), counts, delimiter=",", fmt='%d')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment