nagasurendra commited on
Commit
414f345
·
verified ·
1 Parent(s): 045842d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -95
app.py CHANGED
@@ -1,113 +1,60 @@
1
-
2
- import os
3
  import gradio as gr
 
4
  import cv2
5
- import numpy as np
6
  from ultralytics import YOLO
7
- import tempfile
8
- import torch
9
- import ultralytics.nn.tasks
10
-
11
- # Set Ultralytics config path
12
- os.environ['YOLO_CONFIG_DIR'] = '/tmp/Ultralytics'
13
-
14
- # Custom function to load model with trusted weights
15
- def load_model_with_trusted_weights(model_path):
16
- with torch.serialization.safe_globals([
17
- ultralytics.nn.tasks.DetectionModel,
18
- torch.nn.modules.container.Sequential
19
- ]):
20
- return YOLO(model_path)
21
-
22
- # Load both YOLO models
23
- model_yolo11 = load_model_with_trusted_weights('./data/yolo11n.pt')
24
- model_best = load_model_with_trusted_weights('./data/best.pt')
25
-
26
- def process_video(video_path, model_name, conf_threshold=0.4):
27
- """
28
- Process the input video frame by frame using the selected YOLO model,
29
- draw bounding boxes, and return the processed video path.
30
- """
31
- # Select model
32
- model = model_yolo11 if model_name == "YOLO11n" else model_best
33
 
34
- # Open video capture
35
- cap = cv2.VideoCapture(video_path)
36
- if not cap.isOpened():
37
- raise ValueError("Could not open video file")
38
 
39
- # Get video properties
40
- fps = int(cap.get(cv2.CAP_PROP_FPS))
41
- width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
42
- height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
 
43
 
44
- # Define output video path
45
- temp_video_path = os.path.join(tempfile.gettempdir(), "output.mp4")
46
- fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for MP4
47
- out = cv2.VideoWriter(temp_video_path, fourcc, fps, (width, height))
48
 
49
  while cap.isOpened():
50
  ret, frame = cap.read()
51
  if not ret:
52
  break
53
-
54
- # Perform detection
55
- results = model.predict(
56
- source=frame,
57
- conf=conf_threshold,
58
- imgsz=640,
59
- show_labels=True,
60
- show_conf=True
61
- )
62
-
63
- # Draw bounding boxes
64
- for result in results:
65
- im_array = result.plot() # Plot boxes
66
- out.write(im_array) # Write frame to output video
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  cap.release()
69
  out.release()
70
- cv2.destroyAllWindows()
71
 
72
- return temp_video_path
73
 
74
  # Gradio interface
75
- with gr.Blocks() as app:
76
- gr.HTML("""
77
- <h1 style='text-align: center'>
78
- Video Object Detection with YOLO Models
79
- </h1>
80
- """)
81
-
82
- with gr.Row():
83
- with gr.Column():
84
- video_input = gr.Video(label="Upload Video")
85
- model_choice = gr.Dropdown(
86
- choices=["YOLO11n", "Best Model"],
87
- label="Select Model",
88
- value="YOLO11n"
89
- )
90
- conf_threshold = gr.Slider(
91
- label="Confidence Threshold",
92
- minimum=0.0,
93
- maximum=1.0,
94
- step=0.05,
95
- value=0.4
96
- )
97
- process_button = gr.Button("Process Video")
98
-
99
- with gr.Column():
100
- video_output = gr.Video(
101
- label="Processed Video",
102
- streaming=True,
103
- autoplay=True
104
- )
105
-
106
- process_button.click(
107
- fn=process_video,
108
- inputs=[video_input, model_choice, conf_threshold],
109
- outputs=[video_output]
110
- )
111
 
112
- if __name__ == "__main__":
113
- app.launch()
 
 
 
1
  import gradio as gr
2
+ import torch
3
  import cv2
 
4
  from ultralytics import YOLO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # Load YOLO models
7
+ model_yolo11 = YOLO('./data/yolo11n.pt')
8
+ model_best = YOLO('./data/best.pt')
 
9
 
10
+ def process_video(video):
11
+ # Read video input
12
+ cap = cv2.VideoCapture(video.name)
13
+ fps = cap.get(cv2.CAP_PROP_FPS)
14
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
15
+ frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
16
 
17
+ # Create a VideoWriter object to save the output video
18
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
19
+ out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
 
20
 
21
  while cap.isOpened():
22
  ret, frame = cap.read()
23
  if not ret:
24
  break
25
+
26
+ # Use both YOLO models for detection
27
+ results_yolo11 = model_yolo11(frame)
28
+ results_best = model_best(frame)
29
+
30
+ # Combine the results from both models
31
+ # For simplicity, we will overlay bounding boxes and labels from both models
32
+ for result in results_yolo11:
33
+ boxes = result.boxes
34
+ for box in boxes:
35
+ x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
36
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
37
+ label = f"YOLOv11: {box.cls[0]} - {box.conf[0]:.2f}"
38
+ cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
39
+
40
+ for result in results_best:
41
+ boxes = result.boxes
42
+ for box in boxes:
43
+ x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
44
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
45
+ label = f"Best: {box.cls[0]} - {box.conf[0]:.2f}"
46
+ cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
47
+
48
+ # Write the processed frame to the output video
49
+ out.write(frame)
50
 
51
  cap.release()
52
  out.release()
 
53
 
54
+ return 'output_video.mp4'
55
 
56
  # Gradio interface
57
+ iface = gr.Interface(fn=process_video, inputs=gr.Video(), outputs=gr.Video(), live=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
+ # Launch the app
60
+ iface.launch()