Spaces:
Runtime error
Runtime error
File size: 1,260 Bytes
a87c3be 0462cba 172362f 5b982d8 a87c3be 9378acd a87c3be 5b982d8 172362f 5b982d8 172362f 5b982d8 9378acd 5b982d8 172362f 5b982d8 a87c3be 5b982d8 a87c3be 5b982d8 |
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 |
import gradio as gr
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
from PIL import Image
import numpy as np
import torch
# 모델과 feature extractor 로드
model_name = "nvidia/segformer-b0-finetuned-ade-512-512"
model = SegformerForSemanticSegmentation.from_pretrained(model_name)
feature_extractor = SegformerFeatureExtractor.from_pretrained(model_name)
def segment_image(image):
# 이미지 처리
inputs = feature_extractor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
# 마스크 생성
upsampled_logits = torch.nn.functional.interpolate(
outputs.logits, size=image.size[::-1], mode="bilinear", align_corners=False
)
upsampled_predictions = upsampled_logits.argmax(dim=1)
mask = upsampled_predictions.squeeze().numpy()
# 결과 반환
return Image.fromarray(np.uint8(mask * 255))
# 예시 이미지 경로
example_images = ["image1.jpg", "image2.jpg", "image3.jpg"]
# Gradio 인터페이스 설정
demo = gr.Interface(
fn=segment_image,
inputs=gr.inputs.Image(type="pil"),
outputs="image",
title="머신러닝 7주차 과제_3",
examples=example_images
)
# 인터페이스 실행
demo.launch()
|