wickedreg commited on
Commit
bb22818
·
verified ·
1 Parent(s): 9bb2f86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -46
app.py CHANGED
@@ -30,78 +30,78 @@ def detect_fruit(image):
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
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"]
46
- # total_price = round(price_per_kg * weight, 2)
47
- return result_np_image
48
-
49
- # return result_np_image, fruit_name, weight, total_price
50
 
51
  # =========================== ЧЕК ============================
52
- # def create_receipt(fruit_name, weight, total_price):
53
- # receipt_img = Image.new("RGB", (300, 200), color="white")
54
- # draw = ImageDraw.Draw(receipt_img)
 
 
 
 
55
 
56
- # try:
57
- # font = ImageFont.truetype("arial.ttf", 18)
58
- # except IOError:
59
- # font = ImageFont.load_default()
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[fruit_name]['price_per_kg']} руб.", fill="black", font=font)
65
- # draw.text((10, 140), f"Сумма: {total_price} руб.", fill="black", font=font)
66
 
67
- # with BytesIO() as output:
68
- # receipt_img.save(output, format="PNG")
69
- # output.seek(0)
70
- # return output.getvalue()
71
 
72
  # ======================= ИНТЕРФЕЙС ============================
73
- def gradio_interface(image):
74
- # if weight <= 0:
75
- # return image
 
76
 
77
- detected_image = detect_fruit(image)
78
- # if not fruit_name:
79
- # return image
80
- # return image, None # Вернуть пустой чек
81
 
82
- return detected_image
83
- # receipt = create_receipt(fruit_name, weight, total_price)
84
- # return image, receipt
85
 
86
  image_input = gr.Image(
87
  label="Изображение",
88
  width=640,
89
  height=480
90
  )
91
- # weight_input = gr.Number(label="Вес (кг)")
92
  image_output = gr.Image(
93
  label="Распознанный фрукт",
94
  type="numpy",
95
  width=640,
96
  height=480
97
  )
98
- # receipt_output = gr.Image(label="Чек", type="numpy")
 
 
 
 
 
99
 
100
  gr.Interface(
101
  fn=gradio_interface,
102
- inputs=[image_input],
103
- outputs=[image_output],
104
- # outputs=[image_output, receipt_output],
105
  title="Определение плода и создание чека",
106
  description="Загрузите изображение, введите вес и получите чек"
107
  ).launch()
 
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
39
+ break
40
 
41
+ return result_np_image, detected_fruit
 
 
 
 
 
 
 
 
42
 
43
  # =========================== ЧЕК ============================
44
+ def create_receipt(detected_fruit, weight):
45
+ data = fruits_data[detected_fruit];
46
+ fruit_name = data['name']
47
+ price = data['price_per_kg']
48
+ total_price = round(price * weight, 2)
49
+ receipt_img = Image.new("RGB", (300, 200), color="white")
50
+ draw = ImageDraw.Draw(receipt_img)
51
 
52
+ try:
53
+ font = ImageFont.truetype("arial.ttf", 18)
54
+ except IOError:
55
+ font = ImageFont.load_default()
56
 
57
+ draw.text((10, 10), "Чек", fill="black", font=font)
58
+ draw.text((10, 50), f"Продукт: {fruit_name}", fill="black", font=font)
59
+ draw.text((10, 80), f"Вес: {weight} кг", fill="black", font=font)
60
+ draw.text((10, 110), f"Цена за кг: {price} руб.", fill="black", font=font)
61
+ draw.text((10, 140), f"Сумма: {total_price} руб.", fill="black", font=font)
62
 
63
+ with BytesIO() as output:
64
+ receipt_img.save(output, format="PNG")
65
+ output.seek(0)
66
+ return output.getvalue()
67
 
68
  # ======================= ИНТЕРФЕЙС ============================
69
+ def gradio_interface(image, weight):
70
+ if weight <= 0:
71
+ gr.Info('Укажите вес товара')
72
+ return None, None
73
 
74
+ result_np_image, detected_fruit = detect_fruit(image)
75
+ if not fruit_name:
76
+ gr.Info('Не удалось определить товар')
77
+ return result_np_image, None
78
 
79
+ receipt = create_receipt(detected_fruit, weight)
80
+ return result_np_image, receipt
 
81
 
82
  image_input = gr.Image(
83
  label="Изображение",
84
  width=640,
85
  height=480
86
  )
87
+ weight_input = gr.Number(label="Вес (кг)")
88
  image_output = gr.Image(
89
  label="Распознанный фрукт",
90
  type="numpy",
91
  width=640,
92
  height=480
93
  )
94
+ receipt_output = gr.Image(
95
+ label="Чек",
96
+ type="numpy",
97
+ width=640,
98
+ height=480
99
+ )
100
 
101
  gr.Interface(
102
  fn=gradio_interface,
103
+ inputs=[image_input, weight_input],
104
+ outputs=[image_output, receipt_output],
 
105
  title="Определение плода и создание чека",
106
  description="Загрузите изображение, введите вес и получите чек"
107
  ).launch()