Mohamedgodz commited on
Commit
14e965c
·
verified ·
1 Parent(s): 969d67d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -93
app.py CHANGED
@@ -1,93 +1,93 @@
1
- import gradio as gr
2
- import cv2
3
- from ultralytics import YOLO
4
- import numpy as np
5
- import tempfile
6
-
7
- # Load your 3 YOLO models
8
- models = [
9
- YOLO("yolo_trained_model.pt"),
10
- YOLO("car_person_best.pt"),
11
- YOLO("license best.pt")
12
- ]
13
-
14
- # Image detection
15
-
16
-
17
- def detect_on_image(image):
18
- result_frame = image.copy()
19
- for model in models:
20
- results = model(result_frame)
21
- result_frame = results[0].plot()
22
- return result_frame
23
-
24
- # Video file detection
25
-
26
-
27
- def detect_on_video(video):
28
- temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
29
- cap = cv2.VideoCapture(video)
30
-
31
- fps = cap.get(cv2.CAP_PROP_FPS)
32
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
33
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
34
-
35
- out = cv2.VideoWriter(temp_out.name, cv2.VideoWriter_fourcc(
36
- *'mp4v'), fps, (width, height))
37
-
38
- while cap.isOpened():
39
- ret, frame = cap.read()
40
- if not ret:
41
- break
42
- result_frame = frame.copy()
43
- for model in models:
44
- results = model(result_frame)
45
- result_frame = results[0].plot()
46
- out.write(result_frame)
47
-
48
- cap.release()
49
- out.release()
50
- return temp_out.name
51
-
52
- # Real-time webcam detection (frame-by-frame)
53
-
54
-
55
- def detect_on_camera(frame):
56
- result_frame = frame.copy()
57
- for model in models:
58
- results = model(result_frame)
59
- result_frame = results[0].plot()
60
- return result_frame
61
-
62
-
63
- # Create three separate interfaces
64
- image_interface = gr.Interface(
65
- fn=detect_on_image,
66
- inputs=gr.Image(type="numpy", label="Upload an Image"),
67
- outputs=gr.Image(type="numpy", label="Annotated Image"),
68
- title="Image Object Detection"
69
- )
70
-
71
- video_interface = gr.Interface(
72
- fn=detect_on_video,
73
- inputs=gr.Video(label="Upload a Video"),
74
- outputs=gr.Video(label="Processed Video"),
75
- title="Video Object Detection"
76
- )
77
-
78
- camera_interface = gr.Interface(
79
- fn=detect_on_camera,
80
- inputs=gr.Image(source="webcam", label="Webcam Input", type="numpy"),
81
- outputs=gr.Image(type="numpy", label="Live Detection"),
82
- live=True,
83
- title="Real-Time Webcam Detection"
84
- )
85
-
86
- # Combine all interfaces in a tab layout
87
- demo = gr.TabbedInterface(
88
- interface_list=[image_interface, video_interface, camera_interface],
89
- tab_names=["Image", "Video", "Live Camera"]
90
- )
91
-
92
- if __name__ == "__main__":
93
- demo.launch()
 
1
+ import gradio as gr
2
+ import cv2
3
+ from ultralytics import YOLO
4
+ import numpy as np
5
+ import tempfile
6
+
7
+ # Load your 3 YOLO models
8
+ models = [
9
+ YOLO("yolo_trained_model.pt"),
10
+ YOLO("car_person_best.pt"),
11
+ YOLO("license best.pt")
12
+ ]
13
+
14
+ # Image detection
15
+
16
+
17
+ def detect_on_image(image):
18
+ result_frame = image.copy()
19
+ for model in models:
20
+ results = model(result_frame)
21
+ result_frame = results[0].plot()
22
+ return result_frame
23
+
24
+ # Video file detection
25
+
26
+
27
+ def detect_on_video(video):
28
+ temp_out = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
29
+ cap = cv2.VideoCapture(video)
30
+
31
+ fps = cap.get(cv2.CAP_PROP_FPS)
32
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
33
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
34
+
35
+ out = cv2.VideoWriter(temp_out.name, cv2.VideoWriter_fourcc(
36
+ *'mp4v'), fps, (width, height))
37
+
38
+ while cap.isOpened():
39
+ ret, frame = cap.read()
40
+ if not ret:
41
+ break
42
+ result_frame = frame.copy()
43
+ for model in models:
44
+ results = model(result_frame)
45
+ result_frame = results[0].plot()
46
+ out.write(result_frame)
47
+
48
+ cap.release()
49
+ out.release()
50
+ return temp_out.name
51
+
52
+ # Real-time webcam detection (frame-by-frame)
53
+
54
+
55
+ def detect_on_camera(frame):
56
+ result_frame = frame.copy()
57
+ for model in models:
58
+ results = model(result_frame)
59
+ result_frame = results[0].plot()
60
+ return result_frame
61
+
62
+
63
+ # Create three separate interfaces
64
+ image_interface = gr.Interface(
65
+ fn=detect_on_image,
66
+ inputs=gr.Image(type="numpy", label="Upload an Image"),
67
+ outputs=gr.Image(type="numpy", label="Annotated Image"),
68
+ title="Image Object Detection"
69
+ )
70
+
71
+ video_interface = gr.Interface(
72
+ fn=detect_on_video,
73
+ inputs=gr.Video(label="Upload a Video"),
74
+ outputs=gr.Video(label="Processed Video"),
75
+ title="Video Object Detection"
76
+ )
77
+
78
+ camera_interface = gr.Interface(
79
+ fn=detect_on_camera,
80
+ inputs=gr.Camera(label="Webcam Input"),
81
+ outputs=gr.Image(type="numpy", label="Live Detection"),
82
+ live=True,
83
+ title="Real-Time Webcam Detection"
84
+ )
85
+
86
+ # Combine all interfaces in a tab layout
87
+ demo = gr.TabbedInterface(
88
+ interface_list=[image_interface, video_interface, camera_interface],
89
+ tab_names=["Image", "Video", "Live Camera"]
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ demo.launch()