Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -47,76 +47,85 @@ def preprocess_image(image_path):
|
|
47 |
|
48 |
def extract_expiry_dates(text):
|
49 |
expiry_date_patterns = [
|
50 |
-
r'USE BY (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
51 |
-
r'BEST BEFORE (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
52 |
-
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})',
|
53 |
-
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{
|
54 |
-
r'(\d{1,2}
|
55 |
-
r'(\d{1,2}
|
56 |
-
r'(\d{
|
57 |
-
r'(\d{
|
58 |
-
r'(\d{1,2}[
|
59 |
-
r'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Expiry Date: 20/07/2O24
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
]
|
121 |
|
122 |
current_date = datetime.now()
|
@@ -137,7 +146,10 @@ def extract_expiry_dates(text):
|
|
137 |
continue
|
138 |
|
139 |
days_left = (expiry_date - current_date).days
|
140 |
-
|
|
|
|
|
|
|
141 |
break # Stop after finding the first valid date
|
142 |
|
143 |
return dates_info
|
@@ -146,7 +158,7 @@ def extract_expiry_dates(text):
|
|
146 |
st.title("Image Processing Application")
|
147 |
|
148 |
# User choice for processing
|
149 |
-
task_choice = st.radio("Choose a task", ("Text and Brand Detection", "Freshness Detection"))
|
150 |
|
151 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
152 |
|
@@ -177,7 +189,7 @@ if uploaded_file is not None:
|
|
177 |
if expiry_dates_info:
|
178 |
st.write("Expiry Dates Found:")
|
179 |
for date_str, days_left in expiry_dates_info:
|
180 |
-
st.text(f"{date_str} - {days_left}
|
181 |
else:
|
182 |
st.write("No expiry dates found.")
|
183 |
|
@@ -217,4 +229,36 @@ if uploaded_file is not None:
|
|
217 |
img_array = preprocess_image(image_path)
|
218 |
predictions = fruit_model.predict(img_array)
|
219 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
220 |
-
st.write("Predicted Freshness:", class_names[predicted_class])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
def extract_expiry_dates(text):
|
49 |
expiry_date_patterns = [
|
50 |
+
r'USE BY (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Use by date
|
51 |
+
r'BEST BEFORE (\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Best before date
|
52 |
+
r'EXPIRY DATE[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Expiry Date: 20/07/2024
|
53 |
+
r'EXPIRY[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Expiry: 20/07/2024
|
54 |
+
r'EXP[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Exp: 20/07/2024
|
55 |
+
r'VALID UNTIL[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Valid Until: 20/07/2024
|
56 |
+
r'CONSUME BY[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Consume By: 20/07/2024
|
57 |
+
r'USE BY[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Use By: 20/07/2024
|
58 |
+
r'BEST BEFORE[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Best Before: 20/07/2024
|
59 |
+
r'EXPIRES ON[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Expires On: 20/07/2024
|
60 |
+
r'EXPIRES[:\-]?\s*(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # Expires: 20/07/2024
|
61 |
+
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # General date format
|
62 |
+
r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2})', # Short year date format
|
63 |
+
r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{4})', # Date with month name and full year
|
64 |
+
r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{2})', # Date with month name and short year
|
65 |
+
r'(\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2})', # Year-month-day format
|
66 |
+
r'(\d{4}[A-Za-z]{3,}\d{1,2})', # Year with month name
|
67 |
+
r'(\d{1,2}[A-Za-z]{3,}\d{4})', # Day with month name and full year
|
68 |
+
r'Best before (\d+) months', # Best before in months
|
69 |
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Expiry Date: 20/07/2O24
|
70 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-]\d{4}))', # Expiry Date: 20/07/2024
|
71 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Expiry Date: 20/07/2O24
|
72 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Expiry Date: 20 MAY 2O24
|
73 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*\d{4}))', # Expiry Date: 20 MAY 2024
|
74 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Expiry Date: 20 MAY 2O24
|
75 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Expiry Date: 2024/07/2O24
|
76 |
+
r'(?:exp(?:iry)?\.?\s*date\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-]\d{2}))', # Expiry Date: 2024/07/20
|
77 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{4}))', # Best Before: 2025
|
78 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Best Before: 20/07/2O24
|
79 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-]\d{4}))', # Best Before: 20/07/2024
|
80 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Best Before: 20/07/2O24
|
81 |
+
r'(?:best\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Best Before: 20 MAY 2O24
|
82 |
+
r'(?:best\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*\d{4}))', # Best Before: 20 MAY 2024
|
83 |
+
r'(?:best\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Best Before: 20 MAY 2O24
|
84 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Best Before: 2024/07/2O24
|
85 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-]\d{2}))', # Best Before: 2024/07/20
|
86 |
+
r'(?:best\s*before\s*[:\-]?\s*.*?(\d{1,2}\d{2}\d{2}))', # Best Before: 20250720
|
87 |
+
r'(?:best\s*before\s*[:\-]?\s*(\d{6}))', # Best Before: 202507
|
88 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{1,2}[A-Za-z]{3,}[0O]\d{2}))', # Consume Before: 3ODEC2O24
|
89 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{1,2}[A-Za-z]{3,}\d{2}))', # Consume Before: 30DEC23
|
90 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Consume Before: 20/07/2O24
|
91 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-]\d{4}))', # Consume Before: 20/07/2024
|
92 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Consume Before: 20/07/2O24
|
93 |
+
r'(?:consume\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Consume Before: 20 MAY 2O24
|
94 |
+
r'(?:consume\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*\d{4}))', # Consume Before: 20 MAY 2024
|
95 |
+
r'(?:consume\s*before\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Consume Before: 20 MAY 2O24
|
96 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Consume Before: 2024/07/2O24
|
97 |
+
r'(?:consume\s*before\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-]\d{2}))', # Consume Before: 2024/07/20
|
98 |
+
r'(?:exp\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Exp: 20/07/2O24
|
99 |
+
r'(?:exp\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-]\d{4}))', # Exp: 20/07/2024
|
100 |
+
r'(?:exp\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Exp: 20/07/2O24
|
101 |
+
r'(?:exp\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Exp: 20 MAY 2O24
|
102 |
+
r'(?:exp\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*\d{4}))', # Exp: 20 MAY 2024
|
103 |
+
r'(?:exp\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Exp: 20 MAY 2O24
|
104 |
+
r'(?:exp\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Exp: 2024/07/2O24
|
105 |
+
r'(?:exp\s*[:\-]?\s*.*?(\d{4}[\/\-]\d{2}[\/\-]\d{2}))', # Exp: 2024/07/20
|
106 |
+
r"Exp\.Date\s+(\d{2}[A-Z]{3}\d{4})", # Expiration date format
|
107 |
+
r'(?:exp\s*\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Exp. Date: 16 MAR 2O30 (with typo)
|
108 |
+
r'(?:exp\s*\.?\s*date\s*[:\-]?\s*.*?(\d{2}[\/\-]\d{2}[\/\-][0O]\d{2}))', # Exp. Date: 15/12/2O30 (with typo)
|
109 |
+
r'(?:exp\s*\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Exp. Date: 15 MAR 2O30 (with typo)
|
110 |
+
r'(?:exp\s*\.?\s*date\s*[:\-]?\s*.?(\d{2}\s[A-Za-z]{3,}\s*[0O]\d{2}))', # Exp. Date cdsyubfuyef 15 MAR 2O30 (with typo)
|
111 |
+
r'(\d{2}[\/\-]\d{2}[\/\-]\d{4})', # 20/07/2024
|
112 |
+
r'(\d{2}[\/\-]\d{2}[\/\-]\d{2})', # 20/07/24
|
113 |
+
r'(\d{2}\s*[A-Za-z]{3,}\s*\d{4})', # 20 MAY 2024
|
114 |
+
r'(\d{2}\s*[A-Za-z]{3,}\s*\d{2})', # 20 MAY 24
|
115 |
+
r'(\d{4}[\/\-]\d{2}[\/\-]\d{2})', # 2024/07/20
|
116 |
+
r'(\d{4}[\/\-]\d{2}[\/\-]\d{2})', # 2024-07-20
|
117 |
+
r'(\d{4}[A-Za-z]{3,}\d{2})', # 2024MAY20
|
118 |
+
r'(\d{2}[A-Za-z]{3,}\d{4})', # 20MAY2024
|
119 |
+
r'(?:DX3\s*[:\-]?\s*(\d{2}\s*\d{2}\s*\d{4}))', # DX3 date format
|
120 |
+
r'(?:exp\.?\s*date\s*[:\-]?\s*(\d{2}\s*[A-Za-z]{3,}\s*(\d{4}|\d{2})))', # Exp. Date format
|
121 |
+
r'(?:exp\.?\s*date\s*[:\-]?\s*(\d{2}\s*\d{2}\s*\d{4}))', # Exp. Date: 20 05 2025
|
122 |
+
r'(\d{4}[A-Za-z]{3}\d{2})', # 2025MAY11
|
123 |
+
r'(?:best\s*before\s*[:\-]?\s*(\d+)\s*(days?|months?|years?))', # Best Before: 6 months
|
124 |
+
r'(?:best\s*before\s*[:\-]?\s*(three)\s*(months?))', # Best Before: three months
|
125 |
+
r'(\b(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b\s*\d{4})', # Month name with year
|
126 |
+
r'\bUSE BY\s+(\d{1,2}[A-Za-z]{3}\d{4})\b', # Use by with month name
|
127 |
+
r"Exp\.Date\s*(\d{2}[A-Z]{3}\d{4})", # Expiration date format
|
128 |
+
r"EXP:\d{4}/\d{2}/\d{4}/\d{1}/[A-Z]" # EXP format
|
129 |
]
|
130 |
|
131 |
current_date = datetime.now()
|
|
|
146 |
continue
|
147 |
|
148 |
days_left = (expiry_date - current_date).days
|
149 |
+
if days_left < 0:
|
150 |
+
dates_info.append((date_str, "Expired"))
|
151 |
+
else:
|
152 |
+
dates_info.append((date_str, f"{days_left} days left"))
|
153 |
break # Stop after finding the first valid date
|
154 |
|
155 |
return dates_info
|
|
|
158 |
st.title("Image Processing Application")
|
159 |
|
160 |
# User choice for processing
|
161 |
+
task_choice = st.radio("Choose a task", ("Text and Brand Detection", "Freshness Detection", "Object Detection"))
|
162 |
|
163 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
164 |
|
|
|
189 |
if expiry_dates_info:
|
190 |
st.write("Expiry Dates Found:")
|
191 |
for date_str, days_left in expiry_dates_info:
|
192 |
+
st.text(f"{date_str} - {days_left}")
|
193 |
else:
|
194 |
st.write("No expiry dates found.")
|
195 |
|
|
|
229 |
img_array = preprocess_image(image_path)
|
230 |
predictions = fruit_model.predict(img_array)
|
231 |
predicted_class = np.argmax(predictions, axis=1)[0]
|
232 |
+
st.write("Predicted Freshness:", class_names[predicted_class])
|
233 |
+
|
234 |
+
elif task_choice == "Object Detection":
|
235 |
+
# Object Detection
|
236 |
+
st.header("Object Detection")
|
237 |
+
results = brand_model(image_path) # Using YOLOv5s model for detection
|
238 |
+
detected_objects = []
|
239 |
+
|
240 |
+
for result in results:
|
241 |
+
boxes = result.boxes.data.cpu().numpy()
|
242 |
+
for box in boxes:
|
243 |
+
class_id = int(box[5])
|
244 |
+
confidence = box[4] # Assuming the confidence score is at index 4
|
245 |
+
detected_objects.append((result.names[class_id], confidence))
|
246 |
+
|
247 |
+
# Count occurrences and average confidence of each object
|
248 |
+
object_data = {}
|
249 |
+
for obj, confidence in detected_objects:
|
250 |
+
if obj in object_data:
|
251 |
+
object_data[obj]['count'] += 1
|
252 |
+
object_data[obj]['total_confidence'] += confidence
|
253 |
+
else:
|
254 |
+
object_data[obj] = {'count': 1, 'total_confidence': confidence}
|
255 |
+
|
256 |
+
# Prepare data for display
|
257 |
+
object_display_data = [
|
258 |
+
{'Object': obj, 'Count': data['count'], 'Average Confidence': data['total_confidence'] / data['count']}
|
259 |
+
for obj, data in object_data.items()
|
260 |
+
]
|
261 |
+
|
262 |
+
# Display detected objects in a table with column names
|
263 |
+
st.write("Detected Objects and Counts:")
|
264 |
+
st.table(pd.DataFrame(object_display_data))
|