Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
|
|
5 |
|
6 |
# Segformer ๋ชจ๋ธ ๋ฐ feature extractor ๋ถ๋ฌ์ค๊ธฐ
|
7 |
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
@@ -11,17 +12,21 @@ model = TFSegformerForSemanticSegmentation.from_pretrained(
|
|
11 |
|
12 |
# ๋ชจ๋ธ ์์ธก ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
|
13 |
def classify_image(img):
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
# ๋ชจ๋ธ๋ก ์์ธก์ ์ํํฉ๋๋ค.
|
18 |
-
predictions = model(**inputs)
|
19 |
-
|
20 |
-
# ์์ธก ๊ฒฐ๊ณผ ์ค์์ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ์ ํํฉ๋๋ค.
|
21 |
-
predicted_label = tf.argmax(predictions.logits[0], axis=-1).numpy()
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Gradio UI๋ฅผ ์์ฑํฉ๋๋ค.
|
27 |
iface = gr.Interface(fn=classify_image,
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
5 |
+
import tensorflow as tf
|
6 |
|
7 |
# Segformer ๋ชจ๋ธ ๋ฐ feature extractor ๋ถ๋ฌ์ค๊ธฐ
|
8 |
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
|
|
12 |
|
13 |
# ๋ชจ๋ธ ์์ธก ํจ์๋ฅผ ์ ์ํฉ๋๋ค.
|
14 |
def classify_image(img):
|
15 |
+
try:
|
16 |
+
# ์ด๋ฏธ์ง๋ฅผ ์ ์ฒ๋ฆฌํฉ๋๋ค.
|
17 |
+
inputs = feature_extractor(images=img, return_tensors="tf")
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# ๋ชจ๋ธ๋ก ์์ธก์ ์ํํฉ๋๋ค.
|
20 |
+
predictions = model(**inputs)
|
21 |
+
|
22 |
+
# ์์ธก ๊ฒฐ๊ณผ ์ค์์ ๊ฐ์ฅ ๋์ ํ๋ฅ ์ ๊ฐ์ง ํด๋์ค๋ฅผ ์ ํํฉ๋๋ค.
|
23 |
+
predicted_label = tf.argmax(predictions.logits[0], axis=-1).numpy()
|
24 |
+
|
25 |
+
# ๋ผ๋ฒจ์ ๋ฐํํฉ๋๋ค.
|
26 |
+
return predicted_label
|
27 |
+
except Exception as e:
|
28 |
+
# ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ์์ธ ๋ฉ์์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
29 |
+
return str(e)
|
30 |
|
31 |
# Gradio UI๋ฅผ ์์ฑํฉ๋๋ค.
|
32 |
iface = gr.Interface(fn=classify_image,
|