muhammadsalmanalfaridzi commited on
Commit
506fa35
·
verified ·
1 Parent(s): 271eeb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -14
app.py CHANGED
@@ -169,16 +169,25 @@ def detect_objects_in_video(video_path):
169
  frame_path = os.path.join(temp_frames_dir, f"frame_{frame_index}.jpg")
170
  cv2.imwrite(frame_path, frame)
171
 
172
- # Deteksi objek pada frame menggunakan YOLO
173
  predictions = yolo_model.predict(frame_path, confidence=60, overlap=80).json()
174
 
175
- # Hitung jumlah objek per kelas dari YOLO
176
- yolo_class_count = {}
177
  for prediction in predictions['predictions']:
178
  class_name = prediction['class']
179
- yolo_class_count[class_name] = yolo_class_count.get(class_name, 0) + 1
180
-
181
- # Gambar bounding box dan label pada frame
 
 
 
 
 
 
 
 
 
182
  for prediction in predictions['predictions']:
183
  x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
184
  class_name = prediction['class']
@@ -186,14 +195,6 @@ def detect_objects_in_video(video_path):
186
  cv2.rectangle(frame, (int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)), color, 2)
187
  cv2.putText(frame, class_name, (int(x - w/2), int(y - h/2 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
188
 
189
- # Menyusun teks dinamis untuk jumlah objek yang terdeteksi
190
- result_text = "Detected Objects:\n"
191
- for class_name, count in yolo_class_count.items():
192
- result_text += f"{class_name}: {count}\n"
193
-
194
- # Menambahkan informasi jumlah objek terdeteksi pada video
195
- cv2.putText(frame, result_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2)
196
-
197
  # Tambahkan frame ke video keluaran
198
  output_video.write(frame)
199
  frame_index += 1
 
169
  frame_path = os.path.join(temp_frames_dir, f"frame_{frame_index}.jpg")
170
  cv2.imwrite(frame_path, frame)
171
 
172
+ # Deteksi objek pada frame
173
  predictions = yolo_model.predict(frame_path, confidence=60, overlap=80).json()
174
 
175
+ # Hitung objek per kelas
176
+ class_count = {}
177
  for prediction in predictions['predictions']:
178
  class_name = prediction['class']
179
+ class_count[class_name] = class_count.get(class_name, 0) + 1
180
+
181
+ # Menyusun teks untuk jumlah objek yang terdeteksi (vertikal)
182
+ text_offset = 30 # Jarak antara setiap baris teks
183
+ y_position = 30 # Posisi Y awal untuk menampilkan teks
184
+ for class_name, count in class_count.items():
185
+ # Menulis teks untuk setiap kelas
186
+ cv2.putText(frame, f"{class_name}: {count}", (10, y_position),
187
+ cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 2, cv2.LINE_AA)
188
+ y_position += text_offset # Geser posisi teks untuk baris berikutnya
189
+
190
+ # Gambar bounding box pada objek
191
  for prediction in predictions['predictions']:
192
  x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
193
  class_name = prediction['class']
 
195
  cv2.rectangle(frame, (int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)), color, 2)
196
  cv2.putText(frame, class_name, (int(x - w/2), int(y - h/2 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
197
 
 
 
 
 
 
 
 
 
198
  # Tambahkan frame ke video keluaran
199
  output_video.write(frame)
200
  frame_index += 1