How to Use CSV Files to Upload Images and Tag Them According to File Names

hamza13444 0 Reputation points
2023-02-16T09:25:47.8166667+00:00

Hello,

I have a couple of questions;

  • Firstly, I would like to know if it's possible to use a CSV file to upload my images and tags in a more efficient way.
  • Secondly, can you advise on how I can tag my images according to their file names? Thank you. Hamza
Azure AI Custom Vision
Azure AI Custom Vision
An Azure artificial intelligence service and end-to-end platform for applying computer vision to specific domains.
220 questions
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 42,286 Reputation points Microsoft Employee
    2023-02-16T12:04:37.5766667+00:00

    hamza13444 For the first part of your question, I believe you want to use a CSV file which maps an image to an image path and a tag name. If this is correct, then there is no direct way to just upload this CSV file and get the images uploaded in your project. An easier way to get this accomplished is to store your images on a local path in folders based on the tag that needs to be added to them and then use the programmatic way to upload the images and add a tag. This can be done through any SDK client ex: python client as in this quickstart.

    For example, here we are using an Images root directory and having the images related to the tags we want in our project in separate folders i.e Hemlock and Japanese Cherry.

    User's image

    The rest of the steps mentioned uses a list to read the images from the path and adds a tag to create a ImageFileCreateEntry object list and uploads them with one go using trainer.create_images_from_files()

    base_image_location = os.path.join (os.path.dirname(__file__), "Images")
    
    print("Adding images...")
    
    image_list = []
    
    for image_num in range(1, 11):
        file_name = "hemlock_{}.jpg".format(image_num)
        with open(os.path.join (base_image_location, "Hemlock", file_name), "rb") as image_contents:
            image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[hemlock_tag.id]))
    
    for image_num in range(1, 11):
        file_name = "japanese_cherry_{}.jpg".format(image_num)
        with open(os.path.join (base_image_location, "Japanese_Cherry", file_name), "rb") as image_contents:
            image_list.append(ImageFileCreateEntry(name=file_name, contents=image_contents.read(), tag_ids=[cherry_tag.id]))
    
    upload_result = trainer.create_images_from_files(project.id, ImageFileCreateBatch(images=image_list))
    if not upload_result.is_batch_successful:
        print("Image batch upload failed.")
        for image in upload_result.images:
            print("Image status: ", image.status)
        exit(-1)
    

    Secondly, tagging the images with file names should be avoided because you might end up with a lot of tags to manage. If your images belong to a certain class, tag them under a certain class by creating a tag using trainer.create_tag()

    I hope this helps!!

    0 comments No comments