Spaces:
Sleeping
Sleeping
Commit
•
f9f9a16
1
Parent(s):
85b2157
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from ultralytics import YOLO
|
4 |
+
from datetime import datetime
|
5 |
+
|
6 |
+
|
7 |
+
categories = {
|
8 |
+
0: "Beans",
|
9 |
+
1: "Butter",
|
10 |
+
2: "Cheese",
|
11 |
+
3: "Drink",
|
12 |
+
4: "Jogurt",
|
13 |
+
5: "Milk",
|
14 |
+
6: "Pasta",
|
15 |
+
7: "Rice",
|
16 |
+
8: "Snacks",
|
17 |
+
9: "Tuna"
|
18 |
+
}
|
19 |
+
|
20 |
+
# inventory_separate_entries = pd.DataFrame(columns=["Product", "Time added"])
|
21 |
+
inventory_separate_entries = pd.read_excel("file.xlsx", sheet_name='History of entries')
|
22 |
+
print(inventory_separate_entries)
|
23 |
+
|
24 |
+
|
25 |
+
model = YOLO("best.pt")
|
26 |
+
|
27 |
+
|
28 |
+
def get_value_counts_df(inventory_separate_entries):
|
29 |
+
value_counts = inventory_separate_entries['Product'].value_counts(dropna=True, sort=True)
|
30 |
+
df_val_counts = pd.DataFrame(value_counts)
|
31 |
+
df_value_counts_reset = df_val_counts.reset_index()
|
32 |
+
df_value_counts_reset.columns = ['Product', 'Quantity']
|
33 |
+
return df_value_counts_reset
|
34 |
+
|
35 |
+
|
36 |
+
def add_item(item_name):
|
37 |
+
inventory_separate_entries.loc[-1] = [item_name, datetime.today().strftime('%Y-%m-%d %H:%M:%S')]
|
38 |
+
inventory_separate_entries.index = inventory_separate_entries.index + 1 # shifting index
|
39 |
+
inventory_separate_entries.sort_index(inplace=True)
|
40 |
+
|
41 |
+
|
42 |
+
def detect_objects(input_img):
|
43 |
+
result_img = model([input_img])
|
44 |
+
print("result", result_img[0].boxes)
|
45 |
+
detected_categories = []
|
46 |
+
for r in result_img[0].boxes:
|
47 |
+
for element in r.data:
|
48 |
+
detected_category = int(element[-1])
|
49 |
+
confidence = element[-2]
|
50 |
+
print(r.data)
|
51 |
+
detected_categories.append((detected_category, confidence))
|
52 |
+
print(detected_categories)
|
53 |
+
sorted_detected_categories = sorted(detected_categories, key=lambda x: x[1], reverse=True)
|
54 |
+
if len(sorted_detected_categories) > 0:
|
55 |
+
detected_category = categories[sorted_detected_categories[0][0]]
|
56 |
+
else:
|
57 |
+
detected_category = "Nothing detected"
|
58 |
+
|
59 |
+
label = gr.Label(detected_category, visible=True)
|
60 |
+
|
61 |
+
result_img = result_img[0].plot()
|
62 |
+
return gr.Image(result_img, visible=True), label#, add_item_btn # detection_interface
|
63 |
+
|
64 |
+
|
65 |
+
def refresh_inventory():
|
66 |
+
value_counts_df = get_value_counts_df(inventory_separate_entries)
|
67 |
+
|
68 |
+
with pd.ExcelWriter("file.xlsx") as writer:
|
69 |
+
value_counts_df.to_excel(writer, sheet_name='Total inventory', index=False)
|
70 |
+
inventory_separate_entries.to_excel(writer, sheet_name='History of entries', index=False)
|
71 |
+
|
72 |
+
return value_counts_df, inventory_separate_entries
|
73 |
+
|
74 |
+
|
75 |
+
with gr.Blocks() as inventory_interface:
|
76 |
+
with gr.Row():
|
77 |
+
refresh_btn = gr.Button("Refresh Inventory")
|
78 |
+
export_btn = gr.DownloadButton("Export Inventory", value="/file=file.xlsx")
|
79 |
+
|
80 |
+
value_counts_df = get_value_counts_df(inventory_separate_entries)
|
81 |
+
inventory_title = gr.Label("Inventory Summary", color="grey")
|
82 |
+
inventory_table = gr.DataFrame(value_counts_df)
|
83 |
+
|
84 |
+
history_title = gr.Label("History of added items", color="grey")
|
85 |
+
history_table = gr.DataFrame(inventory_separate_entries)
|
86 |
+
|
87 |
+
output_file = gr.File(visible=False)
|
88 |
+
refresh_btn.click(refresh_inventory, outputs=[inventory_table, history_table])
|
89 |
+
|
90 |
+
|
91 |
+
with gr.Blocks() as scan_interface:
|
92 |
+
with gr.Row(equal_height=False):
|
93 |
+
input = gr.Image()
|
94 |
+
greet_btn = gr.Button("Detect items")
|
95 |
+
|
96 |
+
with gr.Row() as row:
|
97 |
+
output = gr.Image()
|
98 |
+
with gr.Column():
|
99 |
+
label = gr.Label("Nothing detected", visible=True)
|
100 |
+
add_item_btn = gr.Button("Add item", visible=True)
|
101 |
+
|
102 |
+
add_item_btn.click(add_item, inputs=label)
|
103 |
+
greet_btn.click(detect_objects, inputs=input, outputs=[output, label])
|
104 |
+
|
105 |
+
|
106 |
+
demo = gr.TabbedInterface([scan_interface, inventory_interface], ["Scan items", "Inventory"])
|
107 |
+
|
108 |
+
demo.launch(share=True, allowed_paths=["file.xlsx"])
|