21501A0580 commited on
Commit
9300b43
·
verified ·
1 Parent(s): 56c981f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -114
app.py CHANGED
@@ -24,23 +24,20 @@ class_names = {
24
  }
25
 
26
  # Helper function: Extract expiry dates
27
- # Helper function: Extract unique expiry dates
28
  def extract_expiry_dates(text):
29
  patterns = [
30
- r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # 20/07/2024 or 20-07-2024
31
- r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2})', # 20/07/24 or 20-07-24
32
- r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{4})', # 20 MAY 2024
33
- r'([A-Za-z]{3,}\s*\d{1,2}[,\s]*\d{4})', # July 20, 2024
34
- r'(\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2})', # 2024/07/20 or 2024-07-20
35
- r'([A-Za-z]{3}[\-]\d{1,2}[\-]\d{4})' # JAN-15-2024
36
  ]
37
- # Helper function: Extract expiry dates
38
  dates = []
39
  for pattern in patterns:
40
  matches = re.findall(pattern, text)
41
  dates.extend(matches)
42
 
43
- # Deduplicate and prioritize the longest (likely full year) date format
44
  unique_dates = sorted(dates, key=lambda x: len(x), reverse=True)
45
  return [unique_dates[0]] if unique_dates else [] # Return the most likely date
46
 
@@ -58,7 +55,6 @@ def calculate_days_to_expiry(expiry_dates):
58
  today = datetime.now()
59
  for date_str in expiry_dates:
60
  try:
61
- # Parse date
62
  if '/' in date_str or '-' in date_str:
63
  if len(date_str.split('/')[-1]) == 2 or len(date_str.split('-')[-1]) == 2:
64
  date_obj = datetime.strptime(date_str, '%d/%m/%y') if '/' in date_str else datetime.strptime(date_str, '%d-%m-%y')
@@ -67,7 +63,6 @@ def calculate_days_to_expiry(expiry_dates):
67
  else:
68
  date_obj = datetime.strptime(date_str, '%d %B %Y')
69
 
70
- # Calculate difference
71
  delta = (date_obj - today).days
72
  if delta >= 0:
73
  results.append(f"{date_str}: {delta} days to expire")
@@ -77,13 +72,18 @@ def calculate_days_to_expiry(expiry_dates):
77
  results.append(f"{date_str}: Invalid date format")
78
  return results
79
 
 
 
 
 
80
  # Streamlit App
81
  st.title("Flipkart Grid")
82
 
83
  # Sidebar options
84
  app_mode = st.sidebar.selectbox(
85
  "Choose the mode",
86
- ["Home", "Brand & Text Detection", "Fruit Freshness Detection", "Object Detection"]
 
87
  )
88
 
89
  if app_mode == "Home":
@@ -95,113 +95,103 @@ if app_mode == "Home":
95
  - *Object Detection*: Detect objects in uploaded images.
96
  """)
97
 
98
- elif app_mode == "Brand & Text Detection":
99
- st.header("Brand & Text Detection")
100
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
101
-
102
- if uploaded_file is not None:
103
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
104
- image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
105
-
106
- # Brand detection
107
- results = brand_model.predict(source=image, stream=False)
108
- detected_image = results[0].plot()
109
-
110
- # OCR for text extraction
111
- _, img_buffer = cv2.imencode('.jpg', image)
112
- ocr_result = ocr.ocr(img_buffer.tobytes())
113
- if ocr_result and isinstance(ocr_result[0], list) and len(ocr_result[0]) > 0:
114
- extracted_text = ' '.join([line[1][0] for line in ocr_result[0]])
115
- expiry_dates = extract_expiry_dates(extracted_text)
116
- expiry_info = calculate_days_to_expiry(expiry_dates)
117
- else:
118
- extracted_text = "No text detected"
119
- expiry_dates = []
120
- expiry_info = []
121
-
122
- # Count objects
123
- object_counts = {}
124
- for box in results[0].boxes.data.cpu().numpy():
125
- label = results[0].names[int(box[5])]
126
- object_counts[label] = object_counts.get(label, 0) + 1
127
-
128
- # Display results
129
- st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected Image")
130
- st.markdown(f"*Extracted Text:* {extracted_text}")
131
- st.markdown(f"*Expiry Dates:*")
132
- if expiry_info:
133
- for info in expiry_info:
134
- st.markdown(f"- {info}")
135
- else:
136
- st.markdown("None")
137
- st.markdown("*Object Counts:*")
138
- for label, count in object_counts.items():
139
- st.markdown(f"- {label}: {count}")
140
-
141
- elif app_mode == "Fruit Freshness Detection":
142
- st.header("Fruit Freshness Detection")
143
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
144
 
145
  if uploaded_file is not None:
146
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
147
- image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
148
 
149
- # Preprocess and predict
150
- img_array = preprocess_image(image)
151
- predictions = fruit_model.predict(img_array)
152
- predicted_class = np.argmax(predictions, axis=1)[0]
153
- label = class_names[predicted_class]
154
- confidence = predictions[0][predicted_class] * 100
155
-
156
- # Display results
157
- st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image")
158
- st.markdown(f"*Label:* {label}")
159
- st.markdown(f"*Confidence:* {confidence:.2f}%")
160
-
161
- elif app_mode == "Object Detection":
162
- st.header("Object Detection")
163
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
164
-
165
- if uploaded_file is not None:
166
- file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
167
  image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
168
 
169
- # Object Detection
170
- results = object_model.predict(source=image, stream=False)
171
- detected_objects = []
172
-
173
- for result in results:
174
- boxes = result.boxes.data.cpu().numpy()
175
- for box in boxes:
176
- class_id = int(box[5])
177
- confidence = box[4] # Assuming the confidence score is at index 4
178
- detected_objects.append((result.names[class_id], confidence))
179
-
180
- # Draw bounding box and label on the image
181
- x1, y1, x2, y2 = map(int, box[:4])
182
- label = f"{result.names[class_id]} {confidence * 100:.2f}%"
183
- cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
184
- cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
185
-
186
- # Convert the image back to RGB for display in Streamlit
187
- image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
188
- st.image(image_rgb, caption='Detected Objects', use_container_width=True)
189
-
190
- # Count occurrences and average confidence of each object
191
- object_data = {}
192
- for obj, confidence in detected_objects:
193
- if obj in object_data:
194
- object_data[obj]['count'] += 1
195
- object_data[obj]['total_confidence'] += confidence
 
 
 
196
  else:
197
- object_data[obj] = {'count': 1, 'total_confidence': confidence}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
- # Prepare data for display
200
- object_display_data = [
201
- {'Object': obj, 'Count': data['count'], 'Average Confidence': data['total_confidence'] / data['count']}
202
- for obj, data in object_data.items()
203
- ]
204
 
205
- # Display detected objects in a table with column names
206
- st.write("Detected Objects and Counts:")
207
- st.table(pd.DataFrame(object_display_data))
 
24
  }
25
 
26
  # Helper function: Extract expiry dates
 
27
  def extract_expiry_dates(text):
28
  patterns = [
29
+ r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{4})', # 20/07/2024 or 20-07-2024
30
+ r'(\d{1,2}[\/\-]\d{1,2}[\/\-]\d{2})', # 20/07/24 or 20-07-24
31
+ r'(\d{1,2}\s*[A-Za-z]{3,}\s*\d{4})', # 20 MAY 2024
32
+ r'([A-Za-z]{3,}\s*\d{1,2}[,\s]*\d{4})', # July 20, 2024
33
+ r'(\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2})', # 2024/07/20 or 2024-07-20
34
+ r'([A-Za-z]{3}[\-]\d{1,2}[\-]\d{4})' # JAN-15-2024
35
  ]
 
36
  dates = []
37
  for pattern in patterns:
38
  matches = re.findall(pattern, text)
39
  dates.extend(matches)
40
 
 
41
  unique_dates = sorted(dates, key=lambda x: len(x), reverse=True)
42
  return [unique_dates[0]] if unique_dates else [] # Return the most likely date
43
 
 
55
  today = datetime.now()
56
  for date_str in expiry_dates:
57
  try:
 
58
  if '/' in date_str or '-' in date_str:
59
  if len(date_str.split('/')[-1]) == 2 or len(date_str.split('-')[-1]) == 2:
60
  date_obj = datetime.strptime(date_str, '%d/%m/%y') if '/' in date_str else datetime.strptime(date_str, '%d-%m-%y')
 
63
  else:
64
  date_obj = datetime.strptime(date_str, '%d %B %Y')
65
 
 
66
  delta = (date_obj - today).days
67
  if delta >= 0:
68
  results.append(f"{date_str}: {delta} days to expire")
 
72
  results.append(f"{date_str}: Invalid date format")
73
  return results
74
 
75
+ # Initialize session state
76
+ if 'uploaded_file' not in st.session_state:
77
+ st.session_state['uploaded_file'] = None
78
+
79
  # Streamlit App
80
  st.title("Flipkart Grid")
81
 
82
  # Sidebar options
83
  app_mode = st.sidebar.selectbox(
84
  "Choose the mode",
85
+ ["Home", "Brand & Text Detection", "Fruit Freshness Detection", "Object Detection"],
86
+ on_change=lambda: st.session_state.update({'uploaded_file': None}) # Reset uploaded file on mode change
87
  )
88
 
89
  if app_mode == "Home":
 
95
  - *Object Detection*: Detect objects in uploaded images.
96
  """)
