Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
from huggingface_hub import hf_hub_download, list_repo_files
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
# Cricket annotation categories
|
12 |
+
ANNOTATION_CATEGORIES = {
|
13 |
+
"Bowler's Run Up": ["Fast", "Slow"],
|
14 |
+
"Delivery Type": ["Yorker", "Bouncer", "Length Ball", "Slower ball", "Googly", "Arm Ball", "Other"],
|
15 |
+
"Ball's trajectory": ["In Swing", "Out Swing", "Off spin", "Leg spin"],
|
16 |
+
"Shot Played": ["Cover Drive", "Straight Drive", "On Drive", "Pull", "Square Cut", "Defensive Block"],
|
17 |
+
"Outcome of the shot": ["four", "six", "wicket", "runs (1,2,3)"],
|
18 |
+
"Shot direction": ["long on", "long off", "cover", "point", "midwicket", "square leg", "third man", "fine leg"],
|
19 |
+
"Fielder's Action": ["Catch taken", "Catch dropped", "Misfield", "Run-out attempt", "Fielder fields"]
|
20 |
+
}
|
21 |
+
|
22 |
+
# HuggingFace repo info - replace with your actual repo
|
23 |
+
HF_REPO_ID = "username/cricket-videos" # Replace with your actual repo
|
24 |
+
HF_REPO_TYPE = "dataset" # or "model" depending on how you've stored the videos
|
25 |
+
|
26 |
+
class VideoAnnotator:
|
27 |
+
def __init__(self):
|
28 |
+
self.video_files = []
|
29 |
+
self.current_video_idx = 0
|
30 |
+
self.annotations = {}
|
31 |
+
|
32 |
+
def load_videos_from_hf(self):
|
33 |
+
try:
|
34 |
+
logger.info(f"Attempting to list files from HuggingFace repo: {HF_REPO_ID}")
|
35 |
+
self.video_files = [f for f in list_repo_files(HF_REPO_ID, repo_type=HF_REPO_TYPE)
|
36 |
+
if f.endswith(('.mp4', '.avi', '.mov'))]
|
37 |
+
logger.info(f"Found {len(self.video_files)} video files")
|
38 |
+
return len(self.video_files) > 0
|
39 |
+
except Exception as e:
|
40 |
+
logger.error(f"Error accessing HuggingFace repo: {e}")
|
41 |
+
return False
|
42 |
+
|
43 |
+
def get_current_video(self):
|
44 |
+
if not self.video_files:
|
45 |
+
logger.warning("No video files available")
|
46 |
+
return None
|
47 |
+
|
48 |
+
video_path = self.video_files[self.current_video_idx]
|
49 |
+
logger.info(f"Loading video: {video_path}")
|
50 |
+
|
51 |
+
try:
|
52 |
+
local_path = hf_hub_download(
|
53 |
+
repo_id=HF_REPO_ID,
|
54 |
+
filename=video_path,
|
55 |
+
repo_type=HF_REPO_TYPE
|
56 |
+
)
|
57 |
+
logger.info(f"Video downloaded to: {local_path}")
|
58 |
+
return local_path
|
59 |
+
except Exception as e:
|
60 |
+
logger.error(f"Error downloading video: {e}")
|
61 |
+
return None
|
62 |
+
|
63 |
+
def save_annotation(self, annotations_dict):
|
64 |
+
video_name = os.path.basename(self.video_files[self.current_video_idx])
|
65 |
+
annotation_file = f"{video_name}_annotations.json"
|
66 |
+
|
67 |
+
logger.info(f"Saving annotations for {video_name} to {annotation_file}")
|
68 |
+
|
69 |
+
try:
|
70 |
+
with open(annotation_file, 'w') as f:
|
71 |
+
json.dump(annotations_dict, f, indent=4)
|
72 |
+
logger.info("Annotations saved successfully")
|
73 |
+
return f"Annotations saved for {video_name}"
|
74 |
+
except Exception as e:
|
75 |
+
logger.error(f"Error saving annotations: {e}")
|
76 |
+
return f"Error saving: {str(e)}"
|
77 |
+
|
78 |
+
def next_video(self, *current_annotations):
|
79 |
+
# Save current annotations before moving to next video
|
80 |
+
if self.video_files:
|
81 |
+
annotations_dict = {}
|
82 |
+
for i, category in enumerate(ANNOTATION_CATEGORIES.keys()):
|
83 |
+
if current_annotations[i]:
|
84 |
+
annotations_dict[category] = current_annotations[i]
|
85 |
+
|
86 |
+
if annotations_dict:
|
87 |
+
self.save_annotation(annotations_dict)
|
88 |
+
|
89 |
+
# Move to next video
|
90 |
+
if self.current_video_idx < len(self.video_files) - 1:
|
91 |
+
self.current_video_idx += 1
|
92 |
+
logger.info(f"Moving to next video (index: {self.current_video_idx})")
|
93 |
+
return self.get_current_video(), *[None] * len(ANNOTATION_CATEGORIES)
|
94 |
+
else:
|
95 |
+
logger.info("Already at the last video")
|
96 |
+
return self.get_current_video(), *[None] * len(ANNOTATION_CATEGORIES)
|
97 |
+
|
98 |
+
def prev_video(self, *current_annotations):
|
99 |
+
# Save current annotations before moving to previous video
|
100 |
+
if self.video_files:
|
101 |
+
annotations_dict = {}
|
102 |
+
for i, category in enumerate(ANNOTATION_CATEGORIES.keys()):
|
103 |
+
if current_annotations[i]:
|
104 |
+
annotations_dict[category] = current_annotations[i]
|
105 |
+
|
106 |
+
if annotations_dict:
|
107 |
+
self.save_annotation(annotations_dict)
|
108 |
+
|
109 |
+
# Move to previous video
|
110 |
+
if self.current_video_idx > 0:
|
111 |
+
self.current_video_idx -= 1
|
112 |
+
logger.info(f"Moving to previous video (index: {self.current_video_idx})")
|
113 |
+
return self.get_current_video(), *[None] * len(ANNOTATION_CATEGORIES)
|
114 |
+
else:
|
115 |
+
logger.info("Already at the first video")
|
116 |
+
return self.get_current_video(), *[None] * len(ANNOTATION_CATEGORIES)
|
117 |
+
|
118 |
+
def create_interface():
|
119 |
+
annotator = VideoAnnotator()
|
120 |
+
success = annotator.load_videos_from_hf()
|
121 |
+
|
122 |
+
if not success:
|
123 |
+
logger.error("Failed to load videos. Using demo mode with sample video.")
|
124 |
+
# In real app, you might want to provide a sample video or show an error
|
125 |
+
|
126 |
+
with gr.Blocks() as demo:
|
127 |
+
gr.Markdown("# Cricket Video Annotation Tool")
|
128 |
+
|
129 |
+
with gr.Row():
|
130 |
+
video_player = gr.Video(label="Current Video")
|
131 |
+
|
132 |
+
annotation_components = []
|
133 |
+
|
134 |
+
with gr.Row():
|
135 |
+
with gr.Column():
|
136 |
+
for category, options in list(ANNOTATION_CATEGORIES.items())[:4]:
|
137 |
+
radio = gr.Radio(
|
138 |
+
choices=options,
|
139 |
+
label=category,
|
140 |
+
info=f"Select {category}"
|
141 |
+
)
|
142 |
+
annotation_components.append(radio)
|
143 |
+
|
144 |
+
with gr.Column():
|
145 |
+
for category, options in list(ANNOTATION_CATEGORIES.items())[4:]:
|
146 |
+
radio = gr.Radio(
|
147 |
+
choices=options,
|
148 |
+
label=category,
|
149 |
+
info=f"Select {category}"
|
150 |
+
)
|
151 |
+
annotation_components.append(radio)
|
152 |
+
|
153 |
+
with gr.Row():
|
154 |
+
prev_btn = gr.Button("Previous Video")
|
155 |
+
save_btn = gr.Button("Save Annotations", variant="primary")
|
156 |
+
next_btn = gr.Button("Next Video")
|
157 |
+
|
158 |
+
# Initialize with first video
|
159 |
+
current_video = annotator.get_current_video()
|
160 |
+
if current_video:
|
161 |
+
video_player.value = current_video
|
162 |
+
|
163 |
+
# Event handlers
|
164 |
+
save_btn.click(
|
165 |
+
fn=annotator.save_annotation,
|
166 |
+
inputs=[gr.Group(annotation_components)],
|
167 |
+
outputs=gr.Textbox(label="Status")
|
168 |
+
)
|
169 |
+
|
170 |
+
next_btn.click(
|
171 |
+
fn=annotator.next_video,
|
172 |
+
inputs=annotation_components,
|
173 |
+
outputs=[video_player] + annotation_components
|
174 |
+
)
|
175 |
+
|
176 |
+
prev_btn.click(
|
177 |
+
fn=annotator.prev_video,
|
178 |
+
inputs=annotation_components,
|
179 |
+
outputs=[video_player] + annotation_components
|
180 |
+
)
|
181 |
+
|
182 |
+
return demo
|
183 |
+
|
184 |
+
if __name__ == "__main__":
|
185 |
+
demo = create_interface()
|
186 |
+
demo.launch()
|