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()