KenanKeeqi commited on
Commit
c4b0dc0
·
verified ·
1 Parent(s): bd8fd9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -9
app.py CHANGED
@@ -22,6 +22,27 @@ model.overrides['agnostic_nms'] = False # NMS class-agnostic
22
  model.overrides['max_det'] = 1000 # Maksimum deteksi per frame
23
  logger.info("Model berhasil dimuat")
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def process_video(video_path):
26
  try:
27
  logger.info("Memulai pemrosesan video: %s", video_path)
@@ -29,8 +50,12 @@ def process_video(video_path):
29
  # Buka video
30
  cap = cv2.VideoCapture(video_path)
31
  if not cap.isOpened():
32
- logger.error("Gagal membuka video")
33
- return None, "Error: Tidak dapat membuka video."
 
 
 
 
34
 
35
  # Dapatkan FPS
36
  fps = cap.get(cv2.CAP_PROP_FPS)
@@ -122,13 +147,23 @@ def process_video(video_path):
122
  out.release()
123
 
124
  # Setup antarmuka Gradio
125
- iface = gr.Interface(
126
- fn=process_video,
127
- inputs=gr.Video(label="Unggah Video (maks. 30 detik)"),
128
- outputs=[gr.Video(label="Hasil Deteksi Helm Keselamatan"), gr.Textbox(label="Status")],
129
- title="Deteksi Helm Keselamatan",
130
- description="Unggah video pendek (maks. 30 detik, resolusi rendah) untuk mendeteksi helm keselamatan menggunakan YOLOv8 (keremberke/yolov8m-hard-hat-detection)."
131
- )
 
 
 
 
 
 
 
 
 
 
132
 
133
  # Luncurkan aplikasi
134
  if __name__ == "__main__":
 
22
  model.overrides['max_det'] = 1000 # Maksimum deteksi per frame
23
  logger.info("Model berhasil dimuat")
24
 
25
+ def convert_video_to_mp4(input_path):
26
+ """Konversi video input ke MP4 jika format tidak didukung."""
27
+ temp_mp4_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
28
+ ffmpeg_command = [
29
+ "ffmpeg",
30
+ "-y", # Overwrite jika ada
31
+ "-i", input_path,
32
+ "-c:v", "libx264",
33
+ "-preset", "veryfast",
34
+ "-pix_fmt", "yuv420p",
35
+ temp_mp4_path
36
+ ]
37
+ result = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
38
+ if result.returncode == 0 and os.path.exists(temp_mp4_path) and os.path.getsize(temp_mp4_path) > 0:
39
+ logger.info("Video dikonversi ke MP4: %s", temp_mp4_path)
40
+ os.remove(input_path) # Hapus file asli setelah konversi
41
+ return temp_mp4_path
42
+ else:
43
+ logger.error("Konversi video gagal: %s", result.stderr)
44
+ raise Exception("Konversi video ke MP4 gagal.")
45
+
46
  def process_video(video_path):
47
  try:
48
  logger.info("Memulai pemrosesan video: %s", video_path)
 
50
  # Buka video
51
  cap = cv2.VideoCapture(video_path)
52
  if not cap.isOpened():
53
+ logger.warning("Format video tidak didukung, mencoba konversi ke MP4...")
54
+ video_path = convert_video_to_mp4(video_path)
55
+ cap = cv2.VideoCapture(video_path)
56
+ if not cap.isOpened():
57
+ logger.error("Gagal membuka video setelah konversi")
58
+ return None, "Error: Video tidak dapat dibuka setelah konversi."
59
 
60
  # Dapatkan FPS
61
  fps = cap.get(cv2.CAP_PROP_FPS)
 
147
  out.release()
148
 
149
  # Setup antarmuka Gradio
150
+ with gr.Blocks() as iface:
151
+ gr.Markdown("## Deteksi Helm Keselamatan")
152
+ with gr.Row():
153
+ with gr.Column(scale=1):
154
+ video_input = gr.Video(label="Unggah Video (maks. 30 detik)", interactive=True)
155
+ detect_vid_btn = gr.Button("Detect Objects")
156
+ video_examples = gr.Examples(
157
+ examples=[["./examples/video.mp4"]], # Sesuaikan path dengan file contoh di repositori
158
+ inputs=[video_input],
159
+ label="Contoh Video",
160
+ )
161
+ with gr.Column(scale=1):
162
+ video_output = gr.Video(label="Hasil Deteksi Helm Keselamatan")
163
+ status_text = gr.Textbox(label="Status", interactive=False)
164
+ detect_vid_btn.click(fn=process_video, inputs=video_input, outputs=[video_output, status_text])
165
+
166
+ iface.launch()
167
 
168
  # Luncurkan aplikasi
169
  if __name__ == "__main__":