Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,35 @@ import gradio as gr
|
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
from io import BytesIO
|
6 |
import json
|
|
|
7 |
from ultralytics import YOLO
|
8 |
|
9 |
-
|
10 |
# ======================= МОДЕЛЬ ===================================
|
11 |
-
|
12 |
model = YOLO("yolov11m_best.pt")
|
13 |
|
14 |
# ================== ЧТЕНИЕ НАЗВАНИЙ И ЦЕН =======================
|
15 |
-
|
16 |
with open('Fruit_Veggies_Price.json', 'r', encoding='utf-8') as file:
|
17 |
fruits_data = json.load(file)
|
18 |
|
19 |
-
|
20 |
-
# таблица с данными
|
21 |
-
detections = []
|
22 |
-
|
23 |
-
|
24 |
# =========================== ДЕТЕКЦИЯ ПЛОДА ============================
|
25 |
-
|
26 |
-
def detect_image(image_path: str, weight: float):
|
27 |
# Считываем изображение
|
28 |
-
image
|
29 |
-
|
|
|
30 |
# Выполняем детекцию
|
31 |
-
detections = model.predict(source=
|
|
|
|
|
|
|
|
|
|
|
32 |
result_np_image = detections[0].plot()
|
33 |
result_np_image = cv2.cvtColor(result_np_image, cv2.COLOR_BGR2RGB)
|
34 |
|
35 |
detected_fruit = None
|
36 |
|
37 |
-
for det in detections:
|
38 |
label = model.names[int(det[5])] # название фрукта
|
39 |
if label in fruits_data:
|
40 |
detected_fruit = label
|
@@ -49,34 +47,32 @@ def detect_image(image_path: str, weight: float):
|
|
49 |
|
50 |
return result_np_image, fruit_name, weight, total_price
|
51 |
|
52 |
-
|
53 |
# =========================== ЧЕК ============================
|
54 |
-
|
55 |
-
# функция для добавления позиции в чек
|
56 |
def create_receipt(fruit_name, weight, total_price):
|
57 |
-
# белый фон для чека
|
58 |
receipt_img = Image.new("RGB", (300, 200), color="white")
|
59 |
draw = ImageDraw.Draw(receipt_img)
|
60 |
-
|
61 |
-
# шрифт
|
62 |
try:
|
63 |
font = ImageFont.truetype("arial.ttf", 18)
|
64 |
except IOError:
|
65 |
font = ImageFont.load_default()
|
66 |
-
|
67 |
-
# Записываем текст на чеке
|
68 |
draw.text((10, 10), "Чек", fill="black", font=font)
|
69 |
draw.text((10, 50), f"Продукт: {fruit_name}", fill="black", font=font)
|
70 |
draw.text((10, 80), f"Вес: {weight} кг", fill="black", font=font)
|
71 |
-
draw.text((10, 110), f"Цена за кг: {fruits_data[
|
72 |
draw.text((10, 140), f"Сумма: {total_price} руб.", fill="black", font=font)
|
73 |
-
|
74 |
-
return receipt_img
|
75 |
|
|
|
|
|
|
|
|
|
76 |
|
77 |
# ======================= ИНТЕРФЕЙС ============================
|
78 |
-
|
79 |
def gradio_interface(image, weight):
|
|
|
|
|
|
|
80 |
image, fruit_name, weight, total_price = detect_fruit(image, weight)
|
81 |
if not fruit_name:
|
82 |
return image, None # Вернуть пустой чек
|
|
|
4 |
from PIL import Image, ImageDraw, ImageFont
|
5 |
from io import BytesIO
|
6 |
import json
|
7 |
+
import cv2
|
8 |
from ultralytics import YOLO
|
9 |
|
|
|
10 |
# ======================= МОДЕЛЬ ===================================
|
|
|
11 |
model = YOLO("yolov11m_best.pt")
|
12 |
|
13 |
# ================== ЧТЕНИЕ НАЗВАНИЙ И ЦЕН =======================
|
|
|
14 |
with open('Fruit_Veggies_Price.json', 'r', encoding='utf-8') as file:
|
15 |
fruits_data = json.load(file)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
17 |
# =========================== ДЕТЕКЦИЯ ПЛОДА ============================
|
18 |
+
def detect_fruit(image, weight: float):
|
|
|
19 |
# Считываем изображение
|
20 |
+
# Предполагается, что image - это объект PIL Image
|
21 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
22 |
+
|
23 |
# Выполняем детекцию
|
24 |
+
detections = model.predict(source=image_cv, conf=0.5)
|
25 |
+
|
26 |
+
# Проверка на наличие детекций
|
27 |
+
if len(detections) == 0:
|
28 |
+
return image, "Фрукт не обнаружен", None, None
|
29 |
+
|
30 |
result_np_image = detections[0].plot()
|
31 |
result_np_image = cv2.cvtColor(result_np_image, cv2.COLOR_BGR2RGB)
|
32 |
|
33 |
detected_fruit = None
|
34 |
|
35 |
+
for det in detections.xyxy[0]: # Предполагается, что вы используете ультралайтики и доступ к детекциям
|
36 |
label = model.names[int(det[5])] # название фрукта
|
37 |
if label in fruits_data:
|
38 |
detected_fruit = label
|
|
|
47 |
|
48 |
return result_np_image, fruit_name, weight, total_price
|
49 |
|
|
|
50 |
# =========================== ЧЕК ============================
|
|
|
|
|
51 |
def create_receipt(fruit_name, weight, total_price):
|
|
|
52 |
receipt_img = Image.new("RGB", (300, 200), color="white")
|
53 |
draw = ImageDraw.Draw(receipt_img)
|
54 |
+
|
|
|
55 |
try:
|
56 |
font = ImageFont.truetype("arial.ttf", 18)
|
57 |
except IOError:
|
58 |
font = ImageFont.load_default()
|
59 |
+
|
|
|
60 |
draw.text((10, 10), "Чек", fill="black", font=font)
|
61 |
draw.text((10, 50), f"Продукт: {fruit_name}", fill="black", font=font)
|
62 |
draw.text((10, 80), f"Вес: {weight} кг", fill="black", font=font)
|
63 |
+
draw.text((10, 110), f"Цена за кг: {fruits_data[fruit_name]['price_per_kg']} руб.", fill="black", font=font)
|
64 |
draw.text((10, 140), f"Сумма: {total_price} руб.", fill="black", font=font)
|
|
|
|
|
65 |
|
66 |
+
with BytesIO() as output:
|
67 |
+
receipt_img.save(output, format="PNG")
|
68 |
+
output.seek(0)
|
69 |
+
return output.getvalue()
|
70 |
|
71 |
# ======================= ИНТЕРФЕЙС ============================
|
|
|
72 |
def gradio_interface(image, weight):
|
73 |
+
if weight <= 0:
|
74 |
+
return image, "Вес должен быть больше 0", None
|
75 |
+
|
76 |
image, fruit_name, weight, total_price = detect_fruit(image, weight)
|
77 |
if not fruit_name:
|
78 |
return image, None # Вернуть пустой чек
|