Spaces:
Sleeping
Sleeping
| from transformers import pipeline | |
| from PIL import Image, ImageDraw | |
| import os | |
| # ๐ธ ๋์ฅ๊ณ ์ด๋ฏธ์ง ํ์ผ ์ด๋ฆ | |
| image_path = "image.jpg" | |
| # ๐ธ ํ์งํ ์ฌ๋ฃ ๋ผ๋ฒจ (์๋ฌธ) | |
| candidate_labels = [ | |
| "salmon", "chicken breast", "broccoli", "lettuce", "mushroom", | |
| "bell pepper", "onion", "cherry tomato" | |
| ] | |
| # ๐น ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ | |
| if not os.path.exists(image_path): | |
| raise FileNotFoundError(f"{image_path} ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค.") | |
| image = Image.open(image_path).convert("RGB") | |
| # ๐น zero-shot ๊ฐ์ฒด ํ์ง ํ์ดํ๋ผ์ธ ๋ง๋ค๊ธฐ | |
| detector = pipeline( | |
| task="zero-shot-object-detection", | |
| model="google/owlv2-base-patch16-ensemble" | |
| ) | |
| # ๐น ํ์ง ์คํ | |
| outputs = detector(image, candidate_labels=candidate_labels, threshold=0.2) | |
| # ๐น ๊ฒฐ๊ณผ ์ถ๋ ฅ | |
| print("\n[ํ์ง ๊ฒฐ๊ณผ]") | |
| for pred in outputs: | |
| label = pred["label"] | |
| score = pred["score"] | |
| box = pred["box"] | |
| print(f"{label} ({score:.2%}) ์์น: {box}") | |
| # ๐น ๊ฒฐ๊ณผ ์ด๋ฏธ์ง์ ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ | |
| draw = ImageDraw.Draw(image) | |
| for pred in outputs: | |
| box = pred["box"] | |
| label = pred["label"] | |
| score = pred["score"] | |
| draw.rectangle( | |
| [box["xmin"], box["ymin"], box["xmax"], box["ymax"]], | |
| outline="red", width=2 | |
| ) | |
| draw.text((box["xmin"], box["ymin"]), f"{label} {score:.2%}", fill="red") | |
| # ๐น ์๊ฐํ | |
| image.show() | |