mlbench123 commited on
Commit
deaa6fa
·
verified ·
1 Parent(s): f213121

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from ultralytics import YOLO
3
+ import gradio as gr
4
+ import tempfile
5
+ import os
6
+
7
+ # Load trained model
8
+ model = YOLO("best.pt")
9
+
10
+ def process_video(input_video):
11
+ cap = cv2.VideoCapture(input_video)
12
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
13
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
14
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
15
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
16
+
17
+ tmp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
18
+ output_path = tmp_out.name
19
+ tmp_out.close()
20
+
21
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
22
+
23
+ while True:
24
+ ret, frame = cap.read()
25
+ if not ret:
26
+ break
27
+
28
+ results = model(frame)[0]
29
+ for box in results.boxes:
30
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
31
+ conf = box.conf[0]
32
+ if conf < 0.5:
33
+ continue
34
+ cls = int(box.cls[0])
35
+ label = f"Cleaner {conf:.2f}"
36
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 200, 0), 2)
37
+ cv2.putText(frame, label, (x1, y1 - 10),
38
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 200, 0), 2)
39
+
40
+ out.write(frame)
41
+
42
+ cap.release()
43
+ out.release()
44
+ cv2.destroyAllWindows()
45
+
46
+ return output_path
47
+
48
+ with gr.Blocks(css=".gradio-container {background-color: #0f1f3d; color: #cce0ff;}") as demo:
49
+ gr.Markdown("## Video Object Detection (Bluish Tone)")
50
+ with gr.Row():
51
+ with gr.Column():
52
+ inp = gr.Video(label="Upload Video")
53
+ btn = gr.Button("Process Video", variant="primary")
54
+ with gr.Column():
55
+ out = gr.Video(label="Output Video")
56
+ btn.click(fn=process_video, inputs=inp, outputs=out)
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()