muhammadsalmanalfaridzi commited on
Commit
15df14e
·
verified ·
1 Parent(s): 540d913

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -15
app.py CHANGED
@@ -28,7 +28,9 @@ yolo_model = project.version(model_version).model
28
  # ========== Fungsi untuk Mengecek Overlap antara YOLO dan CountGD ==========
29
  def is_overlap(box1, boxes2, threshold=0.5):
30
  """
31
- Mengecek apakah box1 overlap dengan salah satu box di boxes2 berdasarkan IoU.
 
 
32
  """
33
  x1_min, y1_min, x1_max, y1_max = box1
34
  for b2 in boxes2:
@@ -51,6 +53,7 @@ def is_overlap(box1, boxes2, threshold=0.5):
51
  def iou(boxA, boxB):
52
  """
53
  Menghitung Intersection over Union (IoU) antara dua bounding box.
 
54
  """
55
  xA = max(boxA[0], boxB[0])
56
  yA = max(boxA[1], boxB[1])
@@ -59,11 +62,11 @@ def iou(boxA, boxB):
59
  interArea = max(0, xB - xA) * max(0, yB - yA)
60
  boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
61
  boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
62
- iou_val = interArea / float(boxAArea + boxBArea - interArea) if (boxAArea + boxBArea - interArea) > 0 else 0
63
- return iou_val
64
 
65
  # ========== Fungsi Deteksi Kombinasi ==========
66
  def detect_combined(image):
 
67
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
68
  image.save(temp_file, format="JPEG")
69
  temp_path = temp_file.name
@@ -71,6 +74,7 @@ def detect_combined(image):
71
  try:
72
  # ===== YOLO Detection =====
73
  yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
 
74
  nestle_boxes = [(pred['x'], pred['y'], pred['width'], pred['height']) for pred in yolo_pred['predictions']]
75
 
76
  # ===== CountGD Detection =====
@@ -92,18 +96,21 @@ def detect_combined(image):
92
  if 'bounding_box' in obj:
93
  x1, y1, x2, y2 = obj['bounding_box']
94
  countgd_box = (x1, y1, x2, y2)
95
-
96
- if not is_overlap(countgd_box, nestle_boxes, threshold=0.5):
97
- duplicate = False
98
- for existing_box in competitor_boxes:
99
- if iou(countgd_box, existing_box) > 0.4:
100
- duplicate = True
101
- break
102
- if not duplicate:
103
- competitor_boxes.append(countgd_box)
 
 
104
 
105
  # ===== Visualisasi =====
106
  img = cv2.imread(temp_path)
 
107
  for pred in yolo_pred['predictions']:
108
  x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
109
  pt1 = (int(x - w/2), int(y - h/2))
@@ -111,7 +118,7 @@ def detect_combined(image):
111
  cv2.rectangle(img, pt1, pt2, (0, 255, 0), 2)
112
  cv2.putText(img, pred['class'], (pt1[0], pt1[1]-10),
113
  cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0), 3)
114
-
115
  for box in competitor_boxes:
116
  x1, y1, x2, y2 = box
117
  cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
@@ -120,7 +127,10 @@ def detect_combined(image):
120
 
121
  output_path = "/tmp/combined_output.jpg"
122
  cv2.imwrite(output_path, img)
123
- return output_path
 
 
 
124
 
125
  except Exception as e:
126
  return temp_path, f"Error: {str(e)}"
@@ -136,9 +146,16 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", ne
136
  with gr.Row():
137
  with gr.Column():
138
  input_image = gr.Image(type="pil", label="Input Image")
 
 
139
  detect_image_button = gr.Button("Detect Image")
 
 
140
  output_image = gr.Image(label="Detect Object")
 
 
141
  output_text = gr.Textbox(label="Counting Object")
142
- detect_image_button.click(fn=detect_combined, inputs=input_image, outputs=[output_image, output_text])
 
143
 
144
  iface.launch()
 
28
  # ========== Fungsi untuk Mengecek Overlap antara YOLO dan CountGD ==========
29
  def is_overlap(box1, boxes2, threshold=0.5):
30
  """
31
+ Mengecek apakah box1 (format: (x_min, y_min, x_max, y_max)) overlap
32
+ dengan salah satu box di boxes2 (format: (x_center, y_center, width, height))
33
+ berdasarkan IoU, menggunakan threshold yang ditetapkan.
34
  """
35
  x1_min, y1_min, x1_max, y1_max = box1
36
  for b2 in boxes2:
 
53
  def iou(boxA, boxB):
54
  """
55
  Menghitung Intersection over Union (IoU) antara dua bounding box.
56
+ Masing-masing box dalam format (x_min, y_min, x_max, y_max).
57
  """
58
  xA = max(boxA[0], boxB[0])
59
  yA = max(boxA[1], boxB[1])
 
62
  interArea = max(0, xB - xA) * max(0, yB - yA)
63
  boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
64
  boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
65
+ return interArea / float(boxAArea + boxBArea - interArea) if (boxAArea + boxBArea - interArea) > 0 else 0
 
66
 
67
  # ========== Fungsi Deteksi Kombinasi ==========
68
  def detect_combined(image):
69
+ # Simpan image ke file sementara
70
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
71
  image.save(temp_file, format="JPEG")
72
  temp_path = temp_file.name
 
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 =====
 
96
  if 'bounding_box' in obj:
97
  x1, y1, x2, y2 = obj['bounding_box']
98
  countgd_box = (x1, y1, x2, y2)
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:
106
+ duplicate = True
107
+ break
108
+ if not duplicate:
109
+ competitor_boxes.append(countgd_box)
110
 
111
  # ===== Visualisasi =====
112
  img = cv2.imread(temp_path)
113
+ # Gambar bounding box YOLO (hijau)
114
  for pred in yolo_pred['predictions']:
115
  x, y, w, h = pred['x'], pred['y'], pred['width'], pred['height']
116
  pt1 = (int(x - w/2), int(y - h/2))
 
118
  cv2.rectangle(img, pt1, pt2, (0, 255, 0), 2)
119
  cv2.putText(img, pred['class'], (pt1[0], pt1[1]-10),
120
  cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0), 3)
121
+ # Gambar bounding box CountGD (merah)
122
  for box in competitor_boxes:
123
  x1, y1, x2, y2 = box
124
  cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
 
127
 
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:
136
  return temp_path, f"Error: {str(e)}"
 
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
 
161
  iface.launch()