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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -169,10 +169,16 @@ 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
173
  predictions = yolo_model.predict(frame_path, confidence=60, overlap=80).json()
174
 
175
- # Gambar bounding box pada frame
 
 
 
 
 
 
176
  for prediction in predictions['predictions']:
177
  x, y, w, h = prediction['x'], prediction['y'], prediction['width'], prediction['height']
178
  class_name = prediction['class']
@@ -180,6 +186,14 @@ def detect_objects_in_video(video_path):
180
  cv2.rectangle(frame, (int(x - w/2), int(y - h/2)), (int(x + w/2), int(y + h/2)), color, 2)
181
  cv2.putText(frame, class_name, (int(x - w/2), int(y - h/2 - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
182
 
 
 
 
 
 
 
 
 
183
  # Tambahkan frame ke video keluaran
184
  output_video.write(frame)
185
  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 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
  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