farwew commited on
Commit
89dd01d
·
verified ·
1 Parent(s): d7a3153

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+
5
+ # Load model
6
+ model = YOLO("best.pt") # Ganti dengan path model kamu
7
+
8
+ def predict_image(img):
9
+ results = model(img)
10
+ annotated_frame = results[0].plot()
11
+ return annotated_frame
12
+
13
+ def predict_video(video):
14
+ cap = cv2.VideoCapture(video)
15
+ frames = []
16
+ while cap.isOpened():
17
+ ret, frame = cap.read()
18
+ if not ret:
19
+ break
20
+ results = model(frame)
21
+ annotated = results[0].plot()
22
+ frames.append(annotated)
23
+ cap.release()
24
+
25
+ # Simpan video baru
26
+ out_path = "output.mp4"
27
+ height, width, _ = frames[0].shape
28
+ out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'mp4v'), 15, (width, height))
29
+ for f in frames:
30
+ out.write(f)
31
+ out.release()
32
+ return out_path
33
+
34
+ demo = gr.Interface(
35
+ fn=predict_image,
36
+ inputs=gr.Image(type="numpy"),
37
+ outputs="image",
38
+ title="YOLOv8 Image Detection",
39
+ description="Upload an image for detection"
40
+ )
41
+
42
+ video_demo = gr.Interface(
43
+ fn=predict_video,
44
+ inputs=gr.Video(),
45
+ outputs=gr.Video(),
46
+ title="YOLOv8 Video Detection",
47
+ description="Upload a video for detection"
48
+ )
49
+
50
+ app = gr.TabbedInterface([demo, video_demo], ["Image Detection", "Video Detection"])
51
+
52
+ app.launch()