muhammadsalmanalfaridzi commited on
Commit
2c2ffd4
·
verified ·
1 Parent(s): 667c435

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -28,16 +28,22 @@ yolo_model = project.version(model_version).model
28
 
29
  # ========== Fungsi Deteksi Kombinasi ==========
30
  def detect_combined(image):
31
- # Simpan gambar input ke file sementara
 
 
 
 
 
 
32
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
33
- image.save(temp_file, format="JPEG")
34
  temp_path = temp_file.name
35
 
36
  try:
37
- # ========== [1] YOLO: Deteksi Produk Nestlé (Per Class) ==========
38
  yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
39
 
40
- # Hitung per class Nestlé dan simpan bounding box (format: (x_center, y_center, width, height))
41
  nestle_class_count = {}
42
  nestle_boxes = []
43
  for pred in yolo_pred['predictions']:
@@ -98,7 +104,7 @@ def detect_combined(image):
98
  cv2.putText(img, pred['class'], (int(x - w/2), int(y - h/2 - 10)),
99
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
100
 
101
- # Gambar bounding box untuk kompetitor (Merah) dengan label 'unclassified' jika sesuai
102
  for comp in competitor_boxes:
103
  x1, y1, x2, y2 = comp['box']
104
  unclassified_classes = ["cans"]
@@ -117,6 +123,7 @@ def detect_combined(image):
117
  finally:
118
  os.remove(temp_path)
119
 
 
120
  def is_overlap(box1, boxes2, threshold=0.3):
121
  """
122
  Fungsi untuk mendeteksi overlap bounding box.
 
28
 
29
  # ========== Fungsi Deteksi Kombinasi ==========
30
  def detect_combined(image):
31
+ # Konversi gambar ke format numpy array
32
+ image_np = np.array(image)
33
+
34
+ # Resize gambar ke 640x640
35
+ image_resized = cv2.resize(image_np, (640, 640))
36
+
37
+ # Simpan gambar input yang sudah di-resize ke file sementara
38
  with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
39
+ cv2.imwrite(temp_file.name, image_resized)
40
  temp_path = temp_file.name
41
 
42
  try:
43
+ # ========== [1] YOLO: Deteksi Produk Nestlé ==========
44
  yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
45
 
46
+ # Hitung per kelas dan simpan bounding box (format: (x_center, y_center, width, height))
47
  nestle_class_count = {}
48
  nestle_boxes = []
49
  for pred in yolo_pred['predictions']:
 
104
  cv2.putText(img, pred['class'], (int(x - w/2), int(y - h/2 - 10)),
105
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
106
 
107
+ # Gambar bounding box untuk kompetitor (Merah)
108
  for comp in competitor_boxes:
109
  x1, y1, x2, y2 = comp['box']
110
  unclassified_classes = ["cans"]
 
123
  finally:
124
  os.remove(temp_path)
125
 
126
+
127
  def is_overlap(box1, boxes2, threshold=0.3):
128
  """
129
  Fungsi untuk mendeteksi overlap bounding box.