Abhinav-hf commited on
Commit
13c0b53
·
verified ·
1 Parent(s): 94ae5cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -149
app.py CHANGED
@@ -1,16 +1,15 @@
1
  # -*- coding: utf-8 -*-
2
- """Complete_3_model_code.ipynb
3
 
4
  Automatically generated by Colab.
5
 
6
  Original file is located at
7
- https://colab.research.google.com/drive/1Ivlv1jHXwoldi9Mb-quvDBlCNeIIAhc9
8
 
9
  # 1. Install Gradio and Required Libraries
10
  ### Start by installing Gradio if it's not already installed.
11
  """
12
 
13
-
14
  """# 2. Import Libraries
15
  ### Getting all the necessary Libraries
16
  """
@@ -24,9 +23,7 @@ import time
24
  from ultralytics import YOLO
25
  import pandas as pd
26
  from collections import defaultdict, deque
27
- import matplotlib.pyplot as plt
28
  import torch
29
- from PIL import Image
30
  from torchvision import transforms, models, datasets, transforms
31
  from torch.utils.data import DataLoader
32
  import torch.nn as nn
@@ -35,19 +32,28 @@ import google.generativeai as genai
35
  from datetime import datetime
36
  from paddleocr import PaddleOCR
37
  import os
 
38
 
39
- """# 3. Import Drive
40
 
 
41
  """
42
 
43
- # from google.colab import drive
44
- # drive.mount('/content/drive')
 
 
 
 
 
45
 
46
  """# 4. Brand Recognition Backend
47
 
48
- ### Image uploading for Grocery detection
49
  """
50
 
 
 
51
  def detect_grocery_items(image):
52
  model = YOLO('kitkat_s.pt')
53
  image = np.array(image)[:, :, ::-1]
@@ -223,36 +229,55 @@ def annotate_video(input_video):
223
  Run these 3 cells before trying out any model
224
  """
225
 
226
- # Function to draw bounding boxes and show text
227
- def draw_bounding_boxes(image_path):
228
- # Read the image
229
- img = Image.open(image_path)
230
- result = ocr.ocr(image_path, cls=True) # Get the OCR result
 
 
 
 
 
 
 
 
231
 
232
- # Create a figure to display the image
233
- plt.figure(figsize=(10, 10))
234
- plt.imshow(img)
235
- ax = plt.gca()
236
- all_text_data = []
237
- # Iterate through the results and draw boxes
238
- for idx, line in enumerate(result[0]):
239
- box = line[0] # Get the bounding box coordinates
240
- text = line[1][0] # Extracted text
241
- print(f"[DEBUG] Box {idx + 1}: {text}") # Display text with box number
242
- all_text_data.append(f"{text}")
243
 
244
- # Draw the bounding box
245
- polygon = plt.Polygon(box, fill=None, edgecolor='red', linewidth=2)
246
- ax.add_patch(polygon)
247
- # Add text label in the box
248
- # ax.text(box[0][0], box[0][1] - 5, f"{idx + 1}: {text}", color='blue', fontsize=12)
249
 
250
- plt.axis('off') # Hide axes
251
- plt.show()
252
- return all_text_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
- # Set your API key securely (store it in Colab’s userdata)
255
- GOOGLE_API_KEY= os.getenv("GEMINI_API")
256
  genai.configure(api_key=GOOGLE_API_KEY)
257
 
258
  def gemini_context_correction(text):
@@ -265,14 +290,14 @@ def gemini_context_correction(text):
265
  f"The text may contain noise or unclear information. If only one date is provided, assume it is the Expiration Date. "
266
  f"Additionally, extract the MRP (e.g., 'MRP: ₹99.00', 'Rs. 99/-'). "
267
  f"Format the output as:\n"
268
- f"Manufacturing Date: <MFG Date>\n"
269
- f"Expiration Date: <EXP Date>\n"
270
- f"MRP: <MRP Value>\n\n"
271
  f"Here is the text: {text}"
272
  )
273
 
274
  return response.text
275
 
 
 
276
  def validate_dates_with_gemini(mfg_date, exp_date):
277
  """Use Gemini API to validate and correct the manufacturing and expiration dates."""
278
  model = genai.GenerativeModel('models/gemini-1.5-flash')
@@ -295,55 +320,6 @@ def validate_dates_with_gemini(mfg_date, exp_date):
295
  return "Invalid response from Gemini API."
296
 
297
 
298
- def extract_and_validate_with_gemini(refined_text):
299
- """
300
- Use Gemini API to extract, validate, and correct manufacturing and expiration dates.
301
- """
302
- model = genai.GenerativeModel('models/gemini-1.5-flash')
303
-
304
- # Correctly call the generate_content method
305
- response = model.generate_content(
306
- f"The extracted text is:\n'{refined_text}'\n\n"
307
- f"1. Extract the 'Manufacturing Date' and 'Expiration Date' from the above text. "
308
- f"Ignore unrelated data (e.g., 'MRP: Not Found').\n"
309
- f"2. If a date is missing or invalid, return -1 for that date.\n"
310
- f"3. If the 'Expiration Date' is earlier than the 'Manufacturing Date', swap them.\n"
311
- f"4. Ensure both dates are in 'dd/mm/yyyy' format. If the original dates are not in this format, convert them.\n"
312
- f"Respond ONLY in this exact format:\n"
313
- f"Manufacturing Date: <MFG Date>, Expiration Date: <EXP Date>"
314
- )
315
- print("[DEBUG] Response from validation function", response)
316
- # Ensure the response object is valid and contains the required parts
317
- if hasattr(response, 'parts') and response.parts:
318
- final_dates = response.parts[0].text.strip()
319
- print(f"[DEBUG] Gemini Response: {final_dates}")
320
-
321
- # Extract the dates from the response
322
- mfg_date_str, exp_date_str = parse_gemini_response(final_dates)
323
-
324
- # Process and swap if necessary
325
- if mfg_date_str != "-1" and exp_date_str != "-1":
326
- mfg_date = datetime.strptime(mfg_date_str, "%Y/%m/%d")
327
- exp_date = datetime.strptime(exp_date_str, "%Y/%m/%d")
328
-
329
- # Swap if Expiration Date is earlier than Manufacturing Date
330
- if exp_date < mfg_date:
331
- print("[DEBUG] Swapping dates.")
332
- mfg_date, exp_date = exp_date, mfg_date
333
-
334
- # Return the formatted swapped dates
335
- return (
336
- f"Manufacturing Date: {mfg_date.strftime('%Y/%m/%d')}, "
337
- f"Expiration Date: {exp_date.strftime('%Y/%m/%d')}"
338
- )
339
-
340
- # If either date is -1, return them as-is
341
- return final_dates
342
-
343
- # Handle invalid responses gracefully
344
- print("[ERROR] Invalid response from Gemini API.")
345
- return "Invalid response from Gemini API."
346
-
347
  def extract_and_validate_with_gemini(refined_text):
348
  """
