Skip to content

Instantly share code, notes, and snippets.

@stormont
Created November 17, 2017 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormont/87256d09eaf35634a863752c29aee484 to your computer and use it in GitHub Desktop.
Save stormont/87256d09eaf35634a863752c29aee484 to your computer and use it in GitHub Desktop.
Basic function to decompress SVHN dataset .mat files into raw images
import cv2
import numpy as np
import os
import scipy.io 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):
os.mkdir(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