Код: Выделить всё
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: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()
Код (сделал это в строках блокнота Jupyter)
Код: Выделить всё
a_512 = semantic_segmentation_nvidia(image_512)
a_512
Код: Выделить всё
[{'score': None,
'label': 'wall',
'mask':
},
{'score': None,
'label': 'building',
'mask': },
{'score': None,
'label': 'sky',
'mask': },
{'score': None,
'label': 'tree',
'mask': },
{'score': None,
'label': 'earth',
'mask': },
{'score': None,
'label': 'water',
'mask': },
{'score': None,
'label': 'fence',
'mask': },
{'score': None,
'label': 'railing',
'mask': },
{'score': None,
'label': 'bridge',
'mask': },
{'score': None,
'label': 'ship',
'mask': },
{'score': None,
'label': 'pier',
'mask': }]
Код: Выделить всё
(a_512[5]["mask"]
Поскольку Python может сравнивать строки, я пытаюсь создать цикл for, чтобы проверять каждый элемент в списке каждого 8752 изображения. Допустим, некоторые изображения могут содержать 10–12 элементов, как показано в последнем выводе.
Код: Выделить всё
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)
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
Я также попробовал записать его в файл .txt или указать статус с помощью печати, но это не сработало и всегда выходило за пределы. При необходимости с удовольствием добавлю дополнительную информацию. Размер изображения 512 x 512.
Подробнее здесь: https://stackoverflow.com/questions/792 ... python-lis