nagasurendra commited on
Commit
a952223
·
verified ·
1 Parent(s): 6b77b78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -59
app.py CHANGED
@@ -1,71 +1,57 @@
1
  import cv2
2
  import torch
3
- import gradio as gr
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):
14
- # video is now the file path string, not a file object
15
- input_video = cv2.VideoCapture(video) # Directly pass the path to cv2.VideoCapture
16
-
17
- # Get frame width, height, and fps from input video
18
- frame_width = int(input_video.get(cv2.CAP_PROP_FRAME_WIDTH))
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 = 1 # 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
52
-
53
- # Write the annotated frame to the output video
54
- output_video.write(annotated_frame)
55
-
56
- # Release resources
57
- input_video.release()
58
- output_video.release()
59
-
60
- # Return the processed video file path
61
- return output_video_path
62
-
63
- # Create a Gradio interface for video upload
64
- iface = gr.Interface(fn=process_video,
65
- inputs=gr.Video(label="Upload Video"), # Updated line
66
- outputs=gr.Video(label="Processed Video"), # This will display the output video directly
67
- title="YOLOv8 Object Detection on Video",
68
- description="Upload a video for object detection using YOLOv8")
69
-
70
- # Launch the interface
71
- iface.launch()
 
1
  import cv2
2
  import torch
3
+ import gradio
4
  import numpy as np
5
  from ultralytics import YOLO
6
 
7
+ # Load YOLOv8 model
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ print(f"Using device: {device}") # Debug
10
+ model = YOLO('./data/best.pt').to(device)
11
 
 
12
  def process_video(video):
13
+ cap = cv2.VideoCapture(video)
14
+ frame_width, frame_height = 320, 240 # Smaller resolution
15
+ fps = cap.get(cv2.CAP_PROP_FPS)
16
+
17
+ output_path = "processed_output.mp4"
18
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
19
+ out = cv2.VideoWriter(output_path, fourcc, fourcc, fps, (frame_width, frame_height))
20
+
 
 
 
 
 
 
 
 
 
 
21
  frame_count = 0
22
+ frame_skip = 5 # Process every 5th frame
23
+ max_frames = 100 # Limit to 100 frames for testing
24
+
25
  while True:
26
+ ret, frame = cap.read()
27
+ if not ret or frame_count > max_frames:
28
+ break
29
+
 
30
  frame_count += 1
31
  if frame_count % frame_skip != 0:
32
+ continue
33
+
34
+ frame = cv2.resize(frame, (frame_width, frame_height))
35
+ print(f"Processing frame {frame_count}") # Debug
36
+
37
+ # Inference
38
+ results = model(frame)
39
+ annotated_frame = results[0].plot()
40
+
41
+ # Write frame
42
+ out.write(annotated_frame)
43
+
44
+ cap.release()
45
+ out.release()
46
+ return output_path
47
+
48
+ # Gradio interface
49
+ iface = gr.Interface(
50
+ fn=process_video,
51
+ inputs=gr.Video(label="Upload Video"),
52
+ outputs=gr.Video(label="Processed Video"),
53
+ title="YOLOv8 Object Detection",
54
+ description="Upload a short video for testing"
55
+ )
56
+
57
+ iface.launch()