homework3 / app.py
dakkoong's picture
Update app.py
97624c8
raw
history blame
1.36 kB
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()