nagasurendra commited on
Commit
fa66a0f
·
verified ·
1 Parent(s): 79226f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -14
app.py CHANGED
@@ -2,13 +2,18 @@ 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
@@ -23,6 +28,10 @@ def process_video(video):
23
  new_width, new_height = 640, 480 # Resize to 640x480 resolution
24
  frame_width, frame_height = new_width, new_height
25
 
 
 
 
 
26
  while True:
27
  # Read a frame from the video
28
  ret, frame = input_video.read()
@@ -37,24 +46,51 @@ def process_video(video):
37
 
38
  # Check if any object was detected
39
  if len(results[0].boxes) > 0: # If there are detected objects
40
- # Annotate the frame with bounding boxes
41
- annotated_frame = results[0].plot() # Plot the frame with bounding boxes
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # Convert the annotated frame to RGB format for displaying
44
- annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
45
 
46
- # Yield the frame immediately
47
- yield annotated_frame_rgb
 
 
 
 
48
 
49
  # Release resources
50
  input_video.release()
51
 
52
- # Create a Gradio interface for video upload
53
- iface = gr.Interface(fn=process_video,
54
- inputs=gr.Video(label="Upload Video"), # Updated line
55
- outputs=gr.Image(label="Processed Frame", type="numpy"), # Show processed frame immediately
56
- title="YOLOv8 Object Detection - Real-Time Display",
57
- description="Upload a video for object detection using YOLOv8. The frames with detections will be shown in real-time as they are processed.")
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Launch the interface
60
- iface.launch()
 
2
  import torch
3
  import gradio as gr
4
  import numpy as np
5
+ import matplotlib.pyplot as plt
6
  from ultralytics import YOLO
7
 
8
+ # Load YOLOv8 model
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
  model = YOLO('./data/best.pt') # Path to your model
11
  model.to(device)
12
 
13
+ # Store frames with detected objects
14
+ frames_with_detections = []
15
+ detection_counts = []
16
+
17
  # Define the function that processes the uploaded video
18
  def process_video(video):
19
  # video is now the file path string, not a file object
 
28
  new_width, new_height = 640, 480 # Resize to 640x480 resolution
29
  frame_width, frame_height = new_width, new_height
30
 
31
+ # Track detected objects by their bounding box coordinates
32
+ detected_boxes = set()
33
+ total_detections = 0
34
+
35
  while True:
36
  # Read a frame from the video
37
  ret, frame = input_video.read()
 
46
 
47
  # Check if any object was detected
48
  if len(results[0].boxes) > 0: # If there are detected objects
49
+ # Get the bounding boxes for each detected object
50
+ boxes = results[0].boxes.xyxy.cpu().numpy() # Get xyxy coordinates
51
+
52
+ # Loop through each detection and only show the frame for new objects
53
+ for box in boxes:
54
+ x1, y1, x2, y2 = box
55
+ detection_box = (x1, y1, x2, y2)
56
+
57
+ # Check if this box was already processed
58
+ if detection_box not in detected_boxes:
59
+ # Add the box to the set to avoid repeating the detection
60
+ detected_boxes.add(detection_box)
61
+ total_detections += 1
62
 
63
+ # Annotate the frame with bounding boxes
64
+ annotated_frame = results[0].plot() # Plot the frame with bounding boxes
65
 
66
+ # Convert the annotated frame to RGB format for displaying
67
+ annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
68
+
69
+ # Add this frame to the list of frames with detections
70
+ frames_with_detections.append(annotated_frame_rgb)
71
+ detection_counts.append(total_detections)
72
 
73
  # Release resources
74
  input_video.release()
75
 
76
+ # Return the frames with detections for display
77
+ return frames_with_detections
78
+
79
+ # Create a Gradio Blocks interface
80
+ with gr.Blocks() as demo:
81
+ # Define a file input for video upload
82
+ video_input = gr.Video(label="Upload Video")
83
+
84
+ # Define the output area to show processed frames
85
+ gallery_output = gr.Gallery(label="Detection Album").style(columns=3) # Display images in a row (album)
86
+
87
+ # Define the function to update frames in the album
88
+ def update_gallery(video):
89
+ detected_frames = process_video(video)
90
+ return detected_frames # Return all frames with detections
91
+
92
+ # Connect the video input to the gallery update
93
+ video_input.change(update_gallery, inputs=video_input, outputs=gallery_output)
94
 
95
  # Launch the interface
96
+ demo.launch()