Spaces:
Sleeping
Sleeping
updated the annotation library
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 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')
|
|
@@ -14,8 +15,8 @@ ANNOTATION_CATEGORIES = {
|
|
| 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": ["
|
| 18 |
-
"Shot direction": ["
|
| 19 |
"Fielder's Action": ["Catch taken", "Catch dropped", "Misfield", "Run-out attempt", "Fielder fields"]
|
| 20 |
}
|
| 21 |
|
|
@@ -28,6 +29,7 @@ class VideoAnnotator:
|
|
| 28 |
self.video_files = []
|
| 29 |
self.current_video_idx = 0
|
| 30 |
self.annotations = {}
|
|
|
|
| 31 |
|
| 32 |
def load_videos_from_hf(self):
|
| 33 |
try:
|
|
@@ -61,20 +63,72 @@ class VideoAnnotator:
|
|
| 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 |
-
|
|
|
|
| 71 |
json.dump(annotations_dict, f, indent=4)
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
|
@@ -159,6 +213,13 @@ def create_interface():
|
|
| 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(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
+
from huggingface_hub import hf_hub_download, list_repo_files, upload_file, HfApi
|
| 5 |
import logging
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 15 |
"Delivery Type": ["Yorker", "Bouncer", "Length Ball", "Slower ball", "Googly", "Arm Ball", "Other"],
|
| 16 |
"Ball's trajectory": ["In Swing", "Out Swing", "Off spin", "Leg spin"],
|
| 17 |
"Shot Played": ["Cover Drive", "Straight Drive", "On Drive", "Pull", "Square Cut", "Defensive Block"],
|
| 18 |
+
"Outcome of the shot": ["Four (4)", "Six (6)", "Wicket", "Single (1)", "Double (2)", "Triple (3)", "Dot (0)"],
|
| 19 |
+
"Shot direction": ["Long On", "Long Off", "Cover", "Point", "Midwicket", "Square Leg", "Third Man", "Fine Leg"],
|
| 20 |
"Fielder's Action": ["Catch taken", "Catch dropped", "Misfield", "Run-out attempt", "Fielder fields"]
|
| 21 |
}
|
| 22 |
|
|
|
|
| 29 |
self.video_files = []
|
| 30 |
self.current_video_idx = 0
|
| 31 |
self.annotations = {}
|
| 32 |
+
self.hf_token = os.environ.get("HF_TOKEN") # Get token from environment variable
|
| 33 |
|
| 34 |
def load_videos_from_hf(self):
|
| 35 |
try:
|
|
|
|
| 63 |
return None
|
| 64 |
|
| 65 |
def save_annotation(self, annotations_dict):
|
| 66 |
+
if not annotations_dict:
|
| 67 |
+
logger.warning("No annotations to save")
|
| 68 |
+
return "No annotations to save"
|
| 69 |
+
|
| 70 |
video_name = os.path.basename(self.video_files[self.current_video_idx])
|
| 71 |
annotation_file = f"{video_name}_annotations.json"
|
| 72 |
|
| 73 |
logger.info(f"Saving annotations for {video_name} to {annotation_file}")
|
| 74 |
|
| 75 |
try:
|
| 76 |
+
# Save locally in a temporary file
|
| 77 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
|
| 78 |
json.dump(annotations_dict, f, indent=4)
|
| 79 |
+
temp_path = f.name
|
| 80 |
+
|
| 81 |
+
logger.info("Annotations saved locally")
|
| 82 |
+
|
| 83 |
+
# Upload to Hugging Face repo
|
| 84 |
+
if self.hf_token:
|
| 85 |
+
logger.info(f"Uploading annotations to Hugging Face repo: {HF_REPO_ID}")
|
| 86 |
+
api = HfApi()
|
| 87 |
+
api.upload_file(
|
| 88 |
+
path_or_fileobj=temp_path,
|
| 89 |
+
path_in_repo=f"annotations/{annotation_file}",
|
| 90 |
+
repo_id=HF_REPO_ID,
|
| 91 |
+
repo_type=HF_REPO_TYPE,
|
| 92 |
+
token=self.hf_token
|
| 93 |
+
)
|
| 94 |
+
os.unlink(temp_path) # Clean up the temporary file
|
| 95 |
+
return f"Annotations saved for {video_name} and uploaded to Hugging Face repo"
|
| 96 |
+
else:
|
| 97 |
+
logger.warning("HF_TOKEN not found. Annotations saved locally only.")
|
| 98 |
+
# Copy from temp to current directory for local access
|
| 99 |
+
with open(annotation_file, 'w') as f:
|
| 100 |
+
with open(temp_path, 'r') as temp_f:
|
| 101 |
+
f.write(temp_f.read())
|
| 102 |
+
os.unlink(temp_path) # Clean up the temporary file
|
| 103 |
+
return f"Annotations saved locally for {video_name} (no HF upload)"
|
| 104 |
except Exception as e:
|
| 105 |
logger.error(f"Error saving annotations: {e}")
|
| 106 |
return f"Error saving: {str(e)}"
|
| 107 |
|
| 108 |
+
def load_existing_annotation(self):
|
| 109 |
+
"""Try to load existing annotation for the current video from the repo"""
|
| 110 |
+
if not self.video_files:
|
| 111 |
+
return None
|
| 112 |
+
|
| 113 |
+
video_name = os.path.basename(self.video_files[self.current_video_idx])
|
| 114 |
+
annotation_path = f"annotations/{video_name}_annotations.json"
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
# Check if annotation exists in the repo
|
| 118 |
+
all_files = list_repo_files(HF_REPO_ID, repo_type=HF_REPO_TYPE)
|
| 119 |
+
if annotation_path in all_files:
|
| 120 |
+
local_path = hf_hub_download(
|
| 121 |
+
repo_id=HF_REPO_ID,
|
| 122 |
+
filename=annotation_path,
|
| 123 |
+
repo_type=HF_REPO_TYPE
|
| 124 |
+
)
|
| 125 |
+
with open(local_path, 'r') as f:
|
| 126 |
+
return json.load(f)
|
| 127 |
+
return None
|
| 128 |
+
except Exception as e:
|
| 129 |
+
logger.error(f"Error loading existing annotation: {e}")
|
| 130 |
+
return None
|
| 131 |
+
|
| 132 |
def next_video(self, *current_annotations):
|
| 133 |
# Save current annotations before moving to next video
|
| 134 |
if self.video_files:
|
|
|
|
| 213 |
current_video = annotator.get_current_video()
|
| 214 |
if current_video:
|
| 215 |
video_player.value = current_video
|
| 216 |
+
|
| 217 |
+
# Try to load existing annotations
|
| 218 |
+
existing_annotations = annotator.load_existing_annotation()
|
| 219 |
+
if existing_annotations:
|
| 220 |
+
for i, category in enumerate(ANNOTATION_CATEGORIES.keys()):
|
| 221 |
+
if category in existing_annotations:
|
| 222 |
+
annotation_components[i].value = existing_annotations[category]
|
| 223 |
|
| 224 |
# Event handlers
|
| 225 |
save_btn.click(
|