Advertisement
dan-masek

Untitled

Oct 3rd, 2018
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. ID_APP_MAP_FILENAME = 'miguel/train.csv'
  5. IMAGE_PATH_FORMAT = 'miguel/%04d_%s.png'
  6.  
  7. def load_id_app_map(filename):
  8.     result = {}
  9.     with open(filename) as f:
  10.         lines = f.read().splitlines()[1:]
  11.         for line in lines:
  12.             parts = map(int, line.split(','))
  13.             if len(parts) == 2:
  14.                 result[parts[0]] = parts[1]
  15.             elif len(parts) == 1: # no label
  16.                 result[parts[0]] = -1
  17.  
  18.     return result
  19.    
  20. def gen_label_array(label, label_count):
  21.     label_flags = np.zeros(label_count, np.uint8)
  22.     if label >= 0: # -1 signifies no label
  23.         label_flags[label] = 1
  24.     return label_flags
  25.  
  26. def load_flat_image(filename):
  27.     image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
  28.     if image is None:
  29.         raise RuntimeError('Failed to load image "%s".' % filename)
  30.     return image.flatten()
  31.  
  32. id_app_map = load_id_app_map(ID_APP_MAP_FILENAME)
  33.  
  34. label_count = np.max(id_app_map.values()) + 1
  35.  
  36. rows = []
  37. for id,appliance in sorted(id_app_map.items()):
  38.     labels = gen_label_array(appliance, label_count)
  39.     flattened_c = load_flat_image(IMAGE_PATH_FORMAT % (id, 'c'))
  40.     flattened_v = load_flat_image(IMAGE_PATH_FORMAT % (id, 'v'))
  41.     rows.append(np.hstack([labels, flattened_c]))
  42.     rows.append(np.hstack([labels, flattened_v]))
  43.  
  44. data = np.vstack(rows)
  45.  
  46. print data.shape
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement