Creating keypoint labels from filename using image_dataset_from_directory

In the directory, images have names in the format ‘fileNo-x-y’. I’m trying to label the images with the x and y coordinates, but I’m getting issues due to the keypoint_labels function. Here’s what I’ve got so far:

train_ds = image_dataset_from_directory(
    'C:/Users/joshu/Desktop/data',
    label_mode='int',
    image_size=[60, 80],
    interpolation='nearest',
    validation_split=0.2,
    subset='training',
    batch_size=64,
    seed=42, 
)
valid_ds = image_dataset_from_directory(
    'C:/Users/joshu/Desktop/data',
    label_mode='int',
    image_size=[60, 80],
    interpolation='nearest',
    validation_split=0.2,
    subset='validation',
    batch_size=64,
    seed=42, 
    
)
  
# Data Pipeline
def convert_to_float(image, label):
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    return image, label
  
def keypoint_labels(image, label):
    image_string = tf.io.read_file(image)
    _, x, y = file_name.stem.split('-')
    label = (int(x), int(y))
    return image, (x, y)
    
  
AUTOTUNE = tf.data.experimental.AUTOTUNE
  
train = (
    train_ds.map(keypoint_labels)
    .map(convert_to_float)
    .cache()
    .prefetch(buffer_size=AUTOTUNE)
)
valid = (
    valid_ds.map(keypoint_labels)
    .map(convert_to_float)
    .cache()
    .prefetch(buffer_size=AUTOTUNE)
)

Any help or advice would be appreciated. If you need any more info let me know!

Hi @jkaars, you can get the labels from fileNo-x-y by using the code below. For example, i have a directory /content/Untitleda/Untitledb which contains images with names gold-1-1.jpeg,hen-2-2.jpeg,unknown-4-4.jpeg. You can get x-y using

directory = "/content/Untitleda/Untitledb"
ds_train = tf.data.Dataset.list_files(str(pathlib.Path(directory,"*.jpeg")))

def keypoint_labels(file_path):
  for f in file_path:
    xy=tf.strings.split(f,sep='-').numpy()
    xy=tf.strings.split(xy,sep='.').numpy()
    print(int(xy[-2]),int(xy[-2]))

keypoint_labels(ds_train)
#output
1 1
2 2
4 4

Thank you.