muhammadsalmanalfaridzi commited on
Commit
82ba4b5
·
verified ·
1 Parent(s): 70e9e09

Rename app-complete-mantab.py to app.py

Browse files
Files changed (1) hide show
  1. app-complete-mantab.py → app.py +19 -15
app-complete-mantab.py → app.py RENAMED
@@ -23,7 +23,7 @@ model_version = int(os.getenv("ROBOFLOW_MODEL_VERSION"))
23
 
24
  # DINO-X Config
25
  DINOX_API_KEY = os.getenv("DINO_X_API_KEY")
26
- DINOX_PROMPT = "beverage . bottle . cans" # Customize sesuai produk kompetitor : food . drink
27
 
28
  # Inisialisasi Model
29
  rf = Roboflow(api_key=rf_api_key)
@@ -41,7 +41,7 @@ def detect_combined(image):
41
 
42
  try:
43
  # ========== [1] YOLO: Deteksi Produk Nestlé (Per Class) ==========
44
- yolo_pred = yolo_model.predict(temp_path, confidence=60, overlap=80).json()
45
 
46
  # Hitung per class Nestlé
47
  nestle_class_count = {}
@@ -85,15 +85,13 @@ def detect_combined(image):
85
  result_text = "Product Nestle\n\n"
86
  for class_name, count in nestle_class_count.items():
87
  result_text += f"{class_name}: {count}\n"
88
- result_text += f"\nTotal Product Nestle: {total_nestle}\n\n"
89
 
90
- result_text += "Competitor Products\n\n"
91
  if competitor_class_count:
92
- for class_name, count in competitor_class_count.items():
93
- result_text += f"{class_name}: {count}\n"
94
  else:
95
- result_text += "No competitors detected\n"
96
- result_text += f"\nTotal Competitor: {total_competitor}"
97
 
98
  # ========== [4] Visualisasi ==========
99
  img = cv2.imread(temp_path)
@@ -105,13 +103,20 @@ def detect_combined(image):
105
  cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)),
106
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
107
 
108
- # Kompetitor (Merah)
109
  for comp in competitor_boxes:
110
  x1, y1, x2, y2 = comp['box']
111
- cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0,0,255), 2)
112
- cv2.putText(img, f"{comp['class']} {comp['confidence']:.2f}",
113
- (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
114
-
 
 
 
 
 
 
 
115
  output_path = "/tmp/combined_output.jpg"
116
  cv2.imwrite(output_path, img)
117
 
@@ -186,7 +191,7 @@ def detect_objects_in_video(video_path):
186
  cv2.imwrite(frame_path, frame)
187
 
188
  # Process predictions for frame
189
- predictions = yolo_model.predict(frame_path, confidence=60, overlap=80).json()
190
 
191
  # Update class count for this frame
192
  frame_class_count = {}
@@ -210,7 +215,6 @@ def detect_objects_in_video(video_path):
210
  nestle_total = sum(all_class_count.values())
211
 
212
  # Create a vertical layout for counts (dynamically updated)
213
- count_text = "Cumulative Object Counts\n"
214
  for class_name, count in all_class_count.items():
215
  count_text += f"{class_name}: {count}\n"
216
  count_text += f"\nTotal Product Nestlé: {nestle_total}"
 
23
 
24
  # DINO-X Config
25
  DINOX_API_KEY = os.getenv("DINO_X_API_KEY")
26
+ DINOX_PROMPT = "beverage . bottle . cans . boxed milk . milk" # Customize sesuai produk kompetitor : food . drink
27
 
28
  # Inisialisasi Model
29
  rf = Roboflow(api_key=rf_api_key)
 
41
 
42
  try:
43
  # ========== [1] YOLO: Deteksi Produk Nestlé (Per Class) ==========
44
+ yolo_pred = yolo_model.predict(temp_path, confidence=50, overlap=80).json()
45
 
46
  # Hitung per class Nestlé
47
  nestle_class_count = {}
 
85
  result_text = "Product Nestle\n\n"
86
  for class_name, count in nestle_class_count.items():
87
  result_text += f"{class_name}: {count}\n"
88
+ result_text += f"\nTotal Products Nestle: {total_nestle}\n\n"
89
 
90
+ #result_text += "Competitor Products\n\n"
91
  if competitor_class_count:
92
+ result_text += f"Total Unclassified Products: {total_competitor}\n" # Hanya total, tidak per kelas
 
93
  else:
94
+ result_text += "No Unclassified Products detected\n"
 
95
 
96
  # ========== [4] Visualisasi ==========
97
  img = cv2.imread(temp_path)
 
103
  cv2.putText(img, pred['class'], (int(x-w/2), int(y-h/2-10)),
104
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
105
 
106
+ # Kompetitor (Merah) dengan nama 'unclassified'
107
  for comp in competitor_boxes:
108
  x1, y1, x2, y2 = comp['box']
109
+
110
+ # Define a list of target classes to rename
111
+ unclassified_classes = ["beverage", "cans", "bottle", "boxed milk", "milk"]
112
+
113
+ # Normalize the class name to be case-insensitive and check if it's in the unclassified list
114
+ display_name = "unclassified" if any(class_name in comp['class'].lower() for class_name in unclassified_classes) else comp['class']
115
+
116
+ cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2)
117
+ cv2.putText(img, f"{display_name} {comp['confidence']:.2f}",
118
+ (int(x1), int(y1-10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
119
+
120
  output_path = "/tmp/combined_output.jpg"
121
  cv2.imwrite(output_path, img)
122
 
 
191
  cv2.imwrite(frame_path, frame)
192
 
193
  # Process predictions for frame
194
+ predictions = yolo_model.predict(frame_path, confidence=50, overlap=80).json()
195
 
196
  # Update class count for this frame
197
  frame_class_count = {}
 
215
  nestle_total = sum(all_class_count.values())
216
 
217
  # Create a vertical layout for counts (dynamically updated)
 
218
  for class_name, count in all_class_count.items():
219
  count_text += f"{class_name}: {count}\n"
220
  count_text += f"\nTotal Product Nestlé: {nestle_total}"