| import gradio as gr |
| from ultralytics import YOLO |
| import cv2 |
| import tempfile |
| import os |
|
|
| |
| model = YOLO('yolov8n.pt') |
|
|
| def count_people(video): |
| if video is None: |
| return "Please upload a video!" |
| |
| try: |
| |
| cap = cv2.VideoCapture(video) |
| total_detections = 0 |
| frame_count = 0 |
| |
| while cap.isOpened(): |
| ret, frame = cap.read() |
| if not ret: |
| break |
| |
| frame_count += 1 |
| |
| |
| if frame_count % 5 == 0: |
| |
| results = model(frame, classes=[0], verbose=False) |
| |
| |
| if len(results[0].boxes) > 0: |
| total_detections += len(results[0].boxes) |
| |
| cap.release() |
| |
| avg_people = total_detections / (frame_count / 5) if frame_count > 0 else 0 |
| |
| return f""" |
| π Detection Results: |
| - Total Frames Processed: {frame_count} |
| - Total Detections: {total_detections} |
| - Average People per Frame: {avg_people:.2f} |
| """ |
| |
| except Exception as e: |
| return f"Error processing video: {str(e)}" |
|
|
| |
| demo = gr.Interface( |
| fn=count_people, |
| inputs=gr.Video(label="Upload Video"), |
| outputs=gr.Textbox(label="Results"), |
| title="πͺ Guest Entry/Exit Management System", |
| description="Upload a video to detect and count people using YOLOv8", |
| examples=[], |
| theme=gr.themes.Soft() |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |