nagasurendra commited on
Commit
c0cf5bc
·
verified ·
1 Parent(s): 98cc7fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -2
app.py CHANGED
@@ -4,8 +4,10 @@ import gradio as gr
4
  import numpy as np
5
  from ultralytics import YOLO
6
 
7
- # Load YOLOv8 model
 
8
  model = YOLO('./data/best.pt') # Path to your model
 
9
 
10
  # Define the function that processes the uploaded video
11
  def process_video(video):
@@ -17,19 +19,33 @@ def process_video(video):
17
  frame_height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
18
  fps = input_video.get(cv2.CAP_PROP_FPS)
19
 
 
 
 
 
20
  # Create a VideoWriter object to write processed frames to an output file
21
  output_video_path = "processed_output.mp4"
22
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4 format
23
  output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
24
 
 
 
 
25
  while True:
26
  # Read a frame from the video
27
  ret, frame = input_video.read()
28
  if not ret:
29
  break # End of video
30
 
 
 
 
 
 
 
 
31
  # Perform inference on the frame
32
- results = model(frame)
33
 
34
  # The results object contains annotations for the frame
35
  annotated_frame = results[0].plot() # Plot the frame with bounding boxes
 
4
  import numpy as np
5
  from ultralytics import YOLO
6
 
7
+ # Load YOLOv8 model and set device (GPU if available)
8
+ device = "cuda" if torch.cuda.is_available() else "cpu"
9
  model = YOLO('./data/best.pt') # Path to your model
10
+ model.to(device)
11
 
12
  # Define the function that processes the uploaded video
13
  def process_video(video):
 
19
  frame_height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
20
  fps = input_video.get(cv2.CAP_PROP_FPS)
21
 
22
+ # Resize to reduce computation (optional)
23
+ new_width, new_height = 640, 480 # Resize to 640x480 resolution
24
+ frame_width, frame_height = new_width, new_height
25
+
26
  # Create a VideoWriter object to write processed frames to an output file
27
  output_video_path = "processed_output.mp4"
28
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4 format
29
  output_video = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
30
 
31
+ frame_skip = 10 # Skip 10 frames between each processed frame
32
+ frame_count = 0
33
+
34
  while True:
35
  # Read a frame from the video
36
  ret, frame = input_video.read()
37
  if not ret:
38
  break # End of video
39
 
40
+ frame_count += 1
41
+ if frame_count % frame_skip != 0:
42
+ continue # Skip frames
43
+
44
+ # Resize the frame to reduce computational load
45
+ frame = cv2.resize(frame, (new_width, new_height))
46
+
47
  # Perform inference on the frame
48
+ results = model(frame) # Automatically uses GPU if available
49
 
50
  # The results object contains annotations for the frame
51
  annotated_frame = results[0].plot() # Plot the frame with bounding boxes