DDingcheol's picture
Update app.py
81a50c2
raw
history blame contribute delete
No virus
988 Bytes
from transformers import AutoModelForSemanticSegmentation, AutoTokenizer
from PIL import Image
import requests
from io import BytesIO
# Hugging Face ๋ชจ๋ธ ์ด๋ฆ„
model_name = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"
# ๋ชจ๋ธ ๋ฐ ํ† ํฌ๋‚˜์ด์ € ๋กœ๋“œ
model = AutoModelForSemanticSegmentation.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ ๋ฐ ์ „์ฒ˜๋ฆฌ
image_url = "https://example.com/your_image.jpg" # ์ด๋ฏธ์ง€ URL์„ ์‹ค์ œ ์ด๋ฏธ์ง€ URL๋กœ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”.
image = Image.open(BytesIO(requests.get(image_url).content))
# ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ ์ž…๋ ฅ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜
inputs = tokenizer(image, return_tensors="pt")
# ๋ชจ๋ธ ์‹คํ–‰
outputs = model(**inputs)
predictions = outputs.logits
# ์—ฌ๊ธฐ์„œ predictions๋Š” segmentation ๋งต์ž…๋‹ˆ๋‹ค.
# ์ด๋ฅผ ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹œ๊ฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
# ์˜ˆ๋ฅผ ๋“ค์–ด, PIL Image๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.