File size: 2,023 Bytes
88e5715
 
 
 
564b069
bccbfcc
79c110e
88e5715
 
808bedf
88e5715
 
 
 
808bedf
88e5715
 
 
 
808bedf
88e5715
 
 
 
 
 
808bedf
 
88e5715
433bf2b
 
 
 
 
 
 
 
 
706f9eb
808bedf
6a0870f
88e5715
 
 
 
808bedf
88e5715
 
8a40f69
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
41
42
43
44
45
46
47
48
49
50
import requests
import torch
from PIL import Image
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
from IPython.display import display
import torchvision.transforms as T
import gradio as gr

def greet(url):
    # load Mask2Former fine-tuned on Cityscapes semantic segmentation
    processor = AutoImageProcessor.from_pretrained("facebook/mask2former-swin-large-cityscapes-semantic")
    model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-large-cityscapes-semantic")

    image = Image.open(requests.get(url, stream=True).raw)

    inputs = processor(images=image, return_tensors="pt")

    with torch.no_grad():
        outputs = model(**inputs)

    # model predicts class_queries_logits of shape `(batch_size, num_queries)`
    # and masks_queries_logits of shape `(batch_size, num_queries, height, width)`
    class_queries_logits = outputs.class_queries_logits
    masks_queries_logits = outputs.masks_queries_logits

    # you can pass them to processor for postprocessing
    predicted_semantic_map = processor.post_process_semantic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
    # we refer to the demo notebooks for visualization (see "Resources" section in the Mask2Former docs)

    # predicted_semantic_map์„ 8๋น„ํŠธ ๋ถ€ํ˜ธ ์—†๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜
    # ์ด๋ฏธ์ง€๋ฅผ ๋ถ€ํ˜ธ ์—†๋Š” 8๋น„ํŠธ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ (0์—์„œ 255 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์Šค์ผ€์ผ๋ง)
    predicted_semantic_map_scaled = (predicted_semantic_map - predicted_semantic_map.min()) / (predicted_semantic_map.max() - predicted_semantic_map.min()) * 255
    predicted_semantic_map_uint8 = predicted_semantic_map_scaled.to(torch.uint8)

    tensor_to_pil = T.ToPILImage()
    image = tensor_to_pil(predicted_semantic_map_uint8)

    return image

url = "http://www.apparelnews.co.kr/upfiles/manage/202302/5d5f694177b26fc86e5db623bf7ae4b7.jpg"
#greet(url)

iface = gr.Interface(
    fn=greet,
    inputs=gr.Image(value=url),
    live=True
)

iface.launch(debug = True)