349
  Use Gemini API to extract, validate, correct, and swap dates in 'yyyy/mm/dd' format if necessary.
@@ -353,13 +329,18 @@ def extract_and_validate_with_gemini(refined_text):
353
  # Generate content using Gemini with the refined prompt
354
  response = model.generate_content(
355
  f"The extracted text is:\n'{refined_text}'\n\n"
356
- f"1. Extract the 'Manufacturing Date' and 'Expiration Date' from the above text. "
357
- f"Ignore unrelated data (e.g., 'MRP: Not Found').\n"
358
- f"2. If a date is missing or invalid, return -1 for that date.\n"
359
  f"3. If the 'Expiration Date' is earlier than the 'Manufacturing Date', swap them.\n"
360
- f"4. Ensure both dates are in 'dd/mm/yyyy' format. If the original dates are not in this format, convert them.\n"
 
 
 
361
  f"Respond ONLY in this exact format:\n"
362
- f"Manufacturing Date: <MFG Date>, Expiration Date: <EXP Date>"
 
 
363
  )
364
 
365
  # Validate the response and extract dates
@@ -368,12 +349,13 @@ def extract_and_validate_with_gemini(refined_text):
368
  print(f"[DEBUG] Gemini Response: {final_dates}")
369
 
370
  # Extract the dates from the response
371
- mfg_date_str, exp_date_str = parse_gemini_response(final_dates)
372
 
373
  # Process and swap if necessary
374
  if mfg_date_str != "-1" and exp_date_str != "-1":
375
- mfg_date = datetime.strptime(mfg_date_str, "%d/%m/%Y")
376
- exp_date = datetime.strptime(exp_date_str, "%d/%m/%Y")
 
377
 
378
  # Swap if Expiration Date is earlier than Manufacturing Date
379
  swapping_statement = ""
@@ -384,8 +366,9 @@ def extract_and_validate_with_gemini(refined_text):
384
 
385
  # Return the formatted swapped dates
386
  return swapping_statement + (
387
- f"Manufacturing Date: {mfg_date.strftime('%d/%m/%Y')}, "
388
- f"Expiration Date: {exp_date.strftime('%d/%m/%Y')}"
 
389
  )
390
 
391
  # If either date is -1, return them as-is
@@ -400,14 +383,32 @@ def parse_gemini_response(response_text):
400
  Helper function to extract Manufacturing Date and Expiration Date from the response text.
