dakkoong commited on
Commit
89fa28d
ยท
1 Parent(s): be60b03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -100
app.py CHANGED
@@ -1,110 +1,32 @@
1
  import gradio as gr
2
- from matplotlib import gridspec
3
- import matplotlib.pyplot as plt
4
  import numpy as np
5
  from PIL import Image
6
- import tensorflow as tf
7
- from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
8
 
 
 
9
 
10
- feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
- "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
12
- )
13
- model = TFSegformerForSemanticSegmentation.from_pretrained(
14
- "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
15
- )
16
 
17
- def ade_palette():
18
- """ADE20K palette that maps each class to RGB values."""
19
- return [
20
- [204, 166, 62],
21
- [188, 229, 92],
22
- [47, 157,39],
23
- [178, 235, 244],
24
- [0, 51, 153],
25
- [181, 178, 255],
26
- [128, 65, 217],
27
- [255, 178, 245],
28
- [153, 0, 76],
29
- [25, 186, 52],
30
- [81, 162, 235],
31
- [255, 255, 0],
32
- [62, 57, 159],
33
- [91, 189, 203],
34
- [0, 0, 255],
35
- [0, 255, 255],
36
- [12, 168, 0],
37
- [255, 0, 0],
38
- [231, 32, 65]
39
- ]
40
 
41
- labels_list = []
 
 
 
 
42
 
43
- with open(r'labels.txt', 'r') as fp:
44
- for line in fp:
45
- labels_list.append(line[:-1])
46
 
47
- colormap = np.asarray(ade_palette())
 
48
 
49
- def label_to_color_image(label):
50
- if label.ndim != 2:
51
- raise ValueError("Expect 2-D input label")
52
-
53
- if np.max(label) >= len(colormap):
54
- raise ValueError("label value too large.")
55
- return colormap[label]
56
-
57
- def draw_plot(pred_img, seg):
58
- fig = plt.figure(figsize=(20, 15))
59
-
60
- grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
61
-
62
- plt.subplot(grid_spec[0])
63
- plt.imshow(pred_img)
64
- plt.axis('off')
65
- LABEL_NAMES = np.asarray(labels_list)
66
- FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
67
- FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
68
-
69
- unique_labels = np.unique(seg.numpy().astype("uint8"))
70
- ax = plt.subplot(grid_spec[1])
71
- plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
72
- ax.yaxis.tick_right()
73
- plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
74
- plt.xticks([], [])
75
- ax.tick_params(width=0.0, labelsize=25)
76
- return fig
77
-
78
- def sepia(input_img):
79
- input_img = Image.fromarray(input_img)
80
-
81
- inputs = feature_extractor(images=input_img, return_tensors="tf")
82
- outputs = model(**inputs)
83
- logits = outputs.logits
84
-
85
- logits = tf.transpose(logits, [0, 2, 3, 1])
86
- logits = tf.image.resize(
87
- logits, input_img.size[::-1]
88
- )
89
- seg = tf.math.argmax(logits, axis=-1)[0]
90
-
91
- color_seg = np.zeros(
92
- (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
93
- )
94
- for label, color in enumerate(colormap):
95
- color_seg[seg.numpy() == label, :] = color
96
-
97
- pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
98
- pred_img = pred_img.astype(np.uint8)
99
-
100
- fig = draw_plot(pred_img, seg)
101
- return fig
102
-
103
- demo = gr.Interface(fn=sepia,
104
- inputs=gr.Image(shape=(800, 600)),
105
- outputs=['plot'],
106
- examples=["cityoutdoor-1.jpg", "cityoutdoor-2.jpg", "cityoutdoor-3.jpg"],
107
- allow_flagging='never')
108
-
109
-
110
- demo.launch()
 
1
  import gradio as gr
 
 
2
  import numpy as np
3
  from PIL import Image
4
+ from tensorflow.keras.models import load_model
 
5
 
6
+ # ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
7
+ model = load_model('nvidia/segformer-b1-finetuned-cityscapes-1024-1024') # ๋ชจ๋ธ ๊ฒฝ๋กœ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์ˆ˜์ •ํ•˜์„ธ์š”.
8
 
9
+ # ๋ชจ๋ธ ์ž…๋ ฅ ํฌ๊ธฐ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
10
+ input_size = model.input_shape[1:3]
 
 
 
 
11
 
12
+ # ๋ชจ๋ธ ์˜ˆ์ธก ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
13
+ def classify_image(img):
14
+ # ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
15
+ img = img.resize(input_size)
16
+ img_array = np.array(img) / 255.0 # ์ด๋ฏธ์ง€๋ฅผ 0์—์„œ 1 ์‚ฌ์ด๋กœ ์ •๊ทœํ™”ํ•ฉ๋‹ˆ๋‹ค.
17
+ img_array = np.expand_dims(img_array, axis=0) # ๋ฐฐ์น˜ ์ฐจ์›์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # ๋ชจ๋ธ๋กœ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
20
+ predictions = model.predict(img_array)
21
+
22
+ # ์˜ˆ์ธก ๊ฒฐ๊ณผ ์ค‘์—์„œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
23
+ predicted_label = np.argmax(predictions)
24
 
25
+ # ๋ผ๋ฒจ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
26
+ return predicted_label
 
27
 
28
+ # Gradio UI๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
29
+ iface = gr.Interface(fn=classify_image, inputs="image", outputs="label", live=True)
30
 
31
+ # Gradio UI๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
32
+ iface.launch()