|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
import tensorflow as tf |
|
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation |
|
|
|
feature_extractor = SegformerFeatureExtractor.from_pretrained( |
|
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024") |
|
|
|
model = TFSegformerForSemanticSegmentation.from_pretrained( |
|
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024" |
|
) |
|
|
|
|
|
input_size = model.input_shape[1:3] |
|
|
|
|
|
def classify_image(img): |
|
|
|
img = img.resize(input_size) |
|
img_array = np.array(img) / 255.0 |
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
predictions = model.predict(img_array) |
|
|
|
|
|
predicted_label = np.argmax(predictions) |
|
|
|
|
|
return predicted_label |
|
|
|
|
|
iface = gr.Interface(fn=classify_image, |
|
inputs=gr.Image(shape=(800, 600), |
|
outputs="label", live=True) |
|
|
|
|
|
iface.launch() |
|
|