[Python] Creating a 'for' or 'if-else' selector for checking dynamically sized python List

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to mask 8752 images with transformers like this

    from transformers import pipeline
    from PIL import Image
    import requests
    import cv2

    import numpy as np
    from matplotlib import pyplot as plt


    semantic_segmentation_nvidia = pipeline("image-segmentation", "nvidia/segformer-b0-finetuned-ade-512-512")
    jpeg_im = None
    a = 0
    mask_i = 0

    f = open("masking_log.txt", "w")


    for im in large_image_stack_512:
    i=0
    while(i == 0):

    jpeg_im = Image.open(os.path.join(ROOT_DIR,im))
    print(os.path.join(ROOT_DIR,im))
    # Semantic segmentation
    segmentation = semantic_segmentation_nvidia(jpeg_im)
    print("the length of current segmentation labels are: ", len(segmentation))
    water_mask_label = segmentation[mask_i]["label"]
    print(water_mask_label)
    print("here")
    if (water_mask_label == "water"):
    print("Successful labelling at: ", mask_i)
    water_mask = segmentation[mask_i]["mask"]
    print("here")
    imar = np.asarray(water_mask)
    print(water_mask_label)
    print("type im (array)", type(imar))
    f.write("image " + str(a) + "\nsuccess-label at " + str(mask_i) + "\nwith dir: " + str(im) + "\n with mask labeled as: " + str(water_mask_label) + '\n\n')
    plt.imsave('D:\..\Data\'+'img_'+str(a)+'.jpg', imar, cmap="gray")
    i=1
    a+=1
    mask_i= 0
    semantic_jpeg = None
    imar = None
    water_mask = None
    water_mask_label = None
    segmentation = None
    water_mask_label = None
    else:
    print("not water")
    if (mask_i < len(segmentation)):
    mask_i += 1
    else:
    f.write("image " + str(a) + "\n unsuccess-labelling (has no 'water' label)" + "final mask_i value: " + str(mask_i) + "\nwith dir: " + str(im) + "\n check later " + + '\n\n')
    print("masking fails, check later image" + im)
    i = 1
    continue


    #plt.imshow(water_mask)
    #plt.show()
    #print("type jpeg_im (jpeg)", type(water_mask))
    continue

    #print(len(cropped))
    f.close()


    Each of segmentation = semantic_segmentation_nvidia(jpeg_im) will have different size of array, for example with this image I have 11 items inside it like this:

    The code (did this in jupyter notebook rows)

    a_512 = semantic_segmentation_nvidia(image_512)
    a_512


    the output, a_512 variable is a List with 11 items inside it

    [{'score': None,
    'label': 'wall',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'building',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'sky',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'tree',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'earth',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'water',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'fence',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'railing',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'bridge',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'ship',
    'mask': <PIL.Image.Image image mode=L size=512x512>},
    {'score': None,
    'label': 'pier',
    'mask': <PIL.Image.Image image mode=L size=512x512>}]


    To access things I need, probably the 'label', I need to access the PIL.Image.Imagedata from each List item like this

    (a_512[5]["mask"]


    In the codes above, I used a variable to represent 5 in this code because apparently each image has different order of when it detects 'water' as the label and thus the required mask.

    Because python can compare strings, I'm trying to make a for loop to check every items in the list of every 8752 images. Let's say some of the images can have 10-12 items like this output of the final run.

    D:\..\Data\cropped_512\img_2541.jpg
    the length of current segmentation labels are: 15
    car
    here
    not water
    D:\..\Data\cropped_512\img_2541.jpg
    the length of current segmentation labels are: 15
    water
    here
    Successful labelling at: 7
    here
    water
    type im (array) <class 'numpy.ndarray'>
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    wall
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    building
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    sky
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    tree
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    road
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    mountain
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    car
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    sea
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    fence
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    bridge
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    boat
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12
    ship
    here
    not water
    D:\..\Data\cropped_512\img_2542.jpg
    the length of current segmentation labels are: 12

    ---------------------------------------------------------------------------
    IndexError Traceback (most recent call last)
    Cell In[16], line 17
    15 segmentation = semantic_segmentation_nvidia(jpeg_im)
    16 print("the length of current segmentation labels are: ", len(segmentation))
    ---> 17 water_mask_label = segmentation[mask_i]["label"]
    18 print(water_mask_label)
    19 print("here")

    IndexError: list index out of range


    As you can see the index keeps increasing and didn't touch the last else of the if-else comparator, so it went out of bounds. I have tried this and using range on the for-loop but it didn't work and still went out of bounds. I put breaks and it stops the loop, I put continue it keeps went out of bounds.Which part did I do wrong and understood incorrectly about python comparator behavior?

    I also tried logging it into .txt file or give status with print but it didn't go that way and always go out of bounds. Will gladly add more details if needed. The image is 512 x 512 size.

    Continue reading...

Compartilhe esta Página