DDingcheol commited on
Commit
0f6c099
ยท
1 Parent(s): e3aa933

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -18
app.py CHANGED
@@ -1,26 +1,80 @@
1
- from transformers import AutoModelForSemanticSegmentation, AutoTokenizer
2
  from PIL import Image
3
- import requests
4
- from io import BytesIO
 
5
 
6
- # Hugging Face ๋ชจ๋ธ ์ด๋ฆ„
7
  model_name = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"
 
 
8
 
9
- # ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
10
- model = AutoModelForSemanticSegmentation.from_pretrained(model_name)
11
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
 
12
 
13
- # ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ
14
- image_url = "https://example.com/your_image.jpg" # ์ด๋ฏธ์ง€ URL์„ ์‹ค์ œ ์ด๋ฏธ์ง€ URL๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”.
15
- image = Image.open(BytesIO(requests.get(image_url).content))
16
 
17
- # ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
18
- inputs = tokenizer(image, return_tensors="pt")
19
 
20
- # ๋ชจ๋ธ ์‹คํ–‰
21
- outputs = model(**inputs)
22
- predictions = outputs.logits
23
 
24
- # ์—ฌ๊ธฐ์„œ predictions๋Š” segmentation ๋งต์ž…๋‹ˆ๋‹ค.
25
- # ์ด๋ฅผ ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
26
- # ์˜ˆ๋ฅผ ๋“ค์–ด, PIL Image๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  from PIL import Image
3
+ import numpy as np
4
+ import tensorflow as tf
5
+ from transformers import AutoFeatureExtractor, TFAutoModelForSemanticSegmentation
6
 
7
+ # Hugging Face ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ €
8
  model_name = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"
9
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
10
+ model = TFAutoModelForSemanticSegmentation.from_pretrained(model_name)
11
 
12
+ def label_to_color_image(label, colormap):
13
+ color_seg = np.zeros(
14
+ (label.shape[0], label.shape[1], 3), dtype=np.uint8
15
+ ) # height, width, 3
16
+ for i in range(len(colormap)):
17
+ color_seg[label.numpy() == i, :] = colormap[i]
18
+ return color_seg
19
 
20
+ def draw_plot(pred_img, seg, colormap, labels_list):
21
+ # your existing draw_plot function, unchanged
 
22
 
23
+ def huggingface_model(input_img):
24
+ input_img = Image.fromarray(input_img)
25
 
26
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
27
+ outputs = model(**inputs)
28
+ logits = outputs.logits
29
 
30
+ logits = tf.transpose(logits, [0, 2, 3, 1])
31
+ logits = tf.image.resize(
32
+ logits, input_img.size[::-1]
33
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
34
+ seg = tf.math.argmax(logits, axis=-1)[0]
35
+
36
+ # Define the colormap for the cityscapes dataset
37
+ colormap = [
38
+ [128, 64, 128],
39
+ [244, 35, 232],
40
+ [70, 70, 70],
41
+ [102, 102, 156],
42
+ [190, 153, 153],
43
+ [153, 153, 153],
44
+ [250, 170, 30],
45
+ [220, 220, 0],
46
+ [107, 142, 35],
47
+ [152, 251, 152],
48
+ [0, 130, 180],
49
+ [220, 20, 60],
50
+ [255, 0, 0],
51
+ [0, 0, 142],
52
+ [0, 0, 70],
53
+ [0, 60, 100],
54
+ [0, 80, 100],
55
+ [0, 0, 230],
56
+ [119, 11, 32],
57
+ ]
58
+
59
+ color_seg = label_to_color_image(seg, colormap)
60
+
61
+ # Show image + mask
62
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
63
+ pred_img = pred_img.astype(np.uint8)
64
+
65
+ # Draw plot
66
+ fig = draw_plot(pred_img, seg, colormap, labels_list)
67
+ return fig
68
+
69
+ # ์—ฌ๋Ÿฌ๋ถ„์ด ๊ฐ€์ง„ labels.txt ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ labels_list์— ํ• ๋‹นํ•˜์„ธ์š”.
70
+ labels_list = ["label1", "label2", ...]
71
+
72
+ demo = gr.Interface(
73
+ fn=huggingface_model,
74
+ inputs=gr.Image(shape=(1024, 1024)), # ์ž…๋ ฅ ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋Š” ๋ชจ๋ธ์˜ ์ž…๋ ฅ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์กฐ์ ˆํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
75
+ outputs=["plot"],
76
+ examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg"],
77
+ allow_flagging='never'
78
+ )
79
+
80
+ demo.launch()