lokesh341 commited on
Commit
0e161fe
·
verified ·
1 Parent(s): 56492d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from ultralyticsplus import YOLO
3
  from PIL import Image, ImageDraw
4
  import io
5
  from fpdf import FPDF
@@ -7,9 +7,8 @@ import tempfile
7
  import cv2
8
  import os
9
 
10
- # Load the pre-trained model for shelf product and empty spot detection
11
- model = YOLO('foduucom/product-detection-in-shelf-yolov8')
12
-
13
  model.overrides['conf'] = 0.25 # Confidence threshold
14
  model.overrides['iou'] = 0.45 # IoU threshold
15
  model.overrides['agnostic_nms'] = False
@@ -25,7 +24,6 @@ def process_input(input_file):
25
  cap.release()
26
  if not ret:
27
  raise ValueError("Could not read video frame.")
28
- # Save temp frame
29
  temp_frame_path = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False).name
30
  cv2.imwrite(temp_frame_path, frame)
31
  process_img_path = temp_frame_path
@@ -37,24 +35,25 @@ def process_input(input_file):
37
  boxes = results[0].boxes
38
  class_names = results[0].names
39
 
40
- # Count present bottles (products) and empty spots
41
  num_present = 0
42
- num_empty = 0
43
  empty_bboxes = []
44
  for box in boxes:
45
  cls_id = int(box.cls.item())
46
  cls_name = class_names[cls_id]
47
- if cls_name == 'Magical Products':
48
  num_present += 1
49
- elif cls_name == 'Empty Shelves':
50
- num_empty += 1
51
- empty_bboxes.append(box.xyxy[0].cpu().numpy()) # [x1, y1, x2, y2]
52
 
53
- # Load image for annotation
54
  img = Image.open(process_img_path)
55
  draw = ImageDraw.Draw(img)
56
- for bbox in empty_bboxes:
57
- draw.rectangle(((bbox[0], bbox[1]), (bbox[2], bbox[3])), outline="red", width=3)
 
 
 
58
 
59
  # Generate PDF
60
  pdf = FPDF()
@@ -62,31 +61,33 @@ def process_input(input_file):
62
  pdf.set_font("Arial", size=12)
63
  pdf.cell(200, 10, txt="Wine Shop Inventory Report", ln=1, align='C')
64
  pdf.ln(5)
65
- pdf.cell(200, 10, txt=f"Number of bottles present (full/stock): {num_present}", ln=1)
66
- pdf.cell(200, 10, txt=f"Number of empty spots (need to fill): {num_empty}", ln=1)
67
  pdf.ln(10)
68
 
69
- # Add annotated full image (scaled for UX)
70
  with tempfile.NamedTemporaryFile(suffix='.png') as tmp_annotated:
71
  img.save(tmp_annotated.name)
72
- pdf.image(tmp_annotated.name, x=10, y=pdf.get_y(), w=180) # Full width for readability
73
  pdf.add_page()
74
- pdf.cell(200, 10, txt="Screenshots of Empty Spots", ln=1, align='C')
75
  pdf.ln(5)
76
  y_pos = pdf.get_y()
77
- for i, bbox in enumerate(empty_bboxes):
78
- cropped_img = img.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
79
- with tempfile.NamedTemporaryFile(suffix='.png') as tmp_crop:
80
- cropped_img.save(tmp_crop.name)
81
- pdf.image(tmp_crop.name, x=10, y=y_pos, w=90) # Medium size for clear view
82
- y_pos += 100 # Space between images
83
- pdf.cell(200, 10, txt=f"Empty Spot {i+1} (Location: x1={int(bbox[0])}, y1={int(bbox[1])}, x2={int(bbox[2])}, y2={int(bbox[3])})", ln=1)
84
- pdf.ln(5)
85
- if y_pos > 200:
86
- pdf.add_page()
87
- y_pos = 10
 
 
88
 
89
- # Output PDF as bytes
90
  pdf_bytes = io.BytesIO()
91
  pdf.output(pdf_bytes)