97
 
98
+ else:
99
+ st.header(app_mode)
100
+ uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key=app_mode)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  if uploaded_file is not None:
103
+ st.session_state['uploaded_file'] = uploaded_file
 
104
 
105
+ if st.session_state['uploaded_file'] is not None:
106
+ file_bytes = np.asarray(bytearray(st.session_state['uploaded_file'].read()), dtype=np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
108
 
109
+ if app_mode == "Brand & Text Detection":
110
+ # Brand detection
111
+ results = brand_model.predict(source=image, stream=False)
112
+ detected_image = results[0].plot()
113
+
114
+ # OCR for text extraction
115
+ _, img_buffer = cv2.imencode('.jpg', image)
116
+ ocr_result = ocr.ocr(img_buffer.tobytes())
117
+ if ocr_result and isinstance(ocr_result[0], list) and len(ocr_result[0]) > 0:
118
+ extracted_text = ' '.join([line[1][0] for line in ocr_result[0]])
119
+ expiry_dates = extract_expiry_dates(extracted_text)
120
+ expiry_info = calculate_days_to_expiry(expiry_dates)
121
+ else:
122
+ extracted_text = "No text detected"
123
+ expiry_dates = []
124
+ expiry_info = []
125
+
126
+ # Count objects
127
+ object_counts = {}
128
+ for box in results[0].boxes.data.cpu().numpy():
129
+ label = results[0].names[int(box[5])]
130
+ object_counts[label] = object_counts.get(label, 0) + 1
131
+
132
+ # Display results
133
+ st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected Image")
134
+ st.markdown(f"*Extracted Text:* {extracted_text}")
135
+ st.markdown(f"*Expiry Dates:*")
136
+ if expiry_info:
137
+ for info in expiry_info:
138
+ st.markdown(f"- {info}")
139
  else:
140
+ st.markdown("None")
141
+ st.markdown("*Object Counts:*")
142
+ for label, count in object_counts.items():
143
+ st.markdown(f"- {label}: {count}")
144
+
145
+ elif app_mode == "Fruit Freshness Detection":
146
+ # Preprocess and predict
147
+ img_array = preprocess_image(image)
148
+ predictions = fruit_model.predict(img_array)
149
+ predicted_class = np.argmax(predictions, axis=1)[0]
150
+ label = class_names[predicted_class]
151
+ confidence = predictions[0][predicted_class] * 100
152
+
153
+ # Display results
154
+ st.image(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), caption="Uploaded Image")
155
+ st.markdown(f"*Label:* {label}")
156
+ st.markdown(f"*Confidence:* {confidence:.2f}%")
157
+
158
+ elif app_mode == "Object Detection":
159
+ # Object Detection
160
+ results = object_model.predict(source=image, stream=False)
161
+ detected_objects = []
162
+
163
+ for result in results:
164
+ boxes = result.boxes.data.cpu().numpy()
165
+ for box in boxes:
166
+ class_id = int(box[5])
167
+ confidence = box[4] # Assuming the confidence score is at index 4
168
+ detected_objects.append((result.names[class_id], confidence))
169
+
170
+ # Draw bounding box and label on the image
171
+ x1, y1, x2, y2 = map(int, box[:4])
172
+ label = f"{result.names[class_id]} {confidence * 100:.2f}%"
173
+ cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
174
+ cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
175
+
176
+ # Convert the image back to RGB for display in Streamlit
177
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
178
+ st.image(image_rgb, caption='Detected Objects', use_container_width=True)
179
+
180
+ # Count occurrences and average confidence of each object
181
+ object_data = {}
182
+ for obj, confidence in detected_objects:
183
+ if obj in object_data:
184
+ object_data[obj]['count'] += 1
185
+ object_data[obj]['total_confidence'] += confidence
186
+ else:
187
+ object_data[obj] = {'count': 1, 'total_confidence': confidence}
188
 
189
+ # Prepare data for display
190
+ object_display_data = [
191
+ {'Object': obj, 'Count': data['count'], 'Average Confidence': data['total_confidence'] / data['count']}
192
+ for obj, data in object_data.items()
193
+ ]
194
 
195
+ # Display detected objects in a table with column names
196
+ st.write("Detected Objects and Counts:")
197
+ st.table(pd.DataFrame(object_display_data))