CoolFace
Apppublic

7Atchaya6102005/Video_Classification

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.py78 linesDownload Raw Back to root
1import gradio as gr2import cv23import tempfile,os4from ultralytics import YOLO5from datetime import datetime6import torch7torch.set_grad_enabled(False)8model=YOLO("yolov8n.pt")9def video_object_detection(video_path,progress=gr.Progress()):10    cap=cv2.VideoCapture(video_path)11    W,H=416,23412    fps=int(cap.get(cv2.CAP_PROP_FPS)) or 1513    total=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))14    temp_dir=tempfile.mkdtemp()15    out_path=os.path.join(temp_dir,f"detected_{datetime.now().strftime('%H%M%S')}.mp4")16    out=cv2.VideoWriter(out_path,cv2.VideoWriter_fourcc(*"mp4v"),fps,(W,H))17    frame_skip=2018    frame_id=019    last_boxes=[]20    last_labels=[]21    progress(0,desc="Processing video...")22    while True:23        ret,frame=cap.read()24        if not ret:25            break26        frame=cv2.resize(frame,(W,H))27        if frame_id % frame_skip==0:28            result=model(frame,conf=0.4,verbose=False)29            last_boxes=[]30            last_labels=[]31            if results[0].boxes is not None:32                boxes=result[0].boxes.xyxy.cpu().numpy()33                classes = results[0].boxes.cls.cpu().numpy()34                names = results[0].names  # class id → label35 36                for i, box in enumerate(boxes):37                    last_boxes.append(box)38                    last_labels.append(names[int(classes[i])])39 40            # Draw boxes and labels41            for i, box in enumerate(last_boxes):42                x1, y1, x2, y2 = map(int, box)43                label = last_labels[i]44 45                 # Draw rectangle46                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)47 48                # Draw label background49                ((text_w, text_h), _) = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)50                cv2.rectangle(frame, (x1, y1 - 20), (x1 + text_w, y1), (0, 255, 0), -1)51 52                # Put label text53                cv2.putText(frame, label, (x1, y1 - 5),54                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)55 56                out.write(frame)57                if total > 0:58                    progress(frame_id / total)59                frame_id += 160 61                cap.release()62                out.release()63                progress(1.0, desc="Completed")64                return out_path65 66                # Gradio Interface67                demo = gr.Interface(68                    fn=video_object_detection,69                    inputs=gr.Video(label="Upload video (≤1 min, 480p recommended)"),70                    outputs=gr.Video(label="Detected Video"),71                    title="YOLOv8 Video Object Detection with Labels",72                    description="CPU-safe • Stable UI • Prints object labels"73                )74 75                demo.queue()76                demo.launch()77 78