Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 KB | None | 0 0
  1. import os
  2. import cv2
  3. from matplotlib import pyplot as plt
  4. import numpy as np
  5. import pickle
  6. import gzip
  7. import lzma
  8. import pandas
  9. import random
  10. signnames = pandas.read_csv('signnames.csv')
  11. %matplotlib inline
  12. DATASET = 'train'
  13. train_images = os.walk('dataset/'+DATASET)
  14. train_image_list = []
  15. PATH_TO_IMAGE = 'dataset/'+DATASET+'/'
  16. RESIZE_IMG_SHAPE = 32
  17. df = pandas.read_csv('germantraffic'+DATASET+'.csv')
  18. for i in train_images:
  19.     for item in i:
  20.         if type(item) == str:
  21.             SUBFOLDER_PATH = item
  22. #             print('subfolder path',item)
  23.         if type(item) == list:
  24.             if len(item) > 50:
  25.                 for image in item:
  26.                     if '.ppm' in image:
  27.                         folder_name = SUBFOLDER_PATH+"/"+image
  28.                         for index,filename in enumerate(df['filename']):
  29.                             if filename == folder_name[-21:]:
  30.                                 for classindex, classname in enumerate(signnames['class']):
  31.                                     if df['class'][index] == classname:
  32.                                         label = classindex
  33.                         current_image = cv2.imread(SUBFOLDER_PATH +"/" +  image)
  34.                         current_image = current_image[:,:,::-1]
  35. #                         current_image = current_image/255.0
  36.                         h,w,a = current_image.shape
  37.                         RESIZE_IMG_SHAPE = 32
  38.                         blackbar_horizontal = 0
  39.                         blackbar_vertical = 0
  40.                         if h > w:
  41.                             h_larger = True
  42.                             resized_h = RESIZE_IMG_SHAPE
  43.                             resized_w = int(RESIZE_IMG_SHAPE/h*w)
  44.                             resized = cv2.resize(current_image,(resized_w,resized_h))
  45.                             blackbar_horizontal = (RESIZE_IMG_SHAPE - resized_w)/2
  46.                             for i in range(int(blackbar_horizontal)):
  47.                                 resized = np.insert(resized,[0,resized_w],0,axis=1)
  48.                                 resized_w+=1
  49.                             if int(blackbar_horizontal / 0.5) % 2 == 1:
  50.                                 resized = np.insert(resized,0,0,axis=1)
  51.                         if w > h:
  52.                             w_larger = True
  53.                             resized_w = RESIZE_IMG_SHAPE
  54.                             resized_h = int(RESIZE_IMG_SHAPE/w*h)
  55.                             resized = cv2.resize(current_image,(resized_w,resized_h))
  56.                             blackbar_vertical = (RESIZE_IMG_SHAPE - resized_h)/2
  57.                             for i in range(int(blackbar_vertical)):
  58.                                 resized = np.insert(resized,[0,resized_h],0,axis=0)
  59.                                 resized_h+=1
  60.                             if int(blackbar_vertical / 0.5) % 2 == 1:
  61.                                 resized = np.insert(resized,0,0,axis=0)
  62.                         if w == h:
  63.                             w_equal_h = True
  64.                             resized = cv2.resize(current_image,(RESIZE_IMG_SHAPE,RESIZE_IMG_SHAPE))                            
  65. #                         train_image_list.append((resized.astype(np.float16), label))
  66.                         train_image_list.append((resized,label))
  67. #                         train_image_list.append(resized.astype(np.float16))
  68.                        
  69. print(len(train_image_list))
  70. random.shuffle(train_image_list)
  71. pickle_out = lzma.open(f"{DATASET}_data_not_normalized.pickle","wb",preset=9)
  72. pickle.dump(train_image_list, pickle_out)
  73. pickle_out.close()
  74. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement