Spaces:
Sleeping
Sleeping
| import os | |
| from transformers import pipeline | |
| from PIL import Image | |
| import gradio as gr | |
| # ๐น ๊ฐ์ฒด ํ์ง ๋ชจ๋ธ ๋ก๋ฉ | |
| detector = pipeline( | |
| task="zero-shot-object-detection", | |
| model="google/owlv2-base-patch16-ensemble" | |
| ) | |
| # ๐น ํ์งํ ์ฌ๋ฃ ๋ผ๋ฒจ ๋ฆฌ์คํธ | |
| candidate_labels = [ | |
| "salmon", "chicken breast", "broccoli", "lettuce", "mushroom", | |
| "bell pepper", "onion", "cherry tomato", "egg", "milk", "cheese", "garlic" | |
| ] | |
| # ๐น ์์ด โ ํ๊ธ ๋ผ๋ฒจ ๋ณํ ๋์ ๋๋ฆฌ | |
| label_ko = { | |
| "salmon": "์ฐ์ด", | |
| "chicken breast": "๋ญ๊ฐ์ด์ด", | |
| "broccoli": "๋ธ๋ก์ฝ๋ฆฌ", | |
| "lettuce": "์์ถ", | |
| "mushroom": "๋ฒ์ฏ", | |
| "bell pepper": "ํผ๋ง", | |
| "onion": "์ํ", | |
| "cherry tomato": "๋ฐฉ์ธํ ๋งํ ", | |
| "egg": "๊ณ๋", | |
| "milk": "์ฐ์ ", | |
| "cheese": "์น์ฆ", | |
| "garlic": "๋ง๋" | |
| } | |
| # ๐น ์ฌ๋ฃ ํ์ง ํจ์ | |
| def detect_ingredients(image): | |
| if image is None: | |
| return "์ด๋ฏธ์ง๋ฅผ ๋จผ์ ์ ๋ก๋ํด์ฃผ์ธ์." | |
| outputs = detector(image, candidate_labels=candidate_labels, threshold=0.2) | |
| detected_labels = list(set([o["label"] for o in outputs])) | |
| if not detected_labels: | |
| return "์ฌ๋ฃ๋ฅผ ์ธ์ํ์ง ๋ชปํ์ต๋๋ค. ๋ ๋ช ํํ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์." | |
| # ์์ด โ ํ๊ธ ๋ณํ | |
| translated_labels = [label_ko.get(label, label) for label in detected_labels] | |
| return ", ".join(translated_labels) | |
| # ๐น Gradio UI ๊ตฌ์ฑ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## ๐ง ๋์ฅ๊ณ ์ฌ๋ฃ ํ์ง ์์คํ ") | |
| image_input = gr.Image(type="pil", label="๋์ฅ๊ณ ์ด๋ฏธ์ง ์ ๋ก๋") | |
| detect_button = gr.Button("์ฌ๋ฃ ํ์งํ๊ธฐ") | |
| ingredient_output = gr.Textbox(label="๐ ํ์ง๋ ์ฌ๋ฃ (ํ๊ธ)") | |
| detect_button.click(fn=detect_ingredients, inputs=image_input, outputs=ingredient_output) | |
| if __name__ == "__main__": | |
| demo.launch() | |