Skip to content

Instantly share code, notes, and snippets.

@sepfy
Created April 19, 2019 05:35
Show Gist options
  • Save sepfy/686337b5ca835ad3d51bcb10c76e09b8 to your computer and use it in GitHub Desktop.
Save sepfy/686337b5ca835ad3d51bcb10c76e09b8 to your computer and use it in GitHub Desktop.
Preprocess image for Tensorflow
import cv2
import numpy as np
import os
def load_images(image_dir):
batch = np.empty((0, 224, 224, 3), dtype=np.float32)
for image_name in os.listdir(image_dir)[:]:
img_path = image_dir + "/" + image_name
#print(img_path)
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_LINEAR)
npimg = np.asarray(img, dtype=np.float32)
npimg = (npimg - 127.5)/127.5
npimg.resize(1, 224, 224, 3)
#print(npimg.shape)
#print(batch.shape)
batch = np.concatenate((batch, npimg), 0)
return batch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment