JrEasy commited on
Commit
dec4087
·
verified ·
1 Parent(s): f3f95fb

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +201 -0
app.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Judol Gradio YOLO11.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1oiuTAi-cys1ydtUhSDJSRdeA02mAmZQH
8
+ """
9
+
10
+ !pip install ultralytics
11
+ !pip install gradio
12
+
13
+ import cv2
14
+ from ultralytics import YOLO
15
+ import gradio as gr
16
+ import imageio
17
+ from google.colab import drive
18
+
19
+ model = YOLO('https://huggingface.co/JrEasy/Judol-Detection-YOLO11/resolve/main/best.pt')
20
+
21
+
22
+ confidence_threshold = 0.6
23
+
24
+ class_names = {
25
+ 0: "BK8",
26
+ 1: "Gate of Olympus",
27
+ 2: "Princess",
28
+ 3: "Starlight Princess",
29
+ 4: "Zeus",
30
+ }
31
+
32
+ class_colors = {
33
+ 0: (0, 255, 0), # Green for BK8
34
+ 1: (255, 0, 0), # Blue for Gate of Olympus
35
+ 2: (0, 0, 255), # Red for Princess
36
+ 3: (255, 255, 0), # Cyan for Starlight Princess
37
+ 4: (255, 0, 255), # Magenta for Zeus
38
+ }
39
+
40
+ def format_time_ranges(timestamps, classes):
41
+
42
+ if not timestamps:
43
+ return ""
44
+
45
+
46
+ class_timestamps = {}
47
+
48
+ for timestamp, class_id in zip(timestamps, classes):
49
+ class_name = class_names.get(class_id, 'Unknown')
50
+ if class_name not in class_timestamps:
51
+ class_timestamps[class_name] = []
52
+ class_timestamps[class_name].append(timestamp)
53
+
54
+
55
+ formatted_ranges = []
56
+
57
+ for class_name, timestamps in class_timestamps.items():
58
+ timestamps = sorted(timestamps)
59
+ ranges = []
60
+ start = timestamps[0]
61
+ for i in range(1, len(timestamps)):
62
+ if timestamps[i] - timestamps[i - 1] <= 1:
63
+ continue
64
+ else:
65
+ ranges.append(f"{int(start)}-{int(timestamps[i - 1])}")
66
+ start = timestamps[i]
67
+
68
+ ranges.append(f"{int(start)}-{int(timestamps[-1])}")
69
+
70
+ formatted_ranges.append(f"{class_name} = {', '.join(ranges)}")
71
+
72
+ return ", ".join(formatted_ranges)
73
+
74
+ def process_video(input_video):
75
+ cap = cv2.VideoCapture(input_video)
76
+ if not cap.isOpened():
77
+ print("Error: Could not open input video.")
78
+ return None, []
79
+
80
+ fps = cap.get(cv2.CAP_PROP_FPS)
81
+ output_video_path = "/content/drive/MyDrive/Computer Vision YOLO-Judol Detection/processed_video.mp4"
82
+ writer = imageio.get_writer(output_video_path, fps=fps, codec="libx264")
83
+
84
+ frame_count = 0
85
+ timestamps = []
86
+ classes_detected = []
87
+
88
+ while cap.isOpened():
89
+ ret, frame = cap.read()
90
+ if not ret:
91
+ break
92
+
93
+ timestamp = frame_count / fps
94
+ frame_count += 1
95
+
96
+ gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
97
+ input_frame = cv2.merge([gray_frame, gray_frame, gray_frame])
98
+
99
+ results = model.predict(input_frame)
100
+
101
+ for result in results:
102
+ for box in result.boxes:
103
+ if box.conf[0] >= confidence_threshold:
104
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
105
+ class_id = int(box.cls[0])
106
+ class_name = class_names.get(class_id, f"Class {class_id}")
107
+ color = class_colors.get(class_id, (0, 255, 0))
108
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
109
+ text = f'{class_name}, Conf: {box.conf[0]:.2f}'
110
+ text_position = (x1, y1 - 10 if y1 > 20 else y1 + 20)
111
+ cv2.putText(frame, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
112
+
113
+ timestamps.append(timestamp)
114
+ classes_detected.append(class_id)
115
+
116
+ writer.append_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
117
+
118
+ cap.release()
119
+ writer.close()
120
+
121
+
122
+ formatted_time_ranges = format_time_ranges(timestamps, classes_detected)
123
+
124
+ print(f"Processed video saved at: {output_video_path}")
125
+
126
+ return output_video_path, formatted_time_ranges
127
+
128
+
129
+ def process_image(input_image):
130
+ # Convert image from RGB to BGR for OpenCV processing
131
+ bgr_frame = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
132
+
133
+ # Convert to grayscale and create a 3-channel grayscale image
134
+ gray_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2GRAY)
135
+ input_frame = cv2.merge([gray_frame, gray_frame, gray_frame])
136
+
137
+ # Run the model on the processed input
138
+ results = model.predict(input_frame)
139
+
140
+ detections_log = [] # Store detection logs
141
+ classes_detected = [] # Track detected class IDs
142
+
143
+ for result in results:
144
+ for box in result.boxes:
145
+ if box.conf[0] >= confidence_threshold: # Filter by confidence
146
+ x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
147
+ class_id = int(box.cls[0]) # Class ID
148
+ class_name = class_names.get(class_id, f"Class {class_id}")
149
+ color = class_colors.get(class_id, (0, 255, 0)) # Default green color
150
+
151
+ # Draw bounding box and class text on the frame
152
+ cv2.rectangle(bgr_frame, (x1, y1), (x2, y2), color, 2)
153
+ text = f'{class_name}, Conf: {box.conf[0]:.2f}'
154
+ text_position = (x1, y1 - 10 if y1 > 20 else y1 + 20)
155
+ cv2.putText(bgr_frame, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
156
+
157
+ # Log detection information
158
+ detections_log.append({
159
+ "class": class_name,
160
+ "confidence": box.conf[0]
161
+ })
162
+ classes_detected.append(class_id)
163
+
164
+ # Count occurrences of each class detected
165
+ class_count = {class_names.get(cls, f"Class {cls}"): classes_detected.count(cls) for cls in set(classes_detected)}
166
+
167
+ # Format the detections as 'Class = Count' pairs
168
+ formatted_log = ", ".join([f"{class_name} = {count}" for class_name, count in class_count.items()])
169
+
170
+ # Convert the output frame back to RGB
171
+ output_image = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
172
+ return output_image, formatted_log
173
+
174
+ with gr.Blocks() as app:
175
+ gr.Markdown("## Judol Detection using YOLOv11")
176
+
177
+ with gr.Tab("Video Detection"):
178
+ with gr.Row():
179
+ input_video = gr.Video(label="Upload a video")
180
+ output_video = gr.Video(label="Processed Video")
181
+ detections_log = gr.Textbox(label="Detections Log", lines=10)
182
+
183
+ input_video.change(
184
+ fn=lambda input_video: process_video(input_video) if input_video else ("", []),
185
+ inputs=input_video,
186
+ outputs=[output_video, detections_log],
187
+ )
188
+
189
+ with gr.Tab("Image Detection"):
190
+ with gr.Row():
191
+ input_image = gr.Image(label="Upload an image")
192
+ output_image = gr.Image(label="Processed Image")
193
+ image_detections_log = gr.Textbox(label="Detections Log", lines=10)
194
+
195
+ input_image.change(
196
+ fn=process_image,
197
+ inputs=input_image,
198
+ outputs=[output_image, image_detections_log],
199
+ )
200
+
201
+ app.launch()