Код: Выделить всё
from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation, SegformerImageProcessor, SegformerForSemanticSegmentation
from PIL import Image
import requests
import cv2
import numpy as np
from matplotlib import pyplot as plt
# importing image to use
image = Image.open("D:\img_1.jpg")
# Create mask from a pretrained model
# Loading a single model for all three tasks
processor_shilab = OneFormerProcessor.from_pretrained("shi-labs/oneformer_ade20k_swin_large")
model_shilab = OneFormerForUniversalSegmentation.from_pretrained("shi-labs/oneformer_ade20k_swin_large")
panoptic_inputs = processor_shilab(images=image, task_inputs=["panoptic"], return_tensors="pt")
panoptic_outputs = model_shilab(**panoptic_inputs)
#predicted_panoptic_map in torch.Tensor type
predicted_panoptic_map = processor_shilab.post_process_panoptic_segmentation(panoptic_outputs, target_sizes=[image.size[::-1]])[0]["segmentation"]
#color array
color = np.array([0, 255, 0], dtype="uint8")
masked_image = np.where(predicted_panoptic_map, image)
Код: Выделить всё
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[37], line 3
1 color = np.array([0, 255, 0], dtype="uint8")
----> 3 masked_image = np.where(predicted_panoptic_map, color, grey)
ValueError: operands could not be broadcast together with shapes (1080,1080) (3,) (1080, 1080, 3)
Но после уменьшения размера я получил эту ошибку
Код: Выделить всё
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[37], line 3
1 color = np.array([0, 255, 0], dtype="uint8")
----> 3 masked_image = np.where(predicted_panoptic_map, color, grey)
ValueError: operands could not be broadcast together with shapes (1080,1080) (3,) (1080,1080)
Код: Выделить всё
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[38], line 3
1 color = np.array([0, 255, 0], dtype="uint8")
----> 3 masked_image = np.where(predicted_panoptic_map, grey)
ValueError: either both or neither of x and y should be given
Обратите внимание, что маска уже цветная ? что-то вроде этого
[![plotted-mask with matplotlib][1]][1]
Изменить 4: маска не цветная, я думаю, это из matplotlib
Редактировать 5: редактирование только для потомков, будет читать пустые документы, как предложено
Подробнее здесь: https://stackoverflow.com/questions/792 ... -yet-fails
Мобильная версия