hb-setosys commited on
Commit
99a40c7
·
verified ·
1 Parent(s): f4239f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -65
app.py CHANGED
@@ -1,101 +1,86 @@
1
- # Install required libraries
2
- #!pip install gradio opencv-python-headless
3
-
4
- # Download YOLO files
5
- #!wget -nc https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg
6
- #!wget -nc https://pjreddie.com/media/files/yolov3.weights
7
- #!wget -nc https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names
8
-
9
  import gradio as gr
10
  import cv2
11
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def count_people(video_path):
14
- # Load YOLO model
15
- net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
16
-
17
- # Load class names
18
- with open('coco.names', 'r') as f:
19
- classes = [line.strip() for line in f.readlines()]
20
-
21
- # Open video
22
  cap = cv2.VideoCapture(video_path)
23
-
 
 
24
  frame_count = 0
25
- total_people_count = 0
26
  people_per_frame = []
27
-
28
- while cap.isOpened():
29
  ret, frame = cap.read()
30
  if not ret:
31
  break
32
-
33
  height, width, _ = frame.shape
34
-
35
- # Create blob from frame
36
  blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
37
  net.setInput(blob)
38
-
39
- # Get output layer names
40
- output_layers_names = net.getUnconnectedOutLayersNames()
41
-
42
  # Forward pass
43
  layer_outputs = net.forward(output_layers_names)
44
-
45
- # Lists to store detected people
46
- boxes = []
47
- confidences = []
48
-
49
  # Process detections
 
50
  for output in layer_outputs:
51
  for detection in output:
52
  scores = detection[5:]
53
  class_id = np.argmax(scores)
54
  confidence = scores[class_id]
55
-
56
- # Check if detected object is a person
57
  if classes[class_id] == 'person' and confidence > 0.5:
58
- # Object detected
59
- center_x = int(detection[0] * width)
60
- center_y = int(detection[1] * height)
61
- w = int(detection[2] * width)
62
- h = int(detection[3] * height)
63
-
64
- # Rectangle coordinates
65
- x = int(center_x - w/2)
66
- y = int(center_y - h/2)
67
-
68
  boxes.append([x, y, w, h])
69
  confidences.append(float(confidence))
70
-
71
- # Apply non-maximum suppression
72
- indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
73
-
74
  # Count people in this frame
75
- people_in_frame = len(indexes)
76
- people_per_frame.append(people_in_frame)
77
- total_people_count += people_in_frame
78
-
79
  frame_count += 1
80
-
81
- # Release resources
82
  cap.release()
83
-
84
- # Prepare analytics
85
  return {
86
- #'Total Frames Processed': frame_count,
87
- #'Total People Detected': total_people_count,
88
- #'Average People Per Frame': round(np.mean(people_per_frame), 2),
89
- 'People in a Video': int(np.max(people_per_frame)) #Max People in a Single Frame
90
  }
91
 
92
- # Define Gradio interface
93
  def analyze_video(video_file):
94
  result = count_people(video_file)
95
- result_str = "\n".join([f"{key}: {value}" for key, value in result.items()])
96
- return result_str
97
 
98
- # Gradio UI
99
  interface = gr.Interface(
100
  fn=analyze_video,
101
  inputs=gr.Video(label="Upload Video"),
@@ -104,5 +89,5 @@ interface = gr.Interface(
104
  description="Upload a video to detect and count people using YOLOv3."
105
  )
106
 
107
- # Launch Gradio app
108
  interface.launch()
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+ import os
5
+
6
+ # Load YOLO model
7
+ net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
8
+
9
+ # Enable GPU (if available)
10
+ net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
11
+ net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
12
+
13
+ # Load class names
14
+ with open('coco.names', 'r') as f:
15
+ classes = [line.strip() for line in f.readlines()]
16
+
17
+ # Get YOLO output layer names
18
+ output_layers_names = net.getUnconnectedOutLayersNames()
19
 
20
  def count_people(video_path):
21
+ if not os.path.exists(video_path):
22
+ return "Error: Video file not found."
23
+
 
 
 
 
 
24
  cap = cv2.VideoCapture(video_path)
25
+ if not cap.isOpened():
26
+ return "Error: Unable to open video file."
27
+
28
  frame_count = 0
 
29
  people_per_frame = []
30
+
31
+ while True:
32
  ret, frame = cap.read()
33
  if not ret:
34
  break
35
+
36
  height, width, _ = frame.shape
37
+
38
+ # Convert frame to YOLO format
39
  blob = cv2.dnn.blobFromImage(frame, 1/255.0, (416, 416), swapRB=True, crop=False)
40
  net.setInput(blob)
41
+
 
 
 
42
  # Forward pass
43
  layer_outputs = net.forward(output_layers_names)
44
+
 
 
 
 
45
  # Process detections
46
+ boxes, confidences = [], []
47
  for output in layer_outputs:
48
  for detection in output:
49
  scores = detection[5:]
50
  class_id = np.argmax(scores)
51
  confidence = scores[class_id]
52
+
 
53
  if classes[class_id] == 'person' and confidence > 0.5:
54
+ center_x, center_y = int(detection[0] * width), int(detection[1] * height)
55
+ w, h = int(detection[2] * width), int(detection[3] * height)
56
+ x, y = int(center_x - w / 2), int(center_y - h / 2)
57
+
 
 
 
 
 
 
58
  boxes.append([x, y, w, h])
59
  confidences.append(float(confidence))
60
+
61
+ # Apply Non-Maximum Suppression (NMS)
62
+ indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) if boxes else []
63
+
64
  # Count people in this frame
65
+ people_per_frame.append(len(indexes))
66
+
 
 
67
  frame_count += 1
68
+
 
69
  cap.release()
70
+
71
+ # Generate analytics
72
  return {
73
+ "Total Frames Processed": frame_count,
74
+ "Max People in a Single Frame": int(np.max(people_per_frame)) if people_per_frame else 0,
75
+ "Avg People Per Frame": round(np.mean(people_per_frame), 2) if people_per_frame else 0
 
76
  }
77
 
78
+ # Gradio UI function
79
  def analyze_video(video_file):
80
  result = count_people(video_file)
81
+ return "\n".join([f"{key}: {value}" for key, value in result.items()])
 
82
 
83
+ # Gradio Interface
84
  interface = gr.Interface(
85
  fn=analyze_video,
86
  inputs=gr.Video(label="Upload Video"),
 
89
  description="Upload a video to detect and count people using YOLOv3."
90
  )
91
 
92
+ # Launch app
93
  interface.launch()