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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
import os
|
| 380 |
import json
|
|
@@ -418,7 +1027,7 @@ def download_npz_file(video_name: str) -> str:
|
|
| 418 |
"""
|
| 419 |
try:
|
| 420 |
# Construct the file path in the dataset repo
|
| 421 |
-
file_path = f"{video_name}.npz"
|
| 422 |
|
| 423 |
# Check if file already exists in cache
|
| 424 |
local_path = os.path.join(CACHE_DIR, f"{video_name}.npz")
|
|
@@ -455,7 +1064,7 @@ def get_available_videos_from_hf():
|
|
| 455 |
# Filter for .npz files in the I3D subfolder
|
| 456 |
videos = []
|
| 457 |
for file in files:
|
| 458 |
-
if file.startswith(f"") and file.endswith('.npz'):
|
| 459 |
# Extract the full filename without extension
|
| 460 |
# For files like "I3D/OP02-R02-TurkeySandwich.npz"
|
| 461 |
video_name = os.path.basename(file).replace('.npz', '')
|
|
|
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
+
|
| 379 |
+
# import os
|
| 380 |
+
# import json
|
| 381 |
+
# import torch
|
| 382 |
+
# import numpy as np
|
| 383 |
+
# import gradio as gr
|
| 384 |
+
# import opts_egtea as opts
|
| 385 |
+
# from dataset import VideoDataSet, calc_iou
|
| 386 |
+
# from models import MYNET, SuppressNet
|
| 387 |
+
# from loss_func import cls_loss_func, regress_loss_func
|
| 388 |
+
# from eval import evaluation_detection
|
| 389 |
+
# from iou_utils import non_max_suppression, check_overlap_proposal
|
| 390 |
+
# from typing import List, Dict, Optional
|
| 391 |
+
# from huggingface_hub import hf_hub_download, list_repo_files
|
| 392 |
+
# import tempfile
|
| 393 |
+
# import shutil
|
| 394 |
+
# import traceback
|
| 395 |
+
|
| 396 |
+
# # Configuration
|
| 397 |
+
# VIS_CONFIG = {
|
| 398 |
+
# 'iou_threshold': 0.3,
|
| 399 |
+
# 'min_segment_duration': 1.0,
|
| 400 |
+
# }
|
| 401 |
+
|
| 402 |
+
# # Hugging Face Dataset Configuration
|
| 403 |
+
# HF_DATASET_REPO = "Darknsu/EGTEA_Dataset"
|
| 404 |
+
# HF_DATASET_SUBFOLDER = "I3D" # Adjust this based on your dataset structure
|
| 405 |
+
|
| 406 |
+
# # Determine device
|
| 407 |
+
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 408 |
+
# print(f"Using device: {device}")
|
| 409 |
+
|
| 410 |
+
# # Create local cache directory for downloaded files
|
| 411 |
+
# CACHE_DIR = "./hf_cache"
|
| 412 |
+
# os.makedirs(CACHE_DIR, exist_ok=True)
|
| 413 |
+
|
| 414 |
+
# def download_npz_file(video_name: str) -> str:
|
| 415 |
+
# """
|
| 416 |
+
# Download .npz file from Hugging Face dataset repository
|
| 417 |
+
# Returns: Local path to the downloaded file
|
| 418 |
+
# """
|
| 419 |
+
# try:
|
| 420 |
+
# # Construct the file path in the dataset repo
|
| 421 |
+
# file_path = f"{video_name}.npz"
|
| 422 |
+
|
| 423 |
+
# # Check if file already exists in cache
|
| 424 |
+
# local_path = os.path.join(CACHE_DIR, f"{video_name}.npz")
|
| 425 |
+
# if os.path.exists(local_path):
|
| 426 |
+
# print(f"Using cached file: {local_path}")
|
| 427 |
+
# return local_path
|
| 428 |
+
|
| 429 |
+
# # Download from Hugging Face dataset
|
| 430 |
+
# print(f"Downloading {file_path} from {HF_DATASET_REPO}...")
|
| 431 |
+
# downloaded_path = hf_hub_download(
|
| 432 |
+
# repo_id=HF_DATASET_REPO,
|
| 433 |
+
# filename=file_path,
|
| 434 |
+
# repo_type="dataset",
|
| 435 |
+
# cache_dir=CACHE_DIR
|
| 436 |
+
# )
|
| 437 |
+
|
| 438 |
+
# # Copy to our expected location for easier access
|
| 439 |
+
# shutil.copy2(downloaded_path, local_path)
|
| 440 |
+
# print(f"File downloaded and cached: {local_path}")
|
| 441 |
+
# return local_path
|
| 442 |
+
|
| 443 |
+
# except Exception as e:
|
| 444 |
+
# raise Exception(f"Failed to download {video_name}.npz: {str(e)}")
|
| 445 |
+
|
| 446 |
+
# def get_available_videos_from_hf():
|
| 447 |
+
# """Get list of available videos from Hugging Face dataset repository"""
|
| 448 |
+
# try:
|
| 449 |
+
# print("Fetching available videos from Hugging Face dataset...")
|
| 450 |
+
# files = list_repo_files(
|
| 451 |
+
# repo_id=HF_DATASET_REPO,
|
| 452 |
+
# repo_type="dataset"
|
| 453 |
+
# )
|
| 454 |
+
|
| 455 |
+
# # Filter for .npz files in the I3D subfolder
|
| 456 |
+
# videos = []
|
| 457 |
+
# for file in files:
|
| 458 |
+
# if file.startswith(f"") and file.endswith('.npz'):
|
| 459 |
+
# # Extract the full filename without extension
|
| 460 |
+
# # For files like "I3D/OP02-R02-TurkeySandwich.npz"
|
| 461 |
+
# video_name = os.path.basename(file).replace('.npz', '')
|
| 462 |
+
# videos.append(video_name)
|
| 463 |
+
|
| 464 |
+
# videos = sorted(videos)
|
| 465 |
+
# print(f"Found {len(videos)} videos in dataset: {videos[:5]}{'...' if len(videos) > 5 else ''}")
|
| 466 |
+
# return videos
|
| 467 |
+
|
| 468 |
+
# except Exception as e:
|
| 469 |
+
# print(f"Error fetching videos from HF dataset: {str(e)}")
|
| 470 |
+
# return ["Error loading videos"]
|
| 471 |
+
|
| 472 |
+
# class HFVideoDataSet(VideoDataSet):
|
| 473 |
+
# """
|
| 474 |
+
# Modified VideoDataSet that downloads files from Hugging Face on demand
|
| 475 |
+
# """
|
| 476 |
+
# def __init__(self, opt, subset='test', video_name=None):
|
| 477 |
+
# # Store the original video_feature_all_test path
|
| 478 |
+
# self.original_feature_path = opt['video_feature_all_test']
|
| 479 |
+
|
| 480 |
+
# # Create temporary directory for this session
|
| 481 |
+
# self.temp_dir = tempfile.mkdtemp(prefix="hf_video_")
|
| 482 |
+
# print(f"Created temp directory: {self.temp_dir}")
|
| 483 |
+
|
| 484 |
+
# # Download the specific video file if video_name is provided
|
| 485 |
+
# if video_name:
|
| 486 |
+
# try:
|
| 487 |
+
# print(f"Downloading features for video: {video_name}")
|
| 488 |
+
# downloaded_path = download_npz_file(video_name)
|
| 489 |
+
|
| 490 |
+
# # Ensure the temp directory exists
|
| 491 |
+
# os.makedirs(self.temp_dir, exist_ok=True)
|
| 492 |
+
|
| 493 |
+
# # Copy to temp directory with expected structure - FIX: Add proper path separator
|
| 494 |
+
# temp_file_path = os.path.join(self.temp_dir, f"{video_name}.npz")
|
| 495 |
+
# print(f"Copying {downloaded_path} to {temp_file_path}")
|
| 496 |
+
# shutil.copy2(downloaded_path, temp_file_path)
|
| 497 |
+
|
| 498 |
+
# # Verify file exists and print debug info
|
| 499 |
+
# if not os.path.exists(temp_file_path):
|
| 500 |
+
# raise Exception(f"Failed to copy file to {temp_file_path}")
|
| 501 |
+
# else:
|
| 502 |
+
# print(f"Video file ready: {temp_file_path}")
|
| 503 |
+
# print(f"File size: {os.path.getsize(temp_file_path)} bytes")
|
| 504 |
+
|
| 505 |
+
# except Exception as e:
|
| 506 |
+
# print(f"Error downloading video {video_name}: {str(e)}")
|
| 507 |
+
# # Clean up temp directory on error
|
| 508 |
+
# if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
|
| 509 |
+
# shutil.rmtree(self.temp_dir)
|
| 510 |
+
# raise e
|
| 511 |
+
|
| 512 |
+
# # Set the feature path to our temp directory
|
| 513 |
+
# opt['video_feature_all_test'] = self.temp_dir
|
| 514 |
+
# print(f"Set video_feature_all_test to: {opt['video_feature_all_test']}")
|
| 515 |
+
|
| 516 |
+
# # Initialize parent class
|
| 517 |
+
# try:
|
| 518 |
+
# super().__init__(opt, subset, video_name)
|
| 519 |
+
# print(f"Successfully initialized dataset with {len(self.video_list)} videos")
|
| 520 |
+
# except Exception as e:
|
| 521 |
+
# print(f"Error initializing parent VideoDataSet: {str(e)}")
|
| 522 |
+
# # Clean up temp directory on error
|
| 523 |
+
# if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
|
| 524 |
+
# shutil.rmtree(self.temp_dir)
|
| 525 |
+
# raise e
|
| 526 |
+
|
| 527 |
+
# def __del__(self):
|
| 528 |
+
# # Clean up temporary directory
|
| 529 |
+
# try:
|
| 530 |
+
# if hasattr(self, 'temp_dir') and os.path.exists(self.temp_dir):
|
| 531 |
+
# shutil.rmtree(self.temp_dir)
|
| 532 |
+
# print(f"Cleaned up temp directory: {self.temp_dir}")
|
| 533 |
+
# except Exception as e:
|
| 534 |
+
# print(f"Warning: Could not clean up temp directory: {e}")
|
| 535 |
+
|
| 536 |
+
# def eval_frame(opt, model, dataset):
|
| 537 |
+
# """Evaluate model frame by frame"""
|
| 538 |
+
# try:
|
| 539 |
+
# test_loader = torch.utils.data.DataLoader(
|
| 540 |
+
# dataset,
|
| 541 |
+
# batch_size=opt['batch_size'],
|
| 542 |
+
# shuffle=False,
|
| 543 |
+
# num_workers=0,
|
| 544 |
+
# pin_memory=False
|
| 545 |
+
# )
|
| 546 |
+
|
| 547 |
+
# labels_cls = {video_name: [] for video_name in dataset.video_list}
|
| 548 |
+
# labels_reg = {video_name: [] for video_name in dataset.video_list}
|
| 549 |
+
# output_cls = {video_name: [] for video_name in dataset.video_list}
|
| 550 |
+
# output_reg = {video_name: [] for video_name in dataset.video_list}
|
| 551 |
+
|
| 552 |
+
# model.eval()
|
| 553 |
+
# with torch.no_grad():
|
| 554 |
+
# for n_iter, batch_data in enumerate(test_loader):
|
| 555 |
+
# try:
|
| 556 |
+
# if len(batch_data) == 4:
|
| 557 |
+
# input_data, cls_label, reg_label, _ = batch_data
|
| 558 |
+
# else:
|
| 559 |
+
# input_data, cls_label, reg_label = batch_data
|
| 560 |
+
|
| 561 |
+
# input_data = input_data.to(device)
|
| 562 |
+
# cls_label = cls_label.to(device) if cls_label is not None else None
|
| 563 |
+
# reg_label = reg_label.to(device) if reg_label is not None else None
|
| 564 |
+
|
| 565 |
+
# act_cls, act_reg, _ = model(input_data.float())
|
| 566 |
+
# act_cls = torch.softmax(act_cls, dim=-1)
|
| 567 |
+
|
| 568 |
+
# for b in range(input_data.size(0)):
|
| 569 |
+
# batch_idx = n_iter * opt['batch_size'] + b
|
| 570 |
+
# if batch_idx < len(dataset.inputs):
|
| 571 |
+
# video_name = dataset.inputs[batch_idx][0]
|
| 572 |
+
# output_cls[video_name].append(act_cls[b, :].detach().cpu().numpy())
|
| 573 |
+
# output_reg[video_name].append(act_reg[b, :].detach().cpu().numpy())
|
| 574 |
+
|
| 575 |
+
# if cls_label is not None:
|
| 576 |
+
# labels_cls[video_name].append(cls_label[b, :].cpu().numpy())
|
| 577 |
+
# if reg_label is not None:
|
| 578 |
+
# labels_reg[video_name].append(reg_label[b, :].cpu().numpy())
|
| 579 |
+
|
| 580 |
+
# except Exception as e:
|
| 581 |
+
# print(f"Error in batch {n_iter}: {str(e)}")
|
| 582 |
+
# continue
|
| 583 |
+
|
| 584 |
+
# # Stack arrays
|
| 585 |
+
# for video_name in dataset.video_list:
|
| 586 |
+
# if output_cls[video_name]:
|
| 587 |
+
# output_cls[video_name] = np.stack(output_cls[video_name], axis=0)
|
| 588 |
+
# output_reg[video_name] = np.stack(output_reg[video_name], axis=0)
|
| 589 |
+
# if labels_cls[video_name]:
|
| 590 |
+
# labels_cls[video_name] = np.stack(labels_cls[video_name], axis=0)
|
| 591 |
+
# if labels_reg[video_name]:
|
| 592 |
+
# labels_reg[video_name] = np.stack(labels_reg[video_name], axis=0)
|
| 593 |
+
|
| 594 |
+
# return output_cls, output_reg, labels_cls, labels_reg
|
| 595 |
+
|
| 596 |
+
# except Exception as e:
|
| 597 |
+
# print(f"Error in eval_frame: {str(e)}")
|
| 598 |
+
# raise e
|
| 599 |
+
|
| 600 |
+
# def eval_map_nms(opt, dataset, output_cls, output_reg):
|
| 601 |
+
# """Evaluate with Non-Maximum Suppression"""
|
| 602 |
+
# try:
|
| 603 |
+
# result_dict = {}
|
| 604 |
+
# anchors = opt['anchors']
|
| 605 |
+
|
| 606 |
+
# for video_name in dataset.video_list:
|
| 607 |
+
# if video_name not in output_cls or len(output_cls[video_name]) == 0:
|
| 608 |
+
# result_dict[video_name] = []
|
| 609 |
+
# continue
|
| 610 |
+
|
| 611 |
+
# duration = dataset.video_len[video_name]
|
| 612 |
+
# video_time = float(dataset.video_dict[video_name]["duration"])
|
| 613 |
+
# frame_to_time = 100.0 * video_time / duration
|
| 614 |
+
|
| 615 |
+
# proposal_dict = []
|
| 616 |
+
|
| 617 |
+
# for idx in range(min(duration, len(output_cls[video_name]))):
|
| 618 |
+
# cls_anc = output_cls[video_name][idx]
|
| 619 |
+
# reg_anc = output_reg[video_name][idx]
|
| 620 |
+
|
| 621 |
+
# for anc_idx in range(len(anchors)):
|
| 622 |
+
# if anc_idx >= len(cls_anc):
|
| 623 |
+
# continue
|
| 624 |
+
|
| 625 |
+
# cls = np.argwhere(cls_anc[anc_idx][:-1] > opt['threshold']).reshape(-1)
|
| 626 |
+
# if len(cls) == 0:
|
| 627 |
+
# continue
|
| 628 |
+
|
| 629 |
+
# ed = idx + anchors[anc_idx] * reg_anc[anc_idx][0]
|
| 630 |
+
# length = anchors[anc_idx] * np.exp(reg_anc[anc_idx][1])
|
| 631 |
+
# st = ed - length
|
| 632 |
+
|
| 633 |
+
# for cidx in range(len(cls)):
|
| 634 |
+
# label = cls[cidx]
|
| 635 |
+
# if label < len(dataset.label_name):
|
| 636 |
+
# tmp_dict = {
|
| 637 |
+
# "segment": [float(st * frame_to_time / 100.0), float(ed * frame_to_time / 100.0)],
|
| 638 |
+
# "score": float(cls_anc[anc_idx][label]),
|
| 639 |
+
# "label": dataset.label_name[label],
|
| 640 |
+
# "gentime": float(idx * frame_to_time / 100.0)
|
| 641 |
+
# }
|
| 642 |
+
# proposal_dict.append(tmp_dict)
|
| 643 |
+
|
| 644 |
+
# # Apply NMS
|
| 645 |
+
# proposal_dict = non_max_suppression(proposal_dict, overlapThresh=opt['soft_nms'])
|
| 646 |
+
# result_dict[video_name] = proposal_dict
|
| 647 |
+
|
| 648 |
+
# return result_dict
|
| 649 |
+
|
| 650 |
+
# except Exception as e:
|
| 651 |
+
# print(f"Error in eval_map_nms: {str(e)}")
|
| 652 |
+
# raise e
|
| 653 |
+
|
| 654 |
+
# def load_ground_truth(opt, video_name):
|
| 655 |
+
# """Load ground truth annotations if available"""
|
| 656 |
+
# gt_segments = []
|
| 657 |
+
# duration = 0
|
| 658 |
+
|
| 659 |
+
# try:
|
| 660 |
+
# video_anno_file = opt["video_anno"].format(opt["split"])
|
| 661 |
+
# if os.path.exists(video_anno_file):
|
| 662 |
+
# with open(video_anno_file, 'r') as f:
|
| 663 |
+
# anno_data = json.load(f)
|
| 664 |
+
|
| 665 |
+
# if video_name in anno_data['database']:
|
| 666 |
+
# gt_annotations = anno_data['database'][video_name]['annotations']
|
| 667 |
+
# duration = anno_data['database'][video_name]['duration']
|
| 668 |
+
|
| 669 |
+
# for anno in gt_annotations:
|
| 670 |
+
# start, end = anno['segment']
|
| 671 |
+
# gt_segments.append({
|
| 672 |
+
# 'label': anno['label'],
|
| 673 |
+
# 'start': start,
|
| 674 |
+
# 'end': end,
|
| 675 |
+
# 'duration': end - start
|
| 676 |
+
# })
|
| 677 |
+
# except Exception as e:
|
| 678 |
+
# print(f"Could not load ground truth: {str(e)}")
|
| 679 |
+
|
| 680 |
+
# return gt_segments, duration
|
| 681 |
+
|
| 682 |
+
# def process_video(video_name, split_number, progress=gr.Progress()):
|
| 683 |
+
# """Process a single video for action localization"""
|
| 684 |
+
# dataset = None # Initialize dataset variable
|
| 685 |
+
|
| 686 |
+
# try:
|
| 687 |
+
# if not video_name or video_name in ["Error: Could not load videos from HF dataset", "Error loading videos"]:
|
| 688 |
+
# return "Error: Please select a valid video name"
|
| 689 |
+
|
| 690 |
+
# progress(0.1, desc="Initializing...")
|
| 691 |
+
|
| 692 |
+
# # Parse options
|
| 693 |
+
# opt = opts.parse_opt()
|
| 694 |
+
# opt = vars(opt)
|
| 695 |
+
# opt['mode'] = 'test'
|
| 696 |
+
# opt['split'] = str(split_number)
|
| 697 |
+
# opt['checkpoint_path'] = './checkpoint'
|
| 698 |
+
# opt['video_feature_all_test'] = './data/I3D/' # This will be overridden by HFVideoDataSet
|
| 699 |
+
# opt['anchors'] = [int(item) for item in opt['anchors'].split(',')]
|
| 700 |
+
# opt['batch_size'] = 1
|
| 701 |
+
|
| 702 |
+
# progress(0.2, desc="Checking model checkpoint...")
|
| 703 |
+
|
| 704 |
+
# # Check if required files exist
|
| 705 |
+
# checkpoint_path = './checkpoint/01_ckp_best.pth.tar'
|
| 706 |
+
# if not os.path.exists(checkpoint_path):
|
| 707 |
+
# # Try alternative locations
|
| 708 |
+
# alt_paths = ['./01_ckp_best.pth.tar', '01_ckp_best.pth.tar']
|
| 709 |
+
# checkpoint_path = None
|
| 710 |
+
# for alt_path in alt_paths:
|
| 711 |
+
# if os.path.exists(alt_path):
|
| 712 |
+
# checkpoint_path = alt_path
|
| 713 |
+
# break
|
| 714 |
+
|
| 715 |
+
# if checkpoint_path is None:
|
| 716 |
+
# return "Error: Model checkpoint not found. Please ensure '01_ckp_best.pth.tar' is in the repository."
|
| 717 |
+
|
| 718 |
+
# progress(0.3, desc="Loading model...")
|
| 719 |
+
|
| 720 |
+
# # Load model
|
| 721 |
+
# model = MYNET(opt).to(device)
|
| 722 |
+
# checkpoint = torch.load(checkpoint_path, map_location=device)
|
| 723 |
+
|
| 724 |
+
# # Handle different checkpoint formats
|
| 725 |
+
# if 'state_dict' in checkpoint:
|
| 726 |
+
# model.load_state_dict(checkpoint['state_dict'])
|
| 727 |
+
# else:
|
| 728 |
+
# model.load_state_dict(checkpoint)
|
| 729 |
+
|
| 730 |
+
# model.eval()
|
| 731 |
+
# print("Model loaded successfully")
|
| 732 |
+
|
| 733 |
+
# progress(0.4, desc=f"Downloading video features for {video_name}...")
|
| 734 |
+
|
| 735 |
+
# # Create dataset with HF integration
|
| 736 |
+
# try:
|
| 737 |
+
# dataset = HFVideoDataSet(opt, subset='test', video_name=video_name)
|
| 738 |
+
# print(f"Dataset created successfully with {len(dataset.video_list)} videos")
|
| 739 |
+
# except Exception as e:
|
| 740 |
+
# error_msg = f"Error downloading or loading video '{video_name}': {str(e)}\n\nPlease check:\n1. Video name is correct\n2. File exists in HF dataset\n3. Network connection is stable"
|
| 741 |
+
# print(error_msg)
|
| 742 |
+
# return error_msg
|
| 743 |
+
|
| 744 |
+
# if len(dataset.video_list) == 0:
|
| 745 |
+
# return f"Error: No video found with name '{video_name}' in dataset after download"
|
| 746 |
+
|
| 747 |
+
# progress(0.6, desc="Running inference...")
|
| 748 |
+
|
| 749 |
+
# # Run inference
|
| 750 |
+
# try:
|
| 751 |
+
# output_cls, output_reg, labels_cls, labels_reg = eval_frame(opt, model, dataset)
|
| 752 |
+
# print("Inference completed successfully")
|
| 753 |
+
# except Exception as e:
|
| 754 |
+
# error_msg = f"Error during inference: {str(e)}"
|
| 755 |
+
# print(error_msg)
|
| 756 |
+
# return error_msg
|
| 757 |
+
|
| 758 |
+
# progress(0.8, desc="Processing results...")
|
| 759 |
+
|
| 760 |
+
# try:
|
| 761 |
+
# result_dict = eval_map_nms(opt, dataset, output_cls, output_reg)
|
| 762 |
+
# print("NMS processing completed")
|
| 763 |
+
# except Exception as e:
|
| 764 |
+
# error_msg = f"Error during NMS processing: {str(e)}"
|
| 765 |
+
# print(error_msg)
|
| 766 |
+
# return error_msg
|
| 767 |
+
|
| 768 |
+
# # Load ground truth
|
| 769 |
+
# gt_segments, duration = load_ground_truth(opt, video_name)
|
| 770 |
+
|
| 771 |
+
# # Process predictions
|
| 772 |
+
# pred_segments = []
|
| 773 |
+
# for pred in result_dict.get(video_name, []):
|
| 774 |
+
# start, end = pred['segment']
|
| 775 |
+
# pred_segments.append({
|
| 776 |
+
# 'label': pred['label'],
|
| 777 |
+
# 'start': start,
|
| 778 |
+
# 'end': end,
|
| 779 |
+
# 'duration': end - start,
|
| 780 |
+
# 'score': pred['score']
|
| 781 |
+
# })
|
| 782 |
+
|
| 783 |
+
# progress(0.9, desc="Generating output...")
|
| 784 |
+
|
| 785 |
+
# # Generate output text
|
| 786 |
+
# output_text = f"Predicted Actions for Video: {video_name}\n"
|
| 787 |
+
# output_text += "=" * 50 + "\n\n"
|
| 788 |
+
|
| 789 |
+
# if pred_segments:
|
| 790 |
+
# output_text += "PREDICTED ACTIONS:\n"
|
| 791 |
+
# output_text += "-" * 30 + "\n"
|
| 792 |
+
# for i, pred in enumerate(pred_segments, 1):
|
| 793 |
+
# output_text += f"{i}. {pred['label']}\n"
|
| 794 |
+
# output_text += f" Time: [{pred['start']:.2f}s - {pred['end']:.2f}s]\n"
|
| 795 |
+
# output_text += f" Duration: {pred['duration']:.2f}s\n"
|
| 796 |
+
# output_text += f" Confidence: {pred['score']:.3f}\n\n"
|
| 797 |
+
# else:
|
| 798 |
+
# output_text += "No actions detected above threshold.\n\n"
|
| 799 |
+
|
| 800 |
+
# # Add ground truth comparison if available
|
| 801 |
+
# if gt_segments:
|
| 802 |
+
# output_text += "\nGROUND TRUTH COMPARISON:\n"
|
| 803 |
+
# output_text += "-" * 30 + "\n"
|
| 804 |
+
|
| 805 |
+
# # Calculate basic metrics
|
| 806 |
+
# matched_count = 0
|
| 807 |
+
# total_pred = len(pred_segments)
|
| 808 |
+
# total_gt = len(gt_segments)
|
| 809 |
+
|
| 810 |
+
# for gt in gt_segments:
|
| 811 |
+
# output_text += f"GT: {gt['label']} [{gt['start']:.2f}s - {gt['end']:.2f}s]\n"
|
| 812 |
+
|
| 813 |
+
# # Find best matching prediction
|
| 814 |
+
# best_match = None
|
| 815 |
+
# best_iou = 0
|
| 816 |
+
# for pred in pred_segments:
|
| 817 |
+
# # Simple overlap calculation
|
| 818 |
+
# overlap_start = max(gt['start'], pred['start'])
|
| 819 |
+
# overlap_end = min(gt['end'], pred['end'])
|
| 820 |
+
# if overlap_end > overlap_start:
|
| 821 |
+
# overlap = overlap_end - overlap_start
|
| 822 |
+
# union = (gt['end'] - gt['start']) + (pred['end'] - pred['start']) - overlap
|
| 823 |
+
# iou = overlap / union if union > 0 else 0
|
| 824 |
+
# if iou > best_iou:
|
| 825 |
+
# best_iou = iou
|
| 826 |
+
# best_match = pred
|
| 827 |
+
|
| 828 |
+
# if best_match and best_iou > VIS_CONFIG['iou_threshold']:
|
| 829 |
+
# matched_count += 1
|
| 830 |
+
# output_text += f" β Matched with: {best_match['label']} (IoU: {best_iou:.3f})\n"
|
| 831 |
+
# else:
|
| 832 |
+
# output_text += f" β No match found\n"
|
| 833 |
+
# output_text += "\n"
|
| 834 |
+
|
| 835 |
+
# # Summary statistics
|
| 836 |
+
# precision = matched_count / total_pred if total_pred > 0 else 0
|
| 837 |
+
# recall = matched_count / total_gt if total_gt > 0 else 0
|
| 838 |
+
# f1 = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
|
| 839 |
+
|
| 840 |
+
# output_text += f"\nSUMMARY STATISTICS:\n"
|
| 841 |
+
# output_text += f"Total Predictions: {total_pred}\n"
|
| 842 |
+
# output_text += f"Total Ground Truth: {total_gt}\n"
|
| 843 |
+
# output_text += f"Matched: {matched_count}\n"
|
| 844 |
+
# output_text += f"Precision: {precision:.3f}\n"
|
| 845 |
+
# output_text += f"Recall: {recall:.3f}\n"
|
| 846 |
+
# output_text += f"F1-Score: {f1:.3f}\n"
|
| 847 |
+
|
| 848 |
+
# progress(1.0, desc="Complete!")
|
| 849 |
+
# print("Processing completed successfully")
|
| 850 |
+
# return output_text
|
| 851 |
+
|
| 852 |
+
# except Exception as e:
|
| 853 |
+
# error_details = traceback.format_exc()
|
| 854 |
+
# error_msg = f"Error processing video: {str(e)}\n\nDetailed error:\n{error_details}\n\nPlease check:\n1. Model checkpoint exists\n2. Video exists in HF dataset\n3. All dependencies are installed"
|
| 855 |
+
# print(error_msg)
|
| 856 |
+
# return error_msg
|
| 857 |
+
# finally:
|
| 858 |
+
# # Ensure cleanup happens even if there's an error
|
| 859 |
+
# if dataset is not None and hasattr(dataset, '__del__'):
|
| 860 |
+
# try:
|
| 861 |
+
# dataset.__del__()
|
| 862 |
+
# except Exception as e:
|
| 863 |
+
# print(f"Warning: Error during dataset cleanup: {e}")
|
| 864 |
+
|
| 865 |
+
# def refresh_video_list():
|
| 866 |
+
# """Refresh the list of available videos"""
|
| 867 |
+
# try:
|
| 868 |
+
# new_videos = get_available_videos_from_hf()
|
| 869 |
+
# return gr.Dropdown(choices=new_videos)
|
| 870 |
+
# except Exception as e:
|
| 871 |
+
# print(f"Error refreshing video list: {e}")
|
| 872 |
+
# return gr.Dropdown(choices=["Error refreshing videos"])
|
| 873 |
+
|
| 874 |
+
# # Initialize available videos
|
| 875 |
+
# print("Loading available videos from Hugging Face dataset...")
|
| 876 |
+
# try:
|
| 877 |
+
# available_videos = get_available_videos_from_hf()
|
| 878 |
+
# if not available_videos or available_videos == ["Error loading videos"]:
|
| 879 |
+
# available_videos = ["Error: Could not load videos from HF dataset"]
|
| 880 |
+
# except Exception as e:
|
| 881 |
+
# print(f"Error loading initial video list: {e}")
|
| 882 |
+
# available_videos = ["Error: Could not load videos from HF dataset"]
|
| 883 |
+
|
| 884 |
+
# print(f"Available videos: {len(available_videos)} videos found")
|
| 885 |
+
|
| 886 |
+
# # Gradio Interface
|
| 887 |
+
# with gr.Blocks(theme=gr.themes.Soft(), title="π¬ Temporal Action Localization") as iface:
|
| 888 |
+
# gr.Markdown("""
|
| 889 |
+
# # π¬ Temporal Action Localization
|
| 890 |
+
|
| 891 |
+
# This app performs temporal action localization on videos using I3D features loaded dynamically from Hugging Face datasets.
|
| 892 |
+
|
| 893 |
+
# **Features:**
|
| 894 |
+
# - β
Dynamic loading from HF dataset repository
|
| 895 |
+
# - β
Real-time inference with progress tracking
|
| 896 |
+
# - β
Ground truth comparison when available
|
| 897 |
+
# - β
Detailed action predictions with confidence scores
|
| 898 |
+
# """)
|
| 899 |
+
|
| 900 |
+
# with gr.Row():
|
| 901 |
+
# with gr.Column(scale=1):
|
| 902 |
+
# video_dropdown = gr.Dropdown(
|
| 903 |
+
# label="Select Video",
|
| 904 |
+
# choices=available_videos,
|
| 905 |
+
# value=available_videos[0] if available_videos and "Error" not in available_videos[0] else None,
|
| 906 |
+
# info="Videos loaded from Hugging Face dataset"
|
| 907 |
+
# )
|
| 908 |
+
|
| 909 |
+
# split_dropdown = gr.Dropdown(
|
| 910 |
+
# label="Split Number",
|
| 911 |
+
# choices=["1", "2", "3"],
|
| 912 |
+
# value="1",
|
| 913 |
+
# info="Dataset split for annotations"
|
| 914 |
+
# )
|
| 915 |
+
|
| 916 |
+
# refresh_btn = gr.Button("π Refresh Video List", variant="secondary")
|
| 917 |
+
# submit_btn = gr.Button("π Run Action Localization", variant="primary")
|
| 918 |
+
|
| 919 |
+
# with gr.Column(scale=2):
|
| 920 |
+
# output_text = gr.Textbox(
|
| 921 |
+
# label="Action Predictions",
|
| 922 |
+
# lines=25,
|
| 923 |
+
# max_lines=50,
|
| 924 |
+
# show_copy_button=True,
|
| 925 |
+
# placeholder="Results will appear here..."
|
| 926 |
+
# )
|
| 927 |
+
|
| 928 |
+
# gr.Markdown(f"""
|
| 929 |
+
# **Dataset Source:** [{HF_DATASET_REPO}](https://huggingface.co/datasets/{HF_DATASET_REPO})
|
| 930 |
+
|
| 931 |
+
# **Requirements:**
|
| 932 |
+
# - Model checkpoint: `01_ckp_best.pth.tar` in repository root
|
| 933 |
+
# - Video features: Automatically downloaded from HF dataset
|
| 934 |
+
# """)
|
| 935 |
+
|
| 936 |
+
# # Event handlers
|
| 937 |
+
# refresh_btn.click(
|
| 938 |
+
# fn=refresh_video_list,
|
| 939 |
+
# outputs=video_dropdown
|
| 940 |
+
# )
|
| 941 |
+
|
| 942 |
+
# submit_btn.click(
|
| 943 |
+
# fn=process_video,
|
| 944 |
+
# inputs=[video_dropdown, split_dropdown],
|
| 945 |
+
# outputs=output_text
|
| 946 |
+
# )
|
| 947 |
+
|
| 948 |
+
# # Example
|
| 949 |
+
# if available_videos and "Error" not in available_videos[0]:
|
| 950 |
+
# gr.Examples(
|
| 951 |
+
# examples=[[available_videos[0], "1"]],
|
| 952 |
+
# inputs=[video_dropdown, split_dropdown],
|
| 953 |
+
# fn=process_video,
|
| 954 |
+
# outputs=output_text,
|
| 955 |
+
# cache_examples=False
|
| 956 |
+
# )
|
| 957 |
+
|
| 958 |
+
# if __name__ == '__main__':
|
| 959 |
+
# print(f"Available videos: {len(available_videos)}")
|
| 960 |
+
# print(f"Using device: {device}")
|
| 961 |
+
# print(f"HF Dataset: {HF_DATASET_REPO}")
|
| 962 |
+
# iface.launch(
|
| 963 |
+
# server_name="0.0.0.0",
|
| 964 |
+
# server_port=7860,
|
| 965 |
+
# share=False
|
| 966 |
+
# )
|
| 967 |
+
|
| 968 |
+
|
| 969 |
+
|
| 970 |
+
|
| 971 |
+
|
| 972 |
+
|
| 973 |
+
|
| 974 |
+
|
| 975 |
+
|
| 976 |
+
|
| 977 |
+
|
| 978 |
+
|
| 979 |
+
|
| 980 |
+
|
| 981 |
+
|
| 982 |
+
|
| 983 |
+
|
| 984 |
+
|
| 985 |
+
|
| 986 |
+
|
| 987 |
|
| 988 |
import os
|
| 989 |
import json
|
|
|
|
| 1027 |
"""
|
| 1028 |
try:
|
| 1029 |
# Construct the file path in the dataset repo
|
| 1030 |
+
file_path = f"{HF_DATASET_SUBFOLDER}/{video_name}.npz"
|
| 1031 |
|
| 1032 |
# Check if file already exists in cache
|
| 1033 |
local_path = os.path.join(CACHE_DIR, f"{video_name}.npz")
|
|
|
|
| 1064 |
# Filter for .npz files in the I3D subfolder
|
| 1065 |
videos = []
|
| 1066 |
for file in files:
|
| 1067 |
+
if file.startswith(f"{HF_DATASET_SUBFOLDER}/") and file.endswith('.npz'):
|
| 1068 |
# Extract the full filename without extension
|
| 1069 |
# For files like "I3D/OP02-R02-TurkeySandwich.npz"
|
| 1070 |
video_name = os.path.basename(file).replace('.npz', '')
|