muhammadsalmanalfaridzi commited on
Commit
8741a65
·
verified ·
1 Parent(s): 15df14e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -74,8 +74,13 @@ def detect_combined(image):
74
  try:
75
  # ===== YOLO Detection =====
76
  yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
77
- # Get YOLO boxes as (x_center, y_center, width, height)
78
- nestle_boxes = [(pred['x'], pred['y'], pred['width'], pred['height']) for pred in yolo_pred['predictions']]
 
 
 
 
 
79
 
80
  # ===== CountGD Detection =====
81
  url = "https://api.landing.ai/v1/tools/text-to-object-detection"
@@ -99,7 +104,7 @@ def detect_combined(image):
99
  # Prioritaskan deteksi YOLO: hapus jika overlap dengan YOLO (threshold 0.5)
100
  if is_overlap(countgd_box, nestle_boxes, threshold=0.5):
101
  continue
102
- # Hindari duplikasi antar CountGD: jika IoU dengan deteksi lain > 0.4, lewati
103
  duplicate = False
104
  for existing_box in competitor_boxes:
105
  if iou(countgd_box, existing_box) > 0.4:
@@ -128,8 +133,14 @@ def detect_combined(image):
128
  output_path = "/tmp/combined_output.jpg"
129
  cv2.imwrite(output_path, img)
130
 
131
- # Buat result text untuk counting object
132
- result_text = f"Total Produk Nestlé: {len(nestle_boxes)}\nTotal Unclassified Products: {len(competitor_boxes)}"
 
 
 
 
 
 
133
  return output_path, result_text
134
 
135
  except Exception as e:
@@ -144,17 +155,13 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", ne
144
  gr.Markdown("""<div style="text-align: center;"><h1>NESTLE - STOCK COUNTING</h1></div>""")
145
 
146
  with gr.Row():
147
- with gr.Column():
148
- input_image = gr.Image(type="pil", label="Input Image")
149
  with gr.Row():
150
- with gr.Column():
151
- detect_image_button = gr.Button("Detect Image")
152
  with gr.Row():
153
- with gr.Column():
154
- output_image = gr.Image(label="Detect Object")
155
  with gr.Row():
156
- with gr.Column():
157
- output_text = gr.Textbox(label="Counting Object")
158
 
159
  detect_image_button.click(fn=detect_combined, inputs=input_image, outputs=[output_image, output_text])
160
 
 
74
  try:
75
  # ===== YOLO Detection =====
76
  yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
77
+ # Hitung bounding box dan count per class untuk produk Nestlé
78
+ nestle_boxes = []
79
+ nestle_class_count = {}
80
+ for pred in yolo_pred['predictions']:
81
+ class_name = pred['class']
82
+ nestle_class_count[class_name] = nestle_class_count.get(class_name, 0) + 1
83
+ nestle_boxes.append((pred['x'], pred['y'], pred['width'], pred['height']))
84
 
85
  # ===== CountGD Detection =====
86
  url = "https://api.landing.ai/v1/tools/text-to-object-detection"
 
104
  # Prioritaskan deteksi YOLO: hapus jika overlap dengan YOLO (threshold 0.5)
105
  if is_overlap(countgd_box, nestle_boxes, threshold=0.5):
106
  continue
107
+ # Hindari duplikasi antar deteksi CountGD: jika IoU dengan deteksi lain > 0.4, lewati
108
  duplicate = False
109
  for existing_box in competitor_boxes:
110
  if iou(countgd_box, existing_box) > 0.4:
 
133
  output_path = "/tmp/combined_output.jpg"
134
  cv2.imwrite(output_path, img)
135
 
136
+ # Buat result text untuk count produk Nestlé per class dan total keseluruhan
137
+ result_text = "Produk Nestlé per Class:\n"
138
+ for class_name, count in nestle_class_count.items():
139
+ result_text += f"{class_name}: {count}\n"
140
+ total_nestle = sum(nestle_class_count.values())
141
+ result_text += f"\nTotal Produk Nestlé: {total_nestle}\n"
142
+ result_text += f"Total Unclassified Products: {len(competitor_boxes)}"
143
+
144
  return output_path, result_text
145
 
146
  except Exception as e:
 
155
  gr.Markdown("""<div style="text-align: center;"><h1>NESTLE - STOCK COUNTING</h1></div>""")
156
 
157
  with gr.Row():
158
+ input_image = gr.Image(type="pil", label="Input Image")
 
159
  with gr.Row():
160
+ detect_image_button = gr.Button("Detect Image")
 
161
  with gr.Row():
162
+ output_image = gr.Image(label="Detect Object")
 
163
  with gr.Row():
164
+ output_text = gr.Textbox(label="Counting Object")
 
165
 
166
  detect_image_button.click(fn=detect_combined, inputs=input_image, outputs=[output_image, output_text])
167