Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
from io import BytesIO
|
4 |
+
import pandas as pd
|
5 |
+
import json
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
# ======================= МОДЕЛЬ ===================================
|
10 |
+
|
11 |
+
model = YOLO("yolov11m_best.pt")
|
12 |
+
|
13 |
+
# ================== ЧТЕНИЕ НАЗВАНИЙ И ЦЕН =======================
|
14 |
+
|
15 |
+
with open('Fruit_Veggies_Price.json', 'r', encoding='utf-8') as file:
|
16 |
+
fruits_data = json.load(file)
|
17 |
+
|
18 |
+
|
19 |
+
# таблица с данными
|
20 |
+
detections = []
|
21 |
+
|
22 |
+
|
23 |
+
# =========================== ДЕТЕКЦИЯ ПЛОДА ============================
|
24 |
+
|
25 |
+
def detect_fruit(image, weight):
|
26 |
+
results = model.predict(source=image, conf=0.5)
|
27 |
+
detections = results[0].boxes.data.tolist()
|
28 |
+
detected_fruit = None
|
29 |
+
|
30 |
+
for det in detections:
|
31 |
+
label = model.names[int(det[5])] # название фрукта
|
32 |
+
if label in fruits_data:
|
33 |
+
detected_fruit = label
|
34 |
+
break
|
35 |
+
|
36 |
+
if not detected_fruit:
|
37 |
+
return image, "Фрукт не обнаружен", None, None
|
38 |
+
|
39 |
+
fruit_name = fruits_data[detected_fruit]["name"]
|
40 |
+
price_per_kg = fruits_data[detected_fruit]["price_per_kg"]
|
41 |
+
total_price = round(price_per_kg * weight, 2)
|
42 |
+
|
43 |
+
return image, fruit_name, weight, total_price
|
44 |
+
|
45 |
+
|
46 |
+
# =========================== ЧЕК ============================
|
47 |
+
|
48 |
+
# функция для добавления позиции в чек
|
49 |
+
def create_receipt(fruit_name, weight, total_price):
|
50 |
+
# белый фон для чека
|
51 |
+
receipt_img = Image.new("RGB", (300, 200), color="white")
|
52 |
+
draw = ImageDraw.Draw(receipt_img)
|
53 |
+
|
54 |
+
# шрифт
|
55 |
+
try:
|
56 |
+
font = ImageFont.truetype("arial.ttf", 18)
|
57 |
+
except IOError:
|
58 |
+
font = ImageFont.load_default()
|
59 |
+
|
60 |
+
# Записываем текст на чеке
|
61 |
+
draw.text((10, 10), "Чек", fill="black", font=font)
|
62 |
+
draw.text((10, 50), f"Продукт: {fruit_name}", fill="black", font=font)
|
63 |
+
draw.text((10, 80), f"Вес: {weight} кг", fill="black", font=font)
|
64 |
+
draw.text((10, 110), f"Цена за кг: {fruits_data[detected_fruit]['price_per_kg']} руб.", fill="black", font=font)
|
65 |
+
draw.text((10, 140), f"Сумма: {total_price} руб.", fill="black", font=font)
|
66 |
+
|
67 |
+
return receipt_img
|
68 |
+
|
69 |
+
|
70 |
+
# ======================= ИНТЕРФЕЙС ============================
|
71 |
+
|
72 |
+
def gradio_interface(image, weight):
|
73 |
+
image, fruit_name, weight, total_price = detect_fruit(image, weight)
|
74 |
+
if not fruit_name:
|
75 |
+
return image, None # Вернуть пустой чек
|
76 |
+
|
77 |
+
receipt = create_receipt(fruit_name, weight, total_price)
|
78 |
+
return image, receipt
|
79 |
+
|
80 |
+
image_input = gr.inputs.Image(label="Изображение с веб-камеры")
|
81 |
+
weight_input = gr.inputs.Number(label="Вес (кг)", default=1.0)
|
82 |
+
image_output = gr.outputs.Image(label="Распознанный фрукт")
|
83 |
+
receipt_output = gr.outputs.Image(label="Чек")
|
84 |
+
|
85 |
+
gr.Interface(
|
86 |
+
fn=gradio_interface,
|
87 |
+
inputs=[image_input, weight_input],
|
88 |
+
outputs=[image_output, receipt_output],
|
89 |
+
title="Определение фрукта и создание чека",
|
90 |
+
description="Загрузите изображение фрукта на весах, введите вес и получите чек"
|
91 |
+
).launch()
|