Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import cv2
- ID_APP_MAP_FILENAME = 'miguel/train.csv'
- IMAGE_PATH_FORMAT = 'miguel/%04d_%s.png'
- def load_id_app_map(filename):
- result = {}
- with open(filename) as f:
- lines = f.read().splitlines()[1:]
- for line in lines:
- parts = map(int, line.split(','))
- if len(parts) == 2:
- result[parts[0]] = parts[1]
- elif len(parts) == 1: # no label
- result[parts[0]] = -1
- return result
- def gen_label_array(label, label_count):
- label_flags = np.zeros(label_count, np.uint8)
- if label >= 0: # -1 signifies no label
- label_flags[label] = 1
- return label_flags
- def load_flat_image(filename):
- image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
- if image is None:
- raise RuntimeError('Failed to load image "%s".' % filename)
- return image.flatten()
- id_app_map = load_id_app_map(ID_APP_MAP_FILENAME)
- label_count = np.max(id_app_map.values()) + 1
- rows = []
- for id,appliance in sorted(id_app_map.items()):
- labels = gen_label_array(appliance, label_count)
- flattened_c = load_flat_image(IMAGE_PATH_FORMAT % (id, 'c'))
- flattened_v = load_flat_image(IMAGE_PATH_FORMAT % (id, 'v'))
- rows.append(np.hstack([labels, flattened_c]))
- rows.append(np.hstack([labels, flattened_v]))
- data = np.vstack(rows)
- print data.shape
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement