File size: 1,360 Bytes
3a298ab 3fe6fac 3a298ab 3fe6fac 78adffd 83b7fa3 aec538a 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 97624c8 3a298ab 89fa28d 39d8f72 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
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 # ์ด๋ฏธ์ง๋ฅผ 0์์ 1 ์ฌ์ด๋ก ์ ๊ทํํฉ๋๋ค.
img_array = np.expand_dims(img_array, axis=0) # ๋ฐฐ์น ์ฐจ์์ ์ถ๊ฐํฉ๋๋ค.
# ๋ชจ๋ธ๋ก ์์ธก์ ์ํํฉ๋๋ค.
predictions = model.predict(img_array)
# ์์ธก ๊ฒฐ๊ณผ ์ค์์ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ์ ํํฉ๋๋ค.
predicted_label = np.argmax(predictions)
# ๋ผ๋ฒจ์ ๋ฐํํฉ๋๋ค.
return predicted_label
# Gradio UI๋ฅผ ์์ฑํฉ๋๋ค.
iface = gr.Interface(fn=classify_image,
inputs=gr.Image(shape=(800, 600),
outputs="label", live=True)
# Gradio UI๋ฅผ ์์ํฉ๋๋ค.
iface.launch()
|