File size: 1,222 Bytes
3a298ab 89fa28d 3a298ab 89fa28d 83b7fa3 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d 3a298ab 89fa28d |
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 |
import gradio as gr
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
# ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ์ ๋ถ๋ฌ์ต๋๋ค. ์ฌ๊ธฐ์์๋ ๊ฐ๋จํ ์์ ๋ชจ๋ธ์ ์ฌ์ฉํฉ๋๋ค.
model = load_model('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="image", outputs="label", live=True)
# Gradio UI๋ฅผ ์์ํฉ๋๋ค.
iface.launch()
|