dakkoong commited on
Commit
e2067ba
ยท
1 Parent(s): 31405c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
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
- inputs = feature_extractor(images=img, return_tensors="tf")
16
-
17
- # ๋ชจ๋ธ๋กœ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
18
- predictions = model(**inputs)
19
-
20
- # ์˜ˆ์ธก ๊ฒฐ๊ณผ ์ค‘์—์„œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
21
- predicted_label = tf.argmax(predictions.logits[0], axis=-1).numpy()
22
 
23
- # ๋ผ๋ฒจ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
24
- return predicted_label
 
 
 
 
 
 
 
 
 
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,