zainulabedin949 commited on
Commit
40dd3f1
·
verified ·
1 Parent(s): 051e8d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -21
app.py CHANGED
@@ -1,38 +1,62 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
  import cv2
 
 
4
 
5
- # Model load (automatic download hoga)
6
  model = YOLO('yolov8n.pt')
7
 
8
  def count_people(video):
9
- # Video process karo
10
- cap = cv2.VideoCapture(video)
11
- entry_count = 0
12
 
13
- while cap.isOpened():
14
- ret, frame = cap.read()
15
- if not ret:
16
- break
 
17
 
18
- # Person detect karo
19
- results = model.track(frame, classes=[0], persist=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- # Counting logic (simple version)
22
- if results[0].boxes:
23
- entry_count = len(results[0].boxes)
 
 
 
 
 
 
 
24
 
25
- cap.release()
26
- return f"Total People Detected: {entry_count}"
27
 
28
  # Gradio interface
29
  demo = gr.Interface(
30
  fn=count_people,
31
- inputs=gr.Video(),
32
- outputs="text",
33
- title="Guest Entry/Exit Management System",
34
- description="Upload a video to count people"
 
 
35
  )
36
 
37
- demo.launch()
38
- ```
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
  import cv2
4
+ import tempfile
5
+ import os
6
 
7
+ # Model load
8
  model = YOLO('yolov8n.pt')
9
 
10
  def count_people(video):
11
+ if video is None:
12
+ return "Please upload a video!"
 
13
 
14
+ try:
15
+ # Video process karo
16
+ cap = cv2.VideoCapture(video)
17
+ total_detections = 0
18
+ frame_count = 0
19
 
20
+ while cap.isOpened():
21
+ ret, frame = cap.read()
22
+ if not ret:
23
+ break
24
+
25
+ frame_count += 1
26
+
27
+ # Har 5th frame process karo (speed ke liye)
28
+ if frame_count % 5 == 0:
29
+ # Person detect karo
30
+ results = model(frame, classes=[0], verbose=False)
31
+
32
+ # Count detections
33
+ if len(results[0].boxes) > 0:
34
+ total_detections += len(results[0].boxes)
35
 
36
+ cap.release()
37
+
38
+ avg_people = total_detections / (frame_count / 5) if frame_count > 0 else 0
39
+
40
+ return f"""
41
+ 📊 Detection Results:
42
+ - Total Frames Processed: {frame_count}
43
+ - Total Detections: {total_detections}
44
+ - Average People per Frame: {avg_people:.2f}
45
+ """
46
 
47
+ except Exception as e:
48
+ return f"Error processing video: {str(e)}"
49
 
50
  # Gradio interface
51
  demo = gr.Interface(
52
  fn=count_people,
53
+ inputs=gr.Video(label="Upload Video"),
54
+ outputs=gr.Textbox(label="Results"),
55
+ title="🚪 Guest Entry/Exit Management System",
56
+ description="Upload a video to detect and count people using YOLOv8",
57
+ examples=[],
58
+ theme=gr.themes.Soft()
59
  )
60
 
61
+ if __name__ == "__main__":
62
+ demo.launch()