JrEasy commited on
Commit
4645159
·
verified ·
1 Parent(s): 71c3856

Upload folder using huggingface_hub

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