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()