92
  pdf_bytes.seek(0)
 
1
  import gradio as gr
2
+ from ultralytics import YOLO
3
  from PIL import Image, ImageDraw
4
  import io
5
  from fpdf import FPDF
 
7
  import cv2
8
  import os
9
 
10
+ # Load the pre-trained YOLOv8n model
11
+ model = YOLO('yolov8n.pt')
 
12
  model.overrides['conf'] = 0.25 # Confidence threshold
13
  model.overrides['iou'] = 0.45 # IoU threshold
14
  model.overrides['agnostic_nms'] = False
 
24
  cap.release()
25
  if not ret:
26
  raise ValueError("Could not read video frame.")
 
27
  temp_frame_path = tempfile.NamedTemporaryFile(suffix='.jpg', delete=False).name
28
  cv2.imwrite(temp_frame_path, frame)
29
  process_img_path = temp_frame_path
 
35
  boxes = results[0].boxes
36
  class_names = results[0].names
37
 
38
+ # Count bottles (assuming class 'bottle' from COCO dataset)
39
  num_present = 0
40
+ num_empty = 0 # Note: yolov8n.pt doesn't detect empty shelves
41
  empty_bboxes = []
42
  for box in boxes:
43
  cls_id = int(box.cls.item())
44
  cls_name = class_names[cls_id]
45
+ if cls_name == 'bottle':
46
  num_present += 1
47
+ # Empty spots not detected; you can add logic to infer empty spaces later
 
 
48
 
49
+ # Load image for annotation (highlight bottles instead of empty spots for now)
50
  img = Image.open(process_img_path)
51
  draw = ImageDraw.Draw(img)
52
+ for box in boxes:
53
+ cls_id = int(box.cls.item())
54
+ if class_names[cls_id] == 'bottle':
55
+ bbox = box.xyxy[0].cpu().numpy()
56
+ draw.rectangle(((bbox[0], bbox[1]), (bbox[2], bbox[3])), outline="green", width=3)
57
 
58
  # Generate PDF
59
  pdf = FPDF()
 
61
  pdf.set_font("Arial", size=12)
62
  pdf.cell(200, 10, txt="Wine Shop Inventory Report", ln=1, align='C')
63
  pdf.ln(5)
64
+ pdf.cell(200, 10, txt=f"Number of bottles detected: {num_present}", ln=1)
65
+ pdf.cell(200, 10, txt="Note: Empty spot detection not supported in this model.", ln=1)
66
  pdf.ln(10)
67
 
68
+ # Add annotated full image
69
  with tempfile.NamedTemporaryFile(suffix='.png') as tmp_annotated:
70
  img.save(tmp_annotated.name)
71
+ pdf.image(tmp_annotated.name, x=10, y=pdf.get_y(), w=180)
72
  pdf.add_page()
73
+ pdf.cell(200, 10, txt="Detected Bottles", ln=1, align='C')
74
  pdf.ln(5)
75
  y_pos = pdf.get_y()
76
+ for i, box in enumerate(boxes):
77
+ if class_names[int(box.cls.item())] == 'bottle':
78
+ bbox = box.xyxy[0].cpu().numpy()
79
+ cropped_img = img.crop((bbox[0], bbox[1], bbox[2], bbox[3]))
80
+ with tempfile.NamedTemporaryFile(suffix='.png') as tmp_crop:
81
+ cropped_img.save(tmp_crop.name)
82
+ pdf.image(tmp_crop.name, x=10, y=y_pos, w=90)
83
+ y_pos += 100
84
+ pdf.cell(200, 10, txt=f"Bottle {i+1} (Location: x1={int(bbox[0])}, y1={int(bbox[1])}, x2={int(bbox[2])}, y2={int(bbox[3])})", ln=1)
85
+ pdf.ln(5)
86
+ if y_pos > 200:
87
+ pdf.add_page()
88
+ y_pos = 10
89
 
90
+ # Output PDF
91
  pdf_bytes = io.BytesIO()
92
  pdf.output(pdf_bytes)
93
  pdf_bytes.seek(0)