Skip to content

Instantly share code, notes, and snippets.

Created July 18, 2018 20:05
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chricke/56af8cbd7a9cdf03a8b3667ae70ebff0 to your computer and use it in GitHub Desktop.
Preprocess files for training
import os
import cv2
import pandas as pd
root_dir = os.getcwd()
file_list = ['train.csv', 'val.csv']
image_source_dir = os.path.join(root_dir, 'data/images/')
data_root = os.path.join(root_dir, 'data')
for file in file_list:
image_target_dir = os.path.join(data_root, file.split(".")[0])
# read list of image files to process from file
image_list = pd.read_csv(os.path.join(data_root, file), header=None)[0]
print("Start preprocessing images")
for image in image_list:
# open image file
img = cv2.imread(os.path.join(image_source_dir, image))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# perform transformations on image
b = cv2.distanceTransform(img, distanceType=cv2.DIST_L2, maskSize=5)
g = cv2.distanceTransform(img, distanceType=cv2.DIST_L1, maskSize=5)
r = cv2.distanceTransform(img, distanceType=cv2.DIST_C, maskSize=5)
# merge the transformed channels back to an image
transformed_image = cv2.merge((b, g, r))
target_file = os.path.join(image_target_dir, image)
print("Writing target file {}".format(target_file))
cv2.imwrite(target_file, transformed_image)
Copy link

Armyke commented May 16, 2019

I would suggest to insert these lines after the definition of the image_target_dir to create the folder in case someone didn't do that manually.

if not os.path.exists(image_target_dir):

Anyway appreciate your work, I found the article on Medium pretty useful. Keep it up and have a good day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment