File size: 2,285 Bytes
0a9b429
 
 
 
 
 
 
 
 
 
c3cdf4b
 
 
0a9b429
c3cdf4b
0a9b429
 
 
 
 
c3cdf4b
0a9b429
 
 
 
 
 
 
 
 
 
 
 
 
 
c3cdf4b
0a9b429
 
 
 
 
c3cdf4b
 
 
 
0a9b429
 
c3cdf4b
 
0a9b429
 
 
 
fe49260
 
0a9b429
 
 
 
c3cdf4b
0a9b429
 
 
 
 
 
c3cdf4b
 
 
0a9b429
 
 
 
c3cdf4b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import cv2
import numpy as np
import torch
import gradio as gr
from ultralytics import YOLO

# Load YOLOv12x model
MODEL_PATH = "yolov12x.pt"  # Ensure the model is uploaded to the Hugging Face Space
model = YOLO(MODEL_PATH)

# COCO dataset class IDs
PERSON_CLASS_ID = 0  # "person"
TRUCK_CLASS_ID = 7    # "truck"

def count_objects(video_path):
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        return "Error: Unable to open video file."

    frame_count = 0
    object_counts = {"people": [], "trucks": []}
    frame_skip = 5  # Process every 5th frame for efficiency

    while True:
        ret, frame = cap.read()
        if not ret:
            break  # End of video

        frame_count += 1
        if frame_count % frame_skip != 0:
            continue  # Skip frames to improve efficiency

        # Run YOLOv12x inference
        results = model(frame, verbose=False)

        people_count, truck_count = 0, 0
        for result in results:
            for box in result.boxes:
                class_id = int(box.cls.item())  # Get class ID
                confidence = float(box.conf.item())  # Get confidence score

                # Count objects based on their class IDs
                if class_id == PERSON_CLASS_ID and confidence > 0.5:
                    people_count += 1
                elif class_id == TRUCK_CLASS_ID and confidence > 0.5:
                    truck_count += 1

        object_counts["people"].append(people_count)
        object_counts["trucks"].append(truck_count)

    cap.release()

    return {
        #"Max People in a Frame": int(np.max(object_counts["people"])) if object_counts["people"] else 0,
        "Truck Count": int(np.max(object_counts["trucks"])) if object_counts["trucks"] else 0
    }

# Gradio UI function
def analyze_video(video_file):
    result = count_objects(video_file)
    return "\n".join([f"{key}: {value}" for key, value in result.items()])

# Define Gradio interface
iface = gr.Interface(
    fn=analyze_video,
    inputs=gr.Video(label="Upload Video"),
    outputs=gr.Textbox(label="Analysis Result"),
    title="YOLOv12x Object Counter",
    description="Upload a video to count people and trucks using YOLOv12x."
)

# Launch the Gradio app
if __name__ == "__main__":
    iface.launch()