Mar-02-2019, 09:05 PM
Hi everyone. I'm pretty new at Python, and am struggling with something. As you can see from the code attached (line 59 - 77 is what I'm refering to in this post), I am trying to re-name all the files in a particular folder to fit a pattern for a project I'm making. Before running the code, the images are all named "testimage(i), i being the number in the folder. The problem I have is that while this code does run, and does rename them the way I want, the order in which it is going through them is weird. It seems to do the first one called "testimage(1)", followed by "testimage(10)", through to 19, then it just starts getting like every tenth number first. I'm really confused why this is happening, but essentially I want it to just go through the images in the order they appear in the explorer so that it does "testimage(1), testimage(2)" etc all the way through. Any suggestions would be really great. Thanks!
from random import shuffle import os import glob import sys import cv2 import numpy as py import matplotlib.pyplot as plt #import skimage.io as io import tensorflow as tf import time def _int64_feature(value): # takes value and wraps it a int64 list return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def load_image(addr): #read an image and resize to (244, 244) #cv2 load images as BGR, convert it to RGB img = cv2.imread(addr) if img is None: return None img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_CUBIC) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) return img def createDataRecord(out_filename, addrs, labels): # open the TFRecords file writer = tf.python_io.TFRecordWriter(out_filename) for i in range(len(addrs)): # print how many images are saved every 1000 images if not i % 1000: print('Train data: {}/{}'.format(i, len(addrs))) sys.stdout.flush() # Load the image img = load_image(addrs[i]) label = labels[i] if img is None: continue # Create a feature feature = { 'image_raw': _bytes_feature(img.tostring()), 'label': _int64_feature(label) } # Create an example protocol buffer example = tf.train.Example(features=tf.train.Features(feature=feature)) # Serialize to string and write on the file writer.write(example.SerializeToString()) writer.close() sys.stdout.flush() train_path = 'E:/Documents/Year 3/Advanced Technology/Task3/MapScraper/MapsImageScraper/IMAGES_SORTED/*/*.png' width = 38 #dimensions of single split map area in tiles height = 18 imgPath = 'E:/Documents/Year 3/Advanced Technology/Task3/MapScraper/MapsImageScraper/IMAGES_TEST_MAP/images/' h = 1 w = 1 for filename in os.listdir(imgPath): #finds the files in the path dst = str(h) + "_" + str(w) + ".png" #sets rename for map positions src = imgPath + filename dst = imgPath + dst #sets the output images to the same location os.rename(src, dst) time.sleep(1) w += 1 if w > width: w = 0 h += 1 # read addresses and labels from the folders addrs = glob.glob(train_path) labels = [0 if 'BUILDING' in addr #numbers to represent each label type else 1 if 'FOREST' in addr else 2 if 'GRASS' in addr else 3 if 'HOUSE' in addr else 4 if 'RIVER' in addr else 5 if 'ROAD' in addr else 6 if 'SAND' in addr else 7 if 'WATER' in addr else 8 for addr in addrs] # to shuffle data c = list(zip(addrs, labels)) shuffle(c) addrs, labels = zip(*c) # Divide the data into 60% train, 20% validation, and 20% test train_addrs = addrs[0:int(0.6*len(addrs))] train_labels = labels[0:int(0.6*len(labels))] val_addrs = addrs[int(0.6*len(addrs)):int(0.8*len(addrs))] val_labels = labels[int(0.6*len(addrs)):int(0.8*len(addrs))] test_addrs = addrs[int(0.8*len(addrs)):] test_labels = labels[int(0.8*len(labels)):] createDataRecord('train.tfrecords', train_addrs, train_labels) createDataRecord('val.tfrecords', val_addrs, val_labels) createDataRecord('test.tfrecords', test_addrs, test_labels)