nagasurendra commited on
Commit
8c84287
·
verified ·
1 Parent(s): 639daa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -71
app.py CHANGED
@@ -2,82 +2,92 @@ import cv2
2
  import torch
3
  import gradio as gr
4
  import numpy as np
5
- from ultralytics import YOLO
6
  import matplotlib.pyplot as plt
 
 
 
 
 
 
 
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
- # List to store frames with detections
14
- frames_with_detections = []
15
-
16
- # Define the function to process the video
17
- def process_video(video):
18
- # Open the video file
19
- input_video = cv2.VideoCapture(video)
20
- frame_width = int(input_video.get(cv2.CAP_PROP_FRAME_WIDTH))
21
- frame_height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
22
- fps = input_video.get(cv2.CAP_PROP_FPS)
23
-
24
- # Resize frames to 640x480 (optional, to reduce computational load)
25
- new_width, new_height = 640, 480
26
-
 
 
 
 
27
  while True:
28
- # Read a frame from the video
29
- ret, frame = input_video.read()
30
- if not ret:
31
- break # End of video
32
-
33
- # Resize the frame
34
- frame = cv2.resize(frame, (new_width, new_height))
35
-
36
- # Perform inference on the frame
37
- results = model(frame) # Automatically uses GPU if available
38
-
39
- # If there are detections
40
- if len(results[0].boxes) > 0:
41
- boxes = results[0].boxes.xyxy.cpu().numpy() # Get the bounding boxes
42
-
43
- # Annotate the frame with bounding boxes
44
- annotated_frame = results[0].plot()
45
-
46
- # Convert the frame to RGB
47
- annotated_frame_rgb = cv2.cvtColor(annotated_frame, cv2.COLOR_BGR2RGB)
48
-
49
- # Append the frame with detection to list
50
- frames_with_detections.append(annotated_frame_rgb)
51
-
52
- # Create a simple bar chart to show the count of detected objects
53
- fig, ax = plt.subplots()
54
- ax.bar([1], [len(boxes)], color='blue') # Bar for the current frame detection
55
- ax.set_xlabel('Frame')
56
- ax.set_ylabel('Number of Detections')
57
- ax.set_title('Detection Count per Frame')
58
-
59
- # Convert plot to an image to return it in Gradio output
60
- plt.tight_layout()
61
- plt.close(fig)
62
-
63
- # Save the plot as an image in memory
64
- buf = np.frombuffer(fig.canvas.print_to_buffer()[0], dtype=np.uint8)
65
- img = cv2.imdecode(buf, cv2.IMREAD_COLOR)
66
 
67
- # Yield the detected frame and the graph at the same time
68
- yield annotated_frame_rgb, img
69
-
70
- # Release resources
71
- input_video.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
  # Gradio interface
74
- with gr.Blocks() as demo:
75
- with gr.Row():
76
- video_input = gr.Video(label="Upload Video")
77
- gallery_output = gr.Gallery(label="Detection Album") # Removed style() method
78
- graph_output = gr.Image(label="Detection Counts Graph", type="numpy") # For displaying graph
79
-
80
- video_input.change(process_video, inputs=video_input, outputs=[gallery_output, graph_output])
81
-
82
- # Launch the interface
83
- demo.launch()
 
 
 
 
 
2
  import torch
3
  import gradio as gr
4
  import numpy as np
5
+ import os
6
  import matplotlib.pyplot as plt
7
+ from ultralytics import YOLO, __version__ as ultralytics_version
8
+
9
+ # Debug: Check environment
10
+ print(f"Torch version: {torch.__version__}")
11
+ print(f"Gradio version: {gr.__version__}")
12
+ print(f"Ultralytics version: {ultralytics_version}")
13
+ print(f"CUDA available: {torch.cuda.is_available()}")
14
 
15
  # Load YOLOv8 model
16
  device = "cuda" if torch.cuda.is_available() else "cpu"
17
+ print(f"Using device: {device}")
18
+ model = YOLO('./data/best.pt').to(device)
19
+
20
+ def process_video(video, output_folder="detected_frames", plot_graphs=False):
21
+ if video is None:
22
+ return "Error: No video uploaded"
23
+
24
+ # Create output folder if it doesn't exist
25
+ if not os.path.exists(output_folder):
26
+ os.makedirs(output_folder)
27
+
28
+ cap = cv2.VideoCapture(video)
29
+ if not cap.isOpened():
30
+ return "Error: Could not open video file"
31
+
32
+ frame_width, frame_height = 320, 240 # Smaller resolution
33
+ frame_count = 0
34
+ frame_skip = 5 # Process every 5th frame
35
+ max_frames = 100 # Limit for testing
36
+ confidence_scores = [] # Store confidence scores for plotting
37
+
38
  while True:
39
+ ret, frame = cap.read()
40
+ if not ret or frame_count > max_frames:
41
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ frame_count += 1
44
+ if frame_count % frame_skip != 0:
45
+ continue
46
+
47
+ frame = cv2.resize(frame, (frame_width, frame_height))
48
+ print(f"Processing frame {frame_count}")
49
+
50
+ # Run YOLOv8 inference
51
+ results = model(frame)
52
+ annotated_frame = results[0].plot()
53
+
54
+ # Save annotated frame
55
+ frame_filename = os.path.join(output_folder, f"frame_{frame_count:04d}.jpg")
56
+ cv2.imwrite(frame_filename, annotated_frame)
57
+
58
+ # Collect confidence scores for plotting
59
+ if results[0].boxes is not None:
60
+ confs = results[0].boxes.conf.cpu().numpy()
61
+ confidence_scores.extend(confs)
62
+
63
+ cap.release()
64
+
65
+ # Generate confidence score plot if requested
66
+ graph_path = None
67
+ if plot_graphs and confidence_scores:
68
+ plt.figure(figsize=(10, 5))
69
+ plt.hist(confidence_scores, bins=20, color='blue', alpha=0.7)
70
+ plt.title('Distribution of Confidence Scores')
71
+ plt.xlabel('Confidence Score')
72
+ plt.ylabel('Frequency')
73
+ graph_path = os.path.join(output_folder, "confidence_histogram.png")
74
+ plt.savefig(graph_path)
75
+ plt.close()
76
+
77
+ return f"Frames saved in {output_folder}. {f'Graph saved as {graph_path}' if graph_path else ''}"
78
 
79
  # Gradio interface
80
+ iface = gr.Interface(
81
+ fn=process_video,
82
+ inputs=[
83
+ gr.Video(label="Upload Video"),
84
+ gr.Textbox(label="Output Folder", value="detected_frames"),
85
+ gr.Checkbox(label="Generate Confidence Score Graph", value=False)
86
+ ],
87
+ outputs=gr.Text(label="Status"),
88
+ title="YOLOv8 Object Detection - Frames Output",
89
+ description="Upload a short video to save detected frames as images and optionally generate a confidence score graph."
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ iface.launch()