Download app.py from samir-mohamed/Equipment-Utilization-Activity-Classifier: direct link, hf CLI and curl.
- Browser
- Download file 7.11 kB
-
https://huggingface.co/spaces/samir-mohamed/Equipment-Utilization-Activity-Classifier/resolve/main/app.py
- Command line
-
hf download hf://spaces/samir-mohamed/Equipment-Utilization-Activity-Classifier/app.py
-
curl -L -o app.py https://huggingface.co/spaces/samir-mohamed/Equipment-Utilization-Activity-Classifier/resolve/main/app.py
7.11 kB
| from __future__ import annotations | |
| import os | |
| import zipfile | |
| from datetime import datetime | |
| from pathlib import Path | |
| from typing import List | |
| import gradio as gr | |
| import pandas as pd | |
| import torch | |
| from script.pipeline import build_argparser, run_pipeline | |
| RUNS_ROOT = Path("/tmp/euac_runs") | |
| RUNS_ROOT.mkdir(parents=True, exist_ok=True) | |
| DEFAULT_ALLOWED_CLASSES = [ | |
| "backhoe_loader", | |
| "cement_truck", | |
| "compactor", | |
| "dozer", | |
| "dump_truck", | |
| "excavator", | |
| "grader", | |
| "mobile_crane", | |
| "tower_crane", | |
| "wheel_loader", | |
| ] | |
| WEIGHT_OPTIONS = [ | |
| "best.pt", | |
| "yolov8n.pt", | |
| "yolov8s.pt", | |
| "yolov8m.pt", | |
| ] | |
| DEFAULT_WEIGHT = "best.pt" | |
| def parse_allowed_classes(value: str) -> List[str]: | |
| if not value or not value.strip(): | |
| return DEFAULT_ALLOWED_CLASSES | |
| items = [chunk.strip() for chunk in value.split(",")] | |
| return [name for name in items if name] | |
| def build_args( | |
| input_video: str, | |
| output_dir: Path, | |
| weights: str, | |
| conf: float, | |
| imgsz: int, | |
| process_every_n_frames: int, | |
| allowed_classes: List[str], | |
| ) -> object: | |
| parser = build_argparser() | |
| device = "0" if torch.cuda.is_available() else "cpu" | |
| cli_args = [ | |
| "--input_video", | |
| input_video, | |
| "--output_dir", | |
| str(output_dir), | |
| "--weights", | |
| weights, | |
| "--device", | |
| device, | |
| "--imgsz", | |
| str(imgsz), | |
| "--conf", | |
| f"{conf:.2f}", | |
| "--process_every_n_frames", | |
| str(process_every_n_frames), | |
| "--max_equipment_per_frame", | |
| "10", | |
| "--min_track_hits", | |
| "4", | |
| "--summary_min_tracked_seconds", | |
| "1.0", | |
| ] | |
| if allowed_classes: | |
| cli_args.extend(["--allowed_class_names", *allowed_classes]) | |
| return parser.parse_args(cli_args) | |
| def bundle_outputs(artifacts: dict, bundle_path: Path) -> str: | |
| with zipfile.ZipFile(bundle_path, "w", compression=zipfile.ZIP_DEFLATED) as archive: | |
| for key in ("annotated_video", "events_jsonl", "summary_csv"): | |
| file_path = Path(artifacts[key]) | |
| if file_path.exists(): | |
| archive.write(file_path, arcname=file_path.name) | |
| return str(bundle_path) | |
| def run_equipment_pipeline( | |
| video_path: str, | |
| weights: str, | |
| conf: float, | |
| imgsz: int, | |
| process_every_n_frames: int, | |
| allowed_classes_text: str, | |
| ): | |
| if not video_path or not os.path.exists(video_path): | |
| raise gr.Error("Please upload a valid input video.") | |
| run_id = datetime.utcnow().strftime("%Y%m%d_%H%M%S_%f") | |
| run_dir = RUNS_ROOT / run_id | |
| output_dir = run_dir / "outputs" | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| allowed_classes = parse_allowed_classes(allowed_classes_text) | |
| args = build_args( | |
| input_video=video_path, | |
| output_dir=output_dir, | |
| weights=weights, | |
| conf=conf, | |
| imgsz=imgsz, | |
| process_every_n_frames=process_every_n_frames, | |
| allowed_classes=allowed_classes, | |
| ) | |
| artifacts = run_pipeline(args) | |
| summary_path = Path(artifacts["summary_csv"]) | |
| if summary_path.exists() and summary_path.stat().st_size > 0: | |
| summary_df = pd.read_csv(summary_path) | |
| else: | |
| summary_df = pd.DataFrame( | |
| columns=[ | |
| "equipment_id", | |
| "equipment_class", | |
| "total_tracked_seconds", | |
| "total_active_seconds", | |
| "total_idle_seconds", | |
| "utilization_percent", | |
| ] | |
| ) | |
| bundle_path = run_dir / "space_outputs.zip" | |
| bundle_file = bundle_outputs(artifacts, bundle_path) | |
| status = ( | |
| "Run completed successfully. " | |
| f"Detected equipment rows in summary: {len(summary_df)}." | |
| ) | |
| return ( | |
| artifacts["annotated_video"], | |
| summary_df, | |
| artifacts["events_jsonl"], | |
| artifacts["summary_csv"], | |
| bundle_file, | |
| status, | |
| ) | |
| def build_ui() -> gr.Blocks: | |
| with gr.Blocks(title="Equipment Utilization & Activity Classification") as demo: | |
| gr.Markdown( | |
| "# Equipment Utilization & Activity Classification\n" | |
| "Upload a video to run detection, tracking, utilization analytics, and activity classification." | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| input_video = gr.Video( | |
| sources=["upload"], | |
| label="Input Video", | |
| ) | |
| with gr.Column(scale=1): | |
| weights = gr.Dropdown( | |
| choices=WEIGHT_OPTIONS, | |
| value=DEFAULT_WEIGHT, | |
| label="Model Weights", | |
| ) | |
| conf = gr.Slider( | |
| minimum=0.10, | |
| maximum=0.70, | |
| value=0.25, | |
| step=0.01, | |
| label="Detection Confidence", | |
| ) | |
| imgsz = gr.Slider( | |
| minimum=640, | |
| maximum=1280, | |
| value=960, | |
| step=32, | |
| label="Inference Image Size", | |
| ) | |
| process_every_n_frames = gr.Slider( | |
| minimum=1, | |
| maximum=6, | |
| value=2, | |
| step=1, | |
| label="Process Every N Frames", | |
| ) | |
| allowed_classes = gr.Textbox( | |
| value=", ".join(DEFAULT_ALLOWED_CLASSES), | |
| label="Allowed Class Names (comma-separated)", | |
| lines=2, | |
| ) | |
| run_btn = gr.Button("Run Pipeline", variant="primary") | |
| with gr.Row(): | |
| annotated_video = gr.Video(label="Annotated Output Video") | |
| summary_df = gr.Dataframe(label="Summary CSV Preview") | |
| with gr.Row(): | |
| events_file = gr.File(label="events.jsonl") | |
| summary_file = gr.File(label="summary.csv") | |
| bundle_file = gr.File(label="All Outputs (zip)") | |
| status_text = gr.Textbox(label="Run Status") | |
| run_btn.click( | |
| fn=run_equipment_pipeline, | |
| inputs=[ | |
| input_video, | |
| weights, | |
| conf, | |
| imgsz, | |
| process_every_n_frames, | |
| allowed_classes, | |
| ], | |
| outputs=[ | |
| annotated_video, | |
| summary_df, | |
| events_file, | |
| summary_file, | |
| bundle_file, | |
| status_text, | |
| ], | |
| ) | |
| gr.Markdown( | |
| "### Notes\n" | |
| "- For faster runtime on free CPU Spaces, upload short clips.\n" | |
| "- If GPU is available in the Space hardware, it will be used automatically." | |
| ) | |
| return demo | |
| demo = build_ui() | |
| if __name__ == "__main__": | |
| demo.queue(max_size=8).launch() | |