Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ def detect_fruit(image, weight: float):
|
|
39 |
break
|
40 |
|
41 |
if not detected_fruit:
|
42 |
-
return result_np_image,
|
43 |
|
44 |
fruit_name = fruits_data[detected_fruit]["name"]
|
45 |
price_per_kg = fruits_data[detected_fruit]["price_per_kg"]
|
@@ -48,47 +48,50 @@ def detect_fruit(image, weight: float):
|
|
48 |
return result_np_image, fruit_name, weight, total_price
|
49 |
|
50 |
# =========================== ЧЕК ============================
|
51 |
-
def create_receipt(fruit_name, weight, total_price):
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
# ======================= ИНТЕРФЕЙС ============================
|
72 |
def gradio_interface(image, weight):
|
73 |
-
if weight <= 0:
|
74 |
-
|
75 |
|
76 |
-
|
77 |
if not fruit_name:
|
78 |
-
return image
|
79 |
-
|
80 |
-
|
81 |
-
return
|
|
|
|
|
82 |
|
83 |
image_input = gr.Image(label="Изображение")
|
84 |
weight_input = gr.Number(label="Вес (кг)")
|
85 |
image_output = gr.Image(label="Распознанный фрукт", type="numpy")
|
86 |
-
receipt_output = gr.Image(label="Чек", type="numpy")
|
87 |
|
88 |
gr.Interface(
|
89 |
fn=gradio_interface,
|
90 |
inputs=[image_input, weight_input],
|
91 |
-
outputs=[image_output
|
|
|
92 |
title="Определение плода и создание чека",
|
93 |
description="Загрузите изображение, введите вес и получите чек"
|
94 |
).launch()
|
|
|
39 |
break
|
40 |
|
41 |
if not detected_fruit:
|
42 |
+
return result_np_image, None, None, None
|
43 |
|
44 |
fruit_name = fruits_data[detected_fruit]["name"]
|
45 |
price_per_kg = fruits_data[detected_fruit]["price_per_kg"]
|
|
|
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
|
75 |
|
76 |
+
detected_image, fruit_name, weight, total_price = detect_fruit(image, weight)
|
77 |
if not fruit_name:
|
78 |
+
return image
|
79 |
+
# return image, None # Вернуть пустой чек
|
80 |
+
|
81 |
+
return detected_image
|
82 |
+
# receipt = create_receipt(fruit_name, weight, total_price)
|
83 |
+
# return image, receipt
|
84 |
|
85 |
image_input = gr.Image(label="Изображение")
|
86 |
weight_input = gr.Number(label="Вес (кг)")
|
87 |
image_output = gr.Image(label="Распознанный фрукт", type="numpy")
|
88 |
+
# receipt_output = gr.Image(label="Чек", type="numpy")
|
89 |
|
90 |
gr.Interface(
|
91 |
fn=gradio_interface,
|
92 |
inputs=[image_input, weight_input],
|
93 |
+
outputs=[image_output],
|
94 |
+
# outputs=[image_output, receipt_output],
|
95 |
title="Определение плода и создание чека",
|
96 |
description="Загрузите изображение, введите вес и получите чек"
|
97 |
).launch()
|