Axelottle commited on
Commit
ca2302e
1 Parent(s): ae29d06

Upload folder using huggingface_hub

Browse files
.DS_Store ADDED
Binary file (6.15 kB). View file
 
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/pandesal.jpg filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ .DS_Store
2
+ __pycache__/
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Bread Detector Gradio
3
- emoji: 👁
4
- colorFrom: pink
5
- colorTo: gray
6
- sdk: gradio
7
- sdk_version: 3.40.1
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Bread-Detector-Gradio
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 3.39.0
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2 as cv2
3
+ import pandas as pd
4
+ from sahi.prediction import ObjectPrediction
5
+ from sahi.utils.cv import visualize_object_predictions, read_image
6
+ from ultralyticsplus import YOLO
7
+
8
+ # Gradio Theme
9
+ theme = gr.themes.Soft(
10
+ primary_hue="yellow",
11
+ secondary_hue="blue",
12
+ neutral_hue="gray",
13
+ font=[gr.themes.GoogleFont('Inter'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
14
+ font_mono=[gr.themes.GoogleFont('Inter'), 'ui-monospace', 'Consolas', 'monospace'],
15
+ ).set(
16
+ background_fill_primary='*neutral_100',
17
+ )
18
+
19
+ # Bread Prices
20
+ bread_types = {
21
+ "baguette": {"name": "Baguette", "price": 108},
22
+ "binangkal": {"name": "Binangkal", "price": 11},
23
+ "bonete": {"name": "Bonete", "price": 8},
24
+ "cornbread": {"name": "Cornbread", "price": 55},
25
+ "croissant": {"name": "Croissant", "price": 75},
26
+ "ensaymada": {"name": "Ensaymada", "price": 14},
27
+ "flatbread": {"name": "Flatbread", "price": 17},
28
+ "kalihim": {"name": "Kalihim", "price": 15},
29
+ "monay": {"name": "Monay", "price": 6},
30
+ "pandesal": {"name": "Pandesal", "price": 3},
31
+ "sourdough": {"name": "Sourdough", "price": 150},
32
+ "spanish-bread": {"name": "Spanish Bread", "price": 14},
33
+ "wheat-bread": {"name": "Wheat Bread", "price": 8},
34
+ "white-bread": {"name": "White Bread", "price": 4},
35
+ "whole-grain-bread": {"name": "Whole Grain Bread", "price": 10},
36
+ }
37
+
38
+ # Instantiate the model
39
+ model = YOLO('best.pt')
40
+
41
+ # Bread Prediction function
42
+ def detect_bread(image):
43
+ results = model.predict(image, conf=0.4)
44
+ result = results[0]
45
+ object_prediction_list = []
46
+
47
+ # Image Output function
48
+ for box in result.boxes:
49
+ bbox = box.xyxy[0].tolist()
50
+ score = round(box.conf[0].item(), 2)
51
+ category_id = box.cls[0]
52
+ category_name = result.names[box.cls[0].item()]
53
+
54
+ object_prediction = ObjectPrediction(
55
+ bbox=bbox,
56
+ category_id=int(category_id),
57
+ score=score,
58
+ category_name=category_name,
59
+ )
60
+ object_prediction_list.append(object_prediction)
61
+
62
+ image = read_image(image)
63
+ output_image = visualize_object_predictions(image=image, object_prediction_list=object_prediction_list)
64
+
65
+ # Receipt Output function
66
+ detected_classes = []
67
+ detected_items = []
68
+ counts = {} # Dictionary to store bread type counts
69
+
70
+ for cls in result.boxes.cls: # Stores all detected classes in the list
71
+ detected_classes.append(result.names[int(cls)])
72
+
73
+ for item_class in detected_classes: # Counts the quantity of each class
74
+ counts[item_class] = counts.get(item_class, 0) + 1
75
+
76
+ for item_class, count in counts.items(): # Gets the name and price of each class
77
+ bread_info = bread_types.get(item_class, {})
78
+ item_name = bread_info.get("name", "Unknown Bread")
79
+ price = bread_info.get("price", 0)
80
+ detected_items.append({"item": item_name, "quantity": count, "price": price})
81
+
82
+ total_amount = sum(item["quantity"] * item["price"] for item in detected_items)
83
+
84
+ # Creates the receipt dictionary
85
+ data = {"Item": [], "Quantity": [], "Price": [], "Amount": []}
86
+
87
+ for item_info in detected_items:
88
+ item = item_info["item"]
89
+ quantity = item_info["quantity"]
90
+ price = item_info["price"]
91
+ total_item_amount = quantity * price
92
+
93
+ data["Item"].append(item)
94
+ data["Quantity"].append(quantity)
95
+ data["Price"].append(price)
96
+ data["Amount"].append(total_item_amount)
97
+
98
+ # Appends the last row of the dataframe for the total amount
99
+ data["Item"].append("TOTAL")
100
+ data["Quantity"].append("")
101
+ data["Price"].append("")
102
+ data["Amount"].append(total_amount)
103
+
104
+ df = pd.DataFrame(data)
105
+
106
+ return output_image['image'], df
107
+
108
+ # Export to CSV function
109
+ def export_csv(df):
110
+ df.to_csv("receipt.csv", index=False)
111
+ return gr.File.update(value="receipt.csv", visible=True)
112
+
113
+ # Export to JSON function
114
+ def export_json(df):
115
+ df.to_json("receipt.json")
116
+ return gr.File.update(value="receipt.json", visible=True)
117
+
118
+ # Gradio Interface
119
+ with gr.Blocks(theme=theme) as demo:
120
+
121
+ gr.Markdown("# Bread Detector w/ POS")
122
+ gr.Markdown("An application that detects different types of bread and calculates the total price.")
123
+ gr.Markdown("**Bread types include:** baguette, binangkal, bonete, cornbread, croissant, ensaymada, flatbread, kalihim, monay, pandesal, sourdough, spanish bread, wheat bread, white bread, and whole grain bread.")
124
+
125
+ with gr.Row():
126
+ with gr.Column():
127
+ fn = detect_bread
128
+ img_input = gr.Image(type="filepath", label="Input Image")
129
+ #img_input = gr.Files(file_types=["filepath"], label="Input Image")
130
+ detect_btn = gr.Button(variant="primary", value="Detect")
131
+
132
+ with gr.Column():
133
+ img_output = gr.Image(type="filepath", label='Output Image')
134
+ receipt_output = gr.Dataframe(
135
+ headers=["Item", "Quantity", "Price", "Amount"],
136
+ datatype=["str", "number", "number", "number"],
137
+ label="Receipt",
138
+ interactive=False,
139
+ )
140
+ with gr.Row():
141
+ clear_btn = gr.ClearButton([img_input, img_output, receipt_output])
142
+ export_btn = gr.Button(variant="primary", value="Export as CSV")
143
+ export_json_btn = gr.Button(variant="primary", value="Export as JSON")
144
+ with gr.Row():
145
+ csv = gr.File(interactive=False, visible=False)
146
+
147
+ with gr.Row():
148
+ gr.Examples(
149
+ examples = ["examples/bonete.jpg", "examples/pandesal.jpg", "examples/croissant_baguette.jpg", "examples/slices.png"],
150
+ inputs = img_input,
151
+ )
152
+
153
+ detect_btn.click(detect_bread, inputs=img_input, outputs=[img_output, receipt_output])
154
+ export_btn.click(export_csv, receipt_output, csv)
155
+ export_json_btn.click(export_json, receipt_output, csv)
156
+
157
+ demo.queue()
158
+ demo.launch()
examples/.DS_Store ADDED
Binary file (6.15 kB). View file
 
examples/bonete.jpg ADDED
examples/croissant_baguette.jpg ADDED
examples/pandesal.jpg ADDED

Git LFS Details

  • SHA256: 72b0244a8d6951e73092028aefa6cdc55e4bd49aa4bcc4af7ff504372128f262
  • Pointer size: 132 Bytes
  • Size of remote file: 1.32 MB
examples/slices.png ADDED
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio==3.40.1
2
+ opencv_python==4.8.0.74
3
+ pandas==2.0.3
4
+ sahi==0.11.14
5
+ ultralyticsplus==0.0.28
weights/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3ec84ed70c3f5b739b1aa271b767d6225fb85c0922aa8fb38b01023de24884fc
3
+ size 22519224