Lesson 2 - Gradio App for Image Segmentation

I am trying to convert the Image Segmentation example from lesson 1 into a Gradio app and the generated image looks nothing like I had in my notebook.

Has anyone converted the results of predict into an image for Gradio to display in the output?

This is the function I am using

from fastai.vision.all import *
import gradio as gr
import timm

from label_helper import LabelHelper

learn = load_learner(‘model.pkl’)

def segment_image(img):
if isinstance(img, np.ndarray):
img = Image.fromarray(img)

# Convert the input image to RGB mode
img = img.convert("RGB")

# Create PIL image from the input and get the segmentation results
img = PILImage.create(img)
results = learn.predict(img)

# Get the mask and normalize it
mask = results[0].numpy()
mask = (mask * 255).astype(np.uint8)

# Convert mask to RGB and ensure it matches the size of the original image
mask_img = Image.fromarray(mask).convert("RGB").resize(img.size, Image.NEAREST)

# Ensure the mask image has the same size as the original image
if mask_img.size != img.size:
    mask_img = mask_img.resize(img.size, Image.NEAREST)

# Overlay the mask on the original image
overlayed_img = Image.blend(img, mask_img, alpha=0.7)

return overlayed_img

I’m not familiar with Segmentation problems but with Claude’s help I think I found a solution for you. See this Colab notebook.

It seems like you have to apply your own colors to the segmentation mask (results[0]) before blending it with the original image to get the desired result. In the screenshot below rgb_mask is your mask with colors assigned to it: