I used OpenCV when converting images to npy format with TensorFlow, but I got stuck with an error, so I made a note.
Stupidly, I forgot that file I / O related functions would get stuck completely if I didn't handle exceptions when they failed. The imread function is one of them.
Below is a function that creates a dataset for tensorflow. The processing after reading the imread function is done with the None judgment. If you don't do this, you'll get moss on None, so you'll have to do this. (If you think about it, it's natural ...)
import os, sys
import numpy as np
import tensorflow as tf
import cv2
# config
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('imgdir', '/home/hoge/fuga/piyo', 'Image dataset path')
flags.DEFINE_string('imgclasses', 4, 'The num of image dataset classes')
IMAGE_SIZE = 84 # the size of image
TRAIN_IMG_DIRS = os.listdir(FLAGS.imgdir)
def main():
train_image, train_label = cvGetImageAndLabel()
#The code for learning is troublesome, so it's omitted.
def cvGetImageAndLabel():
images = []
labels = []
for i, d in enumerate(TRAIN_IMG_DIRS):
files = os.listdir(FLAGS.imgdir + '/' + d)
for f in files:
img = cv2.imread(FLAGS.imgdir + '/' + d + '/' + f)
#None judgment in this part
if not img is None:
img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation = cv2.INTER_AREA)
img = img.flatten().astype(np.float32)/255.0
images.append(img)
tmp = np.zeros(FLAGS.imgclasses)
tmp[i] = 1
labels.append(tmp)
return np.asarray(images, dtype=np.float32), np.asarray(labels, dtype=np.float32)
if __name__ == '__main__':
main()
I love the dirty code (?)
http://stackoverflow.com/questions/23628325/cv2-imread-checking-if-image-is-being-read