zainulabedin949's picture
Update app.py
40dd3f1 verified
import gradio as gr
from ultralytics import YOLO
import cv2
import tempfile
import os
# Model load
model = YOLO('yolov8n.pt')
def count_people(video):
if video is None:
return "Please upload a video!"
try:
# Video process karo
cap = cv2.VideoCapture(video)
total_detections = 0
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# Har 5th frame process karo (speed ke liye)
if frame_count % 5 == 0:
# Person detect karo
results = model(frame, classes=[0], verbose=False)
# Count detections
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)}"
# Gradio interface
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()