401
  """
402
  try:
403
- # Split and extract the dates
404
  parts = response_text.split(", ")
405
  mfg_date_str = parts[0].split(": ")[1].strip()
406
  exp_date_str = parts[1].split(": ")[1].strip()
407
- return mfg_date_str, exp_date_str
 
408
  except IndexError:
409
  print("[ERROR] Failed to parse Gemini response.")
410
- return "-1", "-1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
411
 
412
  def extract_date(refined_text, date_type):
413
  """Extract the specified date type from the refined text."""
@@ -422,6 +423,37 @@ def extract_date(refined_text, date_type):
422
  return '-1' # Return -1 if the date is not found
423
  return '-1' # Return -1 if the date type is not in the text
424
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  """### **Model 3**
426
  Using Yolov8 x-large model trained till about 75 epochs
427
  and
@@ -430,11 +462,6 @@ Gradio as user interface
430
 
431
  """
432
 
433
- model = YOLO('best.pt')
434
- """## Driver code to be run after selecting from Model 2 or 3.
435
- (Note: not needed for model 1)
436
- """
437
-
438
  def new_draw_bounding_boxes(image):
439
  """Draw bounding boxes around detected text in the image and display it."""
440
  # If the input is a string (file path), open the image
@@ -475,10 +502,13 @@ def new_draw_bounding_boxes(image):
475
 
476
  return all_text_data
477
 
 
478
  # Initialize PaddleOCR
479
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
480
 
481
  def detect_and_ocr(image):
 
 
482
  """Detect objects using YOLO, draw bounding boxes, and perform OCR."""
483
  # Convert input image from PIL to OpenCV format
484
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
@@ -542,31 +572,61 @@ def further_processing(image, previous_result_text):
542
 
543
  def handle_processing(validated_output):
544
  """Decide whether to proceed with further processing."""
545
- # Extract the manufacturing and expiration dates from the string
546
  try:
547
- mfg_date_str = validated_output.split("Manufacturing Date: ")[1].split(",")[0].strip()
548
- exp_date_str = validated_output.split("Expiration Date: ")[1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
549
 
550
- # Convert the extracted values to integers
551
- mfg_date = int(mfg_date_str)
552
- exp_date = int(exp_date_str)
553
- print("Further processing: ", mfg_date, exp_date)
 
 
 
 
 
554
 
555
- except (IndexError, ValueError) as e:
556
- print(f"[ERROR] Failed to parse dates: {e}")
 
 
557
  return gr.update(visible=False) # Hide button on error
558
 
559
- # Check if both dates are -1
560
- if mfg_date == -1 and exp_date == -1:
561
  print("[DEBUG] Showing the 'Further Processing' button.") # Debug print
562
  return gr.update(visible=True) # Show 'Further Processing' button
 
563
  print("[DEBUG] Hiding the 'Further Processing' button.") # Debug print
564
- return gr.update(visible=False) # Hide button if dates are valid
565
 
566
- """# Freshness Backend"""
567
 
568
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
569
 
 
 
570
 
571
  class EfficientNet_FeatureExtractor(nn.Module):
572
 
@@ -580,10 +640,8 @@ class EfficientNet_FeatureExtractor(nn.Module):
580
  x = x.view(x.size(0), -1)
581
 
582
  return x
583
-
584
  # Calculating the mean and variance of the images whose features will be extracted
585
 
586
-
587
  transform = transforms.Compose([
588
  transforms.Resize(256),
589
  transforms.CenterCrop(224),
@@ -617,6 +675,7 @@ std /= total_images
617
  print(f"Mean: {mean}")
618
  print(f"Std: {std}")
619
 
 
620
  # Transforming the images into the format so that they can be passes through the EfficientNet model
621
  # Define the transform for your dataset, including normalization with custom mean and std
622
  transform = transforms.Compose([
@@ -765,7 +824,6 @@ def classify_banana_by_distance(distance):
765
  }
766
 
767
  return result
768
-
769
  def classify_banana(image):
770
 
771
  model = EfficientNet_FeatureExtractor().to(device)
@@ -784,9 +842,6 @@ def classify_banana(image):
784
  distance = (distance) / 1e8
785
 
786
  return classify_banana_by_distance(distance)
787
-
788
- """## Freshness Detect Using image"""
789
-
790
  def detect_objects(image):
791
 
792
 
@@ -817,14 +872,14 @@ def detect_objects(image):
817
 
818
  return img_rgb
819
 
820
- """## Freshness Detect using Video"""
821
-
822
  def detect_objects_video(video_file):
823
-
824
- # Load the YOLO model
825
  model = YOLO('Yash_Best.pt')
826
  # Open the video file
827
- cap = cv2.VideoCapture(video_file.name)
 
 
 
 
828
 
829
  # Get video properties
830
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -866,6 +921,7 @@ def detect_objects_video(video_file):
866
 
867
  return output_video_path
868
 
 
869
  """# 5. Frontend Of Brand Recognition
870
 
871
  ## Layout for Image interface
@@ -924,15 +980,15 @@ def create_ocr_interface():
924
  gr.Markdown("# Flipkart Grid Robotics Track - OCR Interface")
925
 
926
  with gr.Tabs():
 
927
  with gr.TabItem("Upload & Detection"):
928
  with gr.Row():
929
- # Input: Upload image
930
  input_image = gr.Image(type="pil", label="Upload Image", height=400, width=400)
931
  output_image = gr.Image(label="Image with Bounding Boxes", height=400, width=400)
932
 
933
- # Button for Analyze Image & Extract Text
934
  btn = gr.Button("Analyze Image & Extract Text")
935
 
 
936
  with gr.TabItem("OCR Results"):
937
  with gr.Row():
938
  extracted_textbox = gr.Textbox(label="Extracted OCR Text", lines=5)
@@ -941,7 +997,16 @@ def create_ocr_interface():
941
  with gr.Row():
942
  validated_textbox = gr.Textbox(label="Validated Output", lines=5)
943
 
944
- # Comprehensive OCR button (Initially hidden)
 
 
 
 
 
 
 
 
 
945
  further_button = gr.Button("Comprehensive OCR", visible=False)
946
 
947
  # Detect and OCR button click event
@@ -951,6 +1016,13 @@ def create_ocr_interface():
951
  outputs=[output_image, extracted_textbox, refined_textbox, validated_textbox]
952
  )
953
 
 
 
 
 
 
 
 
954
  # Further processing button click event
955
  further_button.click(
956
  further_processing,
@@ -965,7 +1037,6 @@ def create_ocr_interface():
965
  outputs=[further_button]
966
  )
967
 
968
- # Hide the validated_textbox when "Comprehensive OCR" is clicked
969
  further_button.click(
970
  lambda: gr.update(visible=False),
971
  outputs=[validated_textbox]
@@ -973,16 +1044,11 @@ def create_ocr_interface():
973
 
974
  return ocr_interface
975
 
976
- # Create and launch the OCR interface
977
  ocr_interface = create_ocr_interface()
978
- # ocr_interface.launch(share=True, debug=True)
979
 
980
-
981
- """# Frontend for Fruit Freshness
982
-
983
- ## Layout for Freshness Index
984
  """
985
-
986
  def create_banana_classifier_interface():
987
  return gr.Interface(
988
  fn=classify_banana, # Your classification function
@@ -996,8 +1062,8 @@ def create_banana_classifier_interface():
996
  def image_freshness_interface():
997
  return gr.Interface(
998
  fn=detect_objects, # Your detection function
999
- inputs=gr.Image(type="numpy", label="Upload an Image"), # Removed tool argument
1000
- outputs=gr.Image(type="numpy", label="Detected Image"),
1001
  live=True,
1002
  title="Image Freshness Detection",
1003
  description="Upload an image of fruit to detect freshness.",
@@ -1006,13 +1072,15 @@ def image_freshness_interface():
1006
 
1007
  def video_freshness_interface():
1008
  return gr.Interface(
1009
- fn=process_video, # Your video processing function
1010
  inputs=gr.Video(label="Upload a Video"),
1011
- outputs=gr.Video(label="Processed Video"),
 
 
1012
  title="Video Freshness Detection",
1013
  description="Upload a video of fruit to detect freshness.",
1014
  css="#component-0 { width: 300px; height: 300px; }" # Keep your CSS for fixed size
1015
- )
1016
 
1017
  def create_fruit_interface():
1018
  with gr.Blocks() as demo:
@@ -1029,9 +1097,8 @@ def create_fruit_interface():
1029
 
1030
  Fruit = create_fruit_interface()
1031
 
1032
- """# 6. Create a Tabbed Interface for Both Image and Video
1033
  ### Here, we combine the image and video interfaces into a tabbed structure so users can switch between them easily.
1034
- """
1035
 
1036
  def create_tabbed_interface():
1037
  return gr.TabbedInterface(
@@ -1045,4 +1112,4 @@ tabbed_interface = create_tabbed_interface()
1045
  ### Finally, launch the Gradio interface to make it interactable.
1046
  """
1047
 
1048
- tabbed_interface.launch()
 
1
  # -*- coding: utf-8 -*-
2
+ """Flipkart Frontend.ipynb
3
 
4
  Automatically generated by Colab.
5
 
6
  Original file is located at
7
+ https://colab.research.google.com/github/Abhinav-gh/404NotFound/blob/main/Flipkart%20Frontend.ipynb
8
 
9
  # 1. Install Gradio and Required Libraries
10
  ### Start by installing Gradio if it's not already installed.
11
  """
12
 
 
13
  """# 2. Import Libraries
14
  ### Getting all the necessary Libraries
15
  """
 
23
  from ultralytics import YOLO
24
  import pandas as pd
25
  from collections import defaultdict, deque
 
26
  import torch
 
27
  from torchvision import transforms, models, datasets, transforms
28
  from torch.utils.data import DataLoader
29
  import torch.nn as nn
 
32
  from datetime import datetime
33
  from paddleocr import PaddleOCR
34
  import os
35
+ import re
36
 
37
+ """# Path Variables
38
 
39
+ ### Path used in OCR
40
  """
41
 
42
+ # OCR_M3="best.pt"
43
+ GOOGLE_API_KEY = os.getenv("GEMINI_API")
44
+ # GEMINI_MODEL = 'models/gemini-1.5-flash'
45
+
46
+
47
+ # Brand_Recognition_Model ='kitkat_s.pt'
48
+ # annotatedOpFile= 'annotated_output.mp4'
49
 
50
  """# 4. Brand Recognition Backend
51
 
52
+ ### Model for Grocery Detection
53
  """
54
 
55
+ """### Image uploading for Grocery detection"""
56
+
57
  def detect_grocery_items(image):
58
  model = YOLO('kitkat_s.pt')
59
  image = np.array(image)[:, :, ::-1]
 
229
  Run these 3 cells before trying out any model
230
  """
231
 
232
+ def new_draw_bounding_boxes(image):
233
+ """Draw bounding boxes around detected text in the image and display it."""
234
+ try:
235
+ # Check the input type and load the image
236
+ if isinstance(image, str):
237
+ img = Image.open(image)
238
+ np_img = np.array(img) # Convert to NumPy array
239
+ print("[DEBUG] Loaded image from file path.")
240
+ elif isinstance(image, Image.Image):
241
+ np_img = np.array(image) # Convert PIL Image to NumPy array
242
+ print("[DEBUG] Converted PIL Image to NumPy array.")
243
+ else:
244
+ raise ValueError("Input must be a file path or a PIL Image object.")
245
 
246
+ # Perform OCR on the array
247
+ ocr_result = ocr.ocr(np_img, cls=True) # Ensure this line is error-free
248
+ print("[DEBUG] OCR Result:\n", ocr_result)
 
 
 
 
 
 
 
 
249
 
250
+ # Create a figure to display the image
251
+ plt.figure(figsize=(10, 10))
252
+ plt.imshow(image)
253
+ ax = plt.gca()
254
+ all_text_data = []
255
 
256
+ # Iterate through the OCR results and draw boxes
257
+ for idx, line in enumerate(ocr_result[0]):
258
+ box = line[0] # Get the bounding box coordinates
259
+ text = line[1][0] # Extracted text
260
+ print(f"[DEBUG] Box {idx + 1}: {text}") # Debug print
261
+ all_text_data.append(text)
262
+
263
+ # Draw the bounding box
264
+ polygon = plt.Polygon(box, fill=None, edgecolor='red', linewidth=2)
265
+ ax.add_patch(polygon)
266
+
267
+ # Add text label with a small offset for visibility
268
+ x, y = box[0][0], box[0][1]
269
+ ax.text(x, y - 5, f"{idx + 1}: {text}", color='blue', fontsize=12, ha='left')
270
+
271
+ plt.axis('off') # Hide axes
272
+ plt.title("Detected Text with Bounding Boxes", fontsize=16) # Add a title
273
+ plt.show()
274
+
275
+ return all_text_data
276
+
277
+ except Exception as e:
278
+ print(f"[ERROR] Error in new_draw_bounding_boxes: {e}")
279
+ return []
280
 
 
 
281
  genai.configure(api_key=GOOGLE_API_KEY)
282
 
283
  def gemini_context_correction(text):
 
290
  f"The text may contain noise or unclear information. If only one date is provided, assume it is the Expiration Date. "
291
  f"Additionally, extract the MRP (e.g., 'MRP: ₹99.00', 'Rs. 99/-'). "
292
  f"Format the output as:\n"
293
+ f"Manufacturing Date: <MFG Date> Expiration Date: <EXP Date> MRP: <MRP Value>"
 
 
294
  f"Here is the text: {text}"
295
  )
296
 
297
  return response.text
298
 
299
+
300
+
301
  def validate_dates_with_gemini(mfg_date, exp_date):
302
  """Use Gemini API to validate and correct the manufacturing and expiration dates."""
303
  model = genai.GenerativeModel('models/gemini-1.5-flash')
 
320
  return "Invalid response from Gemini API."
321
 
322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  def extract_and_validate_with_gemini(refined_text):
324
  """
325
  Use Gemini API to extract, validate, correct, and swap dates in 'yyyy/mm/dd' format if necessary.
 
329
  # Generate content using Gemini with the refined prompt
330
  response = model.generate_content(
331
  f"The extracted text is:\n'{refined_text}'\n\n"
332
+ f"1. Extract the 'Manufacturing Date', 'Expiration Date', and 'MRP' from the above text. "
333
+ f"Ignore unrelated data.\n"
334
+ f"2. If a date or MRP is missing or invalid, return -1 for that field.\n"
335
  f"3. If the 'Expiration Date' is earlier than the 'Manufacturing Date', swap them.\n"
336
+ f"4. Ensure both dates are in 'dd/mm/yyyy' format. If the original dates are not in this format, convert them. "
337
+ f"However, if the dates are in 'mm/yyyy' format (without a day), leave them as is and return in 'mm/yyyy' format. "
338
+ f"If the dates do not have a day, return them in 'mm/yyyy' format.\n"
339
+ f"5. MRP should be returned in the format 'INR <amount>'. If not found or invalid, return 'INR -1'.\n"
340
  f"Respond ONLY in this exact format:\n"
341
+ f"Manufacturing Date: <MFG Date>\n"
342
+ f"Expiration Date: <EXP Date>\n"
343
+ f"MRP: <MRP>"
344
  )
345
 
346
  # Validate the response and extract dates
 
349
  print(f"[DEBUG] Gemini Response: {final_dates}")
350
 
351
  # Extract the dates from the response
352
+ mfg_date_str, exp_date_str, mrp_str = parse_gemini_response(final_dates)
353
 
354
  # Process and swap if necessary
355
  if mfg_date_str != "-1" and exp_date_str != "-1":
356
+ # Handle dates with possible 'mm/yyyy' format
357
+ mfg_date = parse_date(mfg_date_str)
358
+ exp_date = parse_date(exp_date_str)
359
 
360
  # Swap if Expiration Date is earlier than Manufacturing Date
361
  swapping_statement = ""
 
366
 
367
  # Return the formatted swapped dates
368
  return swapping_statement + (
369
+ f"Manufacturing Date: {format_date(mfg_date)}, "
370
+ f"Expiration Date: {format_date(exp_date)}\n"
371
+ f"MRP: {mrp_str}"
372
  )
373
 
374
  # If either date is -1, return them as-is
 
383
  Helper function to extract Manufacturing Date and Expiration Date from the response text.
384
  """
385
  try:
386
+ # Split and extract the dates and MRP
387
  parts = response_text.split(", ")
388
  mfg_date_str = parts[0].split(": ")[1].strip()
389
  exp_date_str = parts[1].split(": ")[1].strip()
390
+ mrp_str = parts[2].split(": ")[1].strip() if len(parts) > 2 else "INR -1" # Extract MRP
391
+ return mfg_date_str, exp_date_str, mrp_str
392
  except IndexError:
393
  print("[ERROR] Failed to parse Gemini response.")
394
+ return "-1", "-1", "INR -1"
395
+
396
+ def parse_date(date_str):
397
+ """Parse date string to datetime object considering possible formats."""
398
+ if '/' in date_str: # If the date has slashes, we can parse it
399
+ parts = date_str.split('/')
400
+ if len(parts) == 3: # dd/mm/yyyy
401
+ return datetime.strptime(date_str, "%d/%m/%Y")
402
+ elif len(parts) == 2: # mm/yyyy
403
+ return datetime.strptime(date_str, "%m/%Y")
404
+ return datetime.strptime(date_str, "%d/%m/%Y") # Default fallback
405
+
406
+ def format_date(date):
407
+ """Format date back to string."""
408
+ if date.day == 1: # If day is defaulted to 1, return in mm/yyyy format
409
+ return date.strftime('%m/%Y')
410
+ return date.strftime('%d/%m/%Y')
411
+
412
 
413
  def extract_date(refined_text, date_type):
414
  """Extract the specified date type from the refined text."""
 
423
  return '-1' # Return -1 if the date is not found
424
  return '-1' # Return -1 if the date type is not in the text
425
 
426
+ def extract_details_from_validated_output(validated_output):
427
+ """Extract manufacturing date, expiration date, and MRP from the validated output."""
428
+ # Pattern to match the specified format exactly
429
+ pattern = (
430
+ r"Manufacturing Date:\s*([\d\/]+)\s*"
431
+ r"Expiration Date:\s*([\d\/]+)\s*"
432
+ r"MRP:\s*INR\s*([\d\.]+)"
433
+ )
434
+
435
+ print("[DEBUG] Validated Output:", validated_output) # Debug print for input
436
+
437
+ match = re.search(pattern, validated_output)
438
+
439
+ if match:
440
+ mfg_date = match.group(1) # Extract Manufacturing Date
441
+ exp_date = match.group(2) # Extract Expiration Date
442
+ mrp = f"INR {match.group(3)}" # Extract MRP with INR prefix
443
+
444
+ print("[DEBUG] Extracted Manufacturing Date:", mfg_date) # Debug print for extracted values
445
+ print("[DEBUG] Extracted Expiration Date:", exp_date)
446
+ print("[DEBUG] Extracted MRP:", mrp)
447
+ else:
448
+ print("[ERROR] No match found for the specified pattern.") # Debug print for errors
449
+ mfg_date, exp_date, mrp = "Not Found", "Not Found", "INR -1"
450
+
451
+ return [
452
+ ["Manufacturing Date", mfg_date],
453
+ ["Expiration Date", exp_date],
454
+ ["MRP", mrp]
455
+ ]
456
+
457
  """### **Model 3**
458
  Using Yolov8 x-large model trained till about 75 epochs
459
  and
 
462
 
463
  """
464
 
 
 
 
 
 
465
  def new_draw_bounding_boxes(image):
466
  """Draw bounding boxes around detected text in the image and display it."""
467
  # If the input is a string (file path), open the image
 
502
 
503
  return all_text_data
504
 
505
+
506
  # Initialize PaddleOCR
507
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
508
 
509
  def detect_and_ocr(image):
510
+ model = YOLO('best.pt')
511
+
512
  """Detect objects using YOLO, draw bounding boxes, and perform OCR."""
513
  # Convert input image from PIL to OpenCV format
514
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
 
572
 
573
  def handle_processing(validated_output):
574
  """Decide whether to proceed with further processing."""
575
+ # Extract the manufacturing date, expiration date, and MRP from the string
576
  try:
577
+ mfg_date_str = validated_output.split("Manufacturing Date: ")[1].split("\n")[0].strip()
578
+ exp_date_str = validated_output.split("Expiration Date: ")[1].split("\n")[0].strip()
579
+ mrp_str = validated_output.split("MRP: ")[1].strip()
580
+
581
+ # Check for invalid manufacturing date formats
582
+ if mfg_date_str == "-1":
583
+ mfg_date = -1
584
+ else:
585
+ # Attempt to parse the manufacturing date
586
+ if '/' in mfg_date_str: # If it's in dd/mm/yyyy or mm/yyyy format
587
+ mfg_date = mfg_date_str
588
+ else:
589
+ mfg_date = -1
590
+
591
+ # Check for invalid expiration date formats
592
+ if exp_date_str == "-1":
593
+ exp_date = -1
594
+ else:
595
+ # Attempt to parse the expiration date
596
+ if '/' in exp_date_str: # If it's in dd/mm/yyyy or mm/yyyy format
597
+ exp_date = exp_date_str
598
+ else:
599
+ exp_date = -1
600
 
601
+ # Check MRP validity
602
+ if mrp_str == "INR -1":
603
+ mrp = -1
604
+ else:
605
+ # Ensure MRP is in the correct format
606
+ if mrp_str.startswith("INR "):
607
+ mrp = mrp_str.split("INR ")[1].strip()
608
+ else:
609
+ mrp = -1
610
 
611
+ print("Further processing: ", mfg_date, exp_date, mrp)
612
+
613
+ except IndexError as e:
614
+ print(f"[ERROR] Failed to parse validated output: {e}")
615
  return gr.update(visible=False) # Hide button on error
616
 
617
+ # Check if all three values are invalid (-1)
618
+ if mfg_date == -1 and exp_date == -1 and mrp == -1:
619
  print("[DEBUG] Showing the 'Further Processing' button.") # Debug print
620
  return gr.update(visible=True) # Show 'Further Processing' button
621
+
622
  print("[DEBUG] Hiding the 'Further Processing' button.") # Debug print
623
+ return gr.update(visible=False) # Hide button if all values are valid
624
 
 
625
 
626
+ """# 5. Freshness backend
627
 
628
+ """
629
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
630
 
631
  class EfficientNet_FeatureExtractor(nn.Module):
632
 
 
640
  x = x.view(x.size(0), -1)
641
 
642
  return x
 
643
  # Calculating the mean and variance of the images whose features will be extracted
644
 
 
645
  transform = transforms.Compose([
646
  transforms.Resize(256),
647
  transforms.CenterCrop(224),
 
675
  print(f"Mean: {mean}")
676
  print(f"Std: {std}")
677
 
678
+
679
  # Transforming the images into the format so that they can be passes through the EfficientNet model
680
  # Define the transform for your dataset, including normalization with custom mean and std
681
  transform = transforms.Compose([
 
824
  }
825
 
826
  return result
 
827
  def classify_banana(image):
828
 
829
  model = EfficientNet_FeatureExtractor().to(device)
 
842
  distance = (distance) / 1e8
843
 
844
  return classify_banana_by_distance(distance)
 
 
 
845
  def detect_objects(image):
846
 
847
 
 
872
 
873
  return img_rgb
874
 
 
 
875
  def detect_objects_video(video_file):
 
 
876
  model = YOLO('Yash_Best.pt')
877
  # Open the video file
878
+ cap = cv2.VideoCapture(video_file)
879
+
880
+ # Check if the video was opened successfully
881
+ if not cap.isOpened():
882
+ raise Exception("Could not open video file.")
883
 
884
  # Get video properties
885
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
921
 
922
  return output_video_path
923
 
924
+
925
  """# 5. Frontend Of Brand Recognition
926
 
927
  ## Layout for Image interface
 
980
  gr.Markdown("# Flipkart Grid Robotics Track - OCR Interface")
981
 
982
  with gr.Tabs():
983
+ # Upload and Detection Tab
984
  with gr.TabItem("Upload & Detection"):
985
  with gr.Row():
 
986
  input_image = gr.Image(type="pil", label="Upload Image", height=400, width=400)
987
  output_image = gr.Image(label="Image with Bounding Boxes", height=400, width=400)
988
 
 
989
  btn = gr.Button("Analyze Image & Extract Text")
990
 
991
+ # OCR Results Tab
992
  with gr.TabItem("OCR Results"):
993
  with gr.Row():
994
  extracted_textbox = gr.Textbox(label="Extracted OCR Text", lines=5)
 
997
  with gr.Row():
998
  validated_textbox = gr.Textbox(label="Validated Output", lines=5)
999
 
1000
+ # Data table for Manufacturing Date, Expiration Date, and MRP
1001
+ with gr.Row():
1002
+ detail_table = gr.Dataframe(
1003
+ headers=["Label", "Value"],
1004
+ value=[["", ""], ["", ""], ["", ""]], # Initialize with empty values
1005
+ label="Manufacturing, Expiration Dates & MRP",
1006
+ datatype=["str", "str"],
1007
+ interactive=False,
1008
+ )
1009
+
1010
  further_button = gr.Button("Comprehensive OCR", visible=False)
1011
 
1012
  # Detect and OCR button click event
 
1016
  outputs=[output_image, extracted_textbox, refined_textbox, validated_textbox]
1017
  )
1018
 
1019
+ # Update the table when validated_textbox changes
1020
+ validated_textbox.change(
1021
+ lambda validated_output: extract_details_from_validated_output(validated_output),
1022
+ inputs=[validated_textbox],
1023
+ outputs=[detail_table]
1024
+ )
1025
+
1026
  # Further processing button click event
1027
  further_button.click(
1028
  further_processing,
 
1037
  outputs=[further_button]
1038
  )
1039
 
 
1040
  further_button.click(
1041
  lambda: gr.update(visible=False),
1042
  outputs=[validated_textbox]
 
1044
 
1045
  return ocr_interface
1046
 
1047
+ # Initialize the OCR interface
1048
  ocr_interface = create_ocr_interface()
 
1049
 
1050
+ """ 6. Front End of Fruit Index
 
 
 
1051
  """
 
1052
  def create_banana_classifier_interface():
1053
  return gr.Interface(
1054
  fn=classify_banana, # Your classification function
 
1062
  def image_freshness_interface():
1063
  return gr.Interface(
1064
  fn=detect_objects, # Your detection function
1065
+ inputs=gr.Image(type="pil", label="Upload an Image"), # Removed tool argument
1066
+ outputs=gr.Image(type="pil", label="Detected Image"),
1067
  live=True,
1068
  title="Image Freshness Detection",
1069
  description="Upload an image of fruit to detect freshness.",
 
1072
 
1073
  def video_freshness_interface():
1074
  return gr.Interface(
1075
+ fn=detect_objects_video, # Your video processing function
1076
  inputs=gr.Video(label="Upload a Video"),
1077
+ outputs=[
1078
+ gr.Video(label="Processed Video"), # Output video
1079
+ ],
1080
  title="Video Freshness Detection",
1081
  description="Upload a video of fruit to detect freshness.",
1082
  css="#component-0 { width: 300px; height: 300px; }" # Keep your CSS for fixed size
1083
+ )
1084
 
1085
  def create_fruit_interface():
1086
  with gr.Blocks() as demo:
 
1097
 
1098
  Fruit = create_fruit_interface()
1099
 
1100
+ # 6. Create a Tabbed Interface for Both Image and Video
1101
  ### Here, we combine the image and video interfaces into a tabbed structure so users can switch between them easily.
 
1102
 
1103
  def create_tabbed_interface():
1104
  return gr.TabbedInterface(
 
1112
  ### Finally, launch the Gradio interface to make it interactable.
1113
  """
1114
 
1115
+ tabbed_interface.launch(debug=False)