Skip to content

Commit

Permalink
Add OpenCV for images preprocessing
Browse files Browse the repository at this point in the history
feat: Add OpenCV functions for image reading and resizing for speed up train batch generation (~12%).
  • Loading branch information
NikolasEnt authored Dec 28, 2017
1 parent 9cd8c3c commit d0ca86f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import cv2
import random
import numpy as np
import os.path
Expand Down Expand Up @@ -103,14 +104,14 @@ def get_batches_fn(batch_size):
gt_images = []
for image_file in image_paths[batch_i:batch_i+batch_size]:
gt_image_file = label_paths[os.path.basename(image_file)]
image = scipy.misc.imread(image_file)
gt_image = scipy.misc.imread(gt_image_file)
image = cv2.imread(image_file)
gt_image = cv2.imread(gt_image_file)
#image, gt_image = random_crop(image, gt_image) #Random crop augmentation
image = scipy.misc.imresize(image, image_shape)
image = cv2.resize(image, image_shape)
contr = random.uniform(0.85, 1.15) # Contrast augmentation
bright = random.randint(-45, 30) # Brightness augmentation
image = bc_img(image, contr, bright)
gt_image = scipy.misc.imresize(gt_image, image_shape)
gt_image = cv2.resize(gt_image, image_shape)
gt_bg = np.all(gt_image == background_color, axis=2)
gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
gt_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)
Expand Down
13 changes: 7 additions & 6 deletions helper_cityscapes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import cv2
import random
import numpy as np
import os.path
Expand Down Expand Up @@ -90,8 +91,8 @@ def bc_img(img, s = 1.0, m = 0.0):
def load_data():
image_paths = os.listdir(gt_dataset_dir)
for image_file in image_paths:
gt_image = scipy.misc.imread(os.path.join(gt_dataset_dir, image_file))
image = scipy.misc.imread(os.path.join(train_dataset_dir, image_file[:-5]+'.png'))
gt_image = cv2.imread(os.path.join(gt_dataset_dir, image_file))
image = cv2.imread(os.path.join(train_dataset_dir, image_file[:-5]+'.png'))
gt_input.append(gt_image)
img_input.append(image)

Expand All @@ -115,11 +116,11 @@ def get_batches_fn(batch_size):
images = []
gt_images = []
for image_file in image_paths[batch_i:batch_i+batch_size]:
gt_image = scipy.misc.imread(os.path.join(gt_dataset_dir, image_file))
image = scipy.misc.imread(os.path.join(train_dataset_dir, image_file[:-5]+'.png'))
gt_image = cv2.imread(os.path.join(gt_dataset_dir, image_file))
image = cv2.imread(os.path.join(train_dataset_dir, image_file[:-5]+'.png'))
image, gt_image = random_crop(image, gt_image) #Random crop augmentation
image = scipy.misc.imresize(image, image_shape)
gt_image = scipy.misc.imresize(gt_image, image_shape)
image = cv2.resize(image, image_shape)
gt_image = cv2.resize(gt_image, image_shape)
contr = random.uniform(0.85, 1.15) # Contrast augmentation
bright = random.randint(-40, 30) # Brightness augmentation
image = bc_img(image, contr, bright)
Expand Down

0 comments on commit d0ca86f

Please sign in to comment.