Sanshruth commited on
Commit
36ab56f
·
verified ·
1 Parent(s): 04d2ef3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -83
app.py CHANGED
@@ -1,30 +1,28 @@
 
1
  import cv2
2
  import gradio as gr
3
  import numpy as np
4
  from PIL import Image, ImageDraw
5
  from ultralytics import YOLO
 
6
  import logging
7
- import threading
8
- import queue
9
  import time
 
10
 
11
  # Set up logging
12
  logging.basicConfig(level=logging.INFO)
13
  logger = logging.getLogger(__name__)
14
 
15
- # Global variables for line coordinates and line equation
16
  start_point = None
17
  end_point = None
18
- line_params = None # Stores (slope, intercept, start_point, end_point)
19
 
20
- # Low-resolution for inference
21
- LOW_RES = (320, 180)
22
-
23
- # Frame queue for processed frames
24
- frame_queue = queue.Queue(maxsize=30) # Adjust queue size based on memory constraints
25
-
26
- # Thread control flag
27
- processing_active = True
28
 
29
  def extract_first_frame(stream_url):
30
  """
@@ -56,27 +54,46 @@ def update_line(image, evt: gr.SelectData):
56
  """
57
  global start_point, end_point, line_params
58
 
 
59
  if start_point is None:
60
  start_point = (evt.index[0], evt.index[1])
 
 
61
  draw = ImageDraw.Draw(image)
62
- draw.ellipse((start_point[0] - 5, start_point[1] - 5, start_point[0] + 5, start_point[1] + 5), fill="blue", outline="blue")
 
 
 
 
63
  return image, f"Line Coordinates:\nStart: {start_point}, End: None"
64
 
 
65
  end_point = (evt.index[0], evt.index[1])
 
 
66
  if start_point[0] != end_point[0]: # Avoid division by zero
67
  slope = (end_point[1] - start_point[1]) / (end_point[0] - start_point[0])
68
  intercept = start_point[1] - slope * start_point[0]
69
- line_params = (slope, intercept, start_point, end_point)
70
  else:
 
71
  line_params = (float('inf'), start_point[0], start_point, end_point)
72
 
 
73
  draw = ImageDraw.Draw(image)
74
  draw.line([start_point, end_point], fill="red", width=2)
75
- draw.ellipse((end_point[0] - 5, end_point[1] - 5, end_point[0] + 5, end_point[1] + 5), fill="green", outline="green")
 
 
 
76
 
 
77
  line_info = f"Line Coordinates:\nStart: {start_point}, End: {end_point}\nLine Equation: y = {line_params[0]:.2f}x + {line_params[1]:.2f}"
 
 
78
  start_point = None
79
  end_point = None
 
80
  return image, line_info
81
 
82
  def reset_line():
@@ -89,19 +106,6 @@ def reset_line():
89
  line_params = None
90
  return None, "Line reset. Click to draw a new line."
91
 
92
- def is_object_crossing_line(box, line_params):
93
- """
94
- Determines if an object's bounding box is fully intersected by the user-drawn line.
95
- """
96
- _, _, line_start, line_end = line_params
97
- x1, y1, x2, y2 = box
98
- box_edges = [((x1, y1), (x2, y1)), ((x2, y1), (x2, y2)), ((x2, y2), (x1, y2)), ((x1, y2), (x1, y1))]
99
- intersection_count = 0
100
- for edge_start, edge_end in box_edges:
101
- if intersect(line_start, line_end, edge_start, edge_end):
102
- intersection_count += 1
103
- return intersection_count >= 2
104
-
105
  def intersect(A, B, C, D):
106
  """
107
  Determines if two line segments AB and CD intersect.
@@ -110,51 +114,54 @@ def intersect(A, B, C, D):
110
  return (C[1] - A[1]) * (B[0] - A[0]) - (B[1] - A[1]) * (C[0] - A[0])
111
 
112
  def on_segment(A, B, C):
113
- return min(A[0], B[0]) <= C[0] <= max(A[0], B[0]) and min(A[1], B[1]) <= C[1] <= max(A[1], B[1])
 
 
114
 
 
115
  ccw1 = ccw(A, B, C)
116
  ccw2 = ccw(A, B, D)
117
  ccw3 = ccw(C, D, A)
118
  ccw4 = ccw(C, D, B)
119
- return ((ccw1 * ccw2 < 0) and (ccw3 * ccw4 < 0)) or (ccw1 == 0 and on_segment(A, B, C)) or (ccw2 == 0 and on_segment(A, B, D)) or (ccw3 == 0 and on_segment(C, D, A)) or (ccw4 == 0 and on_segment(C, D, B))
120
 
121
- def process_frames(stream_url, confidence_threshold, selected_classes):
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  """
123
- Processes frames in a separate thread and adds them to the frame queue.
124
  """
125
- global processing_active, frame_queue
126
- cap = cv2.VideoCapture(stream_url)
127
- model = YOLO(model="yolo11n.pt")
128
- crossed_objects = {}
129
 
130
- while processing_active and cap.isOpened():
131
- ret, frame = cap.read()
132
- if not ret:
133
- break
134
 
135
- # Perform detection on low-res frame
136
- low_res_frame = cv2.resize(frame, LOW_RES)
137
- results = model.track(low_res_frame, persist=True, conf=confidence_threshold)
138
-
139
- # Scale bounding boxes to high-res
140
- scale_x = frame.shape[1] / LOW_RES[0]
141
- scale_y = frame.shape[0] / LOW_RES[1]
142
- for detection in results[0].boxes.data:
143
- x1, y1, x2, y2, conf, cls = detection
144
- x1, y1, x2, y2 = int(x1 * scale_x), int(y1 * scale_y), int(x2 * scale_x), int(y2 * scale_y)
145
- if is_object_crossing_line((x1, y1, x2, y2), line_params):
146
- crossed_objects[results[0].boxes.id.int().cpu().tolist()[0]] = True
147
-
148
- # Draw bounding boxes and line on the frame
149
- annotated_frame = results[0].plot()
150
- if line_params:
151
- draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
152
 
153
- # Add frame to the queue
154
- if not frame_queue.full():
155
- frame_queue.put(annotated_frame)
 
 
156
 
157
- cap.release()
 
158
 
159
  def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
160
  """
@@ -163,22 +170,102 @@ def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
163
  _, _, start_point, end_point = line_params
164
  cv2.line(image, start_point, end_point, color, thickness)
165
 
166
- def display_frames():
167
  """
168
- Displays frames from the queue at a consistent frame rate.
169
  """
170
- while processing_active:
171
- if not frame_queue.empty():
172
- frame = frame_queue.get()
173
- yield cv2.cvtColor(frame, cv2.COLOR_BGR2RGB), ""
174
- else:
175
- time.sleep(0.03) # Wait for the next frame
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
 
177
  # Define the Gradio interface
178
  with gr.Blocks() as demo:
179
  gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
180
  gr.Markdown("## https://github.com/SanshruthR/CCTV_SENTRY_YOLO11")
181
-
182
  # Step 1: Enter the IP Camera Stream URL
183
  stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s104.ipcamlive.com/streams/68idokwtondsqpmkr/stream.m3u8", visible=False)
184
 
@@ -188,36 +275,33 @@ with gr.Blocks() as demo:
188
  if first_frame is None:
189
  gr.Markdown(f"**Error:** {status}")
190
  else:
 
191
  image = gr.Image(value=first_frame, label="First Frame of Stream", type="pil")
 
192
  line_info = gr.Textbox(label="Line Coordinates", value="Line Coordinates:\nStart: None, End: None")
193
  image.select(update_line, inputs=image, outputs=[image, line_info])
194
 
195
  # Step 2: Select classes to detect
196
  gr.Markdown("### Step 2: Select Classes to Detect")
197
- model = YOLO(model="yolo11n.pt")
198
- class_names = list(model.names.values())
199
  selected_classes = gr.CheckboxGroup(choices=class_names, label="Select Classes to Detect")
200
 
201
- # Step 3: Adjust confidence threshold
202
  gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
203
  confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
204
 
205
  # Process the stream
206
  process_button = gr.Button("Process Stream")
 
 
207
  output_image = gr.Image(label="Processed Frame", streaming=True)
 
 
208
  error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
209
 
210
  # Event listener for processing the video
211
- process_button.click(
212
- fn=lambda: (setattr(globals(), "processing_active", True), threading.Thread(target=process_frames, args=(stream_url.value, confidence_threshold.value, selected_classes.value)).start()),
213
- outputs=None
214
- )
215
-
216
- # Display frames using a custom thread
217
- def start_display_thread():
218
- threading.Thread(target=display_frames, daemon=True).start()
219
-
220
- demo.load(start_display_thread, inputs=None, outputs=[output_image, error_box])
221
 
222
  # Launch the interface
223
  demo.launch(debug=True)
 
1
+ import multiprocessing
2
  import cv2
3
  import gradio as gr
4
  import numpy as np
5
  from PIL import Image, ImageDraw
6
  from ultralytics import YOLO
7
+ from ultralytics.utils.plotting import Annotator, colors
8
  import logging
9
+ import math
 
10
  import time
11
+ from collections import deque
12
 
13
  # Set up logging
14
  logging.basicConfig(level=logging.INFO)
15
  logger = logging.getLogger(__name__)
16
 
17
+ # Global variables to store line coordinates and line equation
18
  start_point = None
19
  end_point = None
20
+ line_params = None # Stores (slope, intercept) of the line
21
 
22
+ # Maximize CPU usage
23
+ cpu_cores = multiprocessing.cpu_count()
24
+ cv2.setNumThreads(cpu_cores)
25
+ logger.info(f"OpenCV using {cv2.getNumThreads()} threads out of {cpu_cores} available cores")
 
 
 
 
26
 
27
  def extract_first_frame(stream_url):
28
  """
 
54
  """
55
  global start_point, end_point, line_params
56
 
57
+ # If it's the first click, set the start point and show it on the image
58
  if start_point is None:
59
  start_point = (evt.index[0], evt.index[1])
60
+
61
+ # Draw the start point on the image
62
  draw = ImageDraw.Draw(image)
63
+ draw.ellipse(
64
+ (start_point[0] - 5, start_point[1] - 5, start_point[0] + 5, start_point[1] + 5),
65
+ fill="blue", outline="blue"
66
+ )
67
+
68
  return image, f"Line Coordinates:\nStart: {start_point}, End: None"
69
 
70
+ # If it's the second click, set the end point and draw the line
71
  end_point = (evt.index[0], evt.index[1])
72
+
73
+ # Calculate the slope (m) and intercept (b) of the line: y = mx + b
74
  if start_point[0] != end_point[0]: # Avoid division by zero
75
  slope = (end_point[1] - start_point[1]) / (end_point[0] - start_point[0])
76
  intercept = start_point[1] - slope * start_point[0]
77
+ line_params = (slope, intercept, start_point, end_point) # Store slope, intercept, and points
78
  else:
79
+ # Vertical line (special case)
80
  line_params = (float('inf'), start_point[0], start_point, end_point)
81
 
82
+ # Draw the line and end point on the image
83
  draw = ImageDraw.Draw(image)
84
  draw.line([start_point, end_point], fill="red", width=2)
85
+ draw.ellipse(
86
+ (end_point[0] - 5, end_point[1] - 5, end_point[0] + 5, end_point[1] + 5),
87
+ fill="green", outline="green"
88
+ )
89
 
90
+ # Return the updated image and line info
91
  line_info = f"Line Coordinates:\nStart: {start_point}, End: {end_point}\nLine Equation: y = {line_params[0]:.2f}x + {line_params[1]:.2f}"
92
+
93
+ # Reset the points for the next interaction
94
  start_point = None
95
  end_point = None
96
+
97
  return image, line_info
98
 
99
  def reset_line():
 
106
  line_params = None
107
  return None, "Line reset. Click to draw a new line."
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  def intersect(A, B, C, D):
110
  """
111
  Determines if two line segments AB and CD intersect.
 
114
  return (C[1] - A[1]) * (B[0] - A[0]) - (B[1] - A[1]) * (C[0] - A[0])
115
 
116
  def on_segment(A, B, C):
117
+ if min(A[0], B[0]) <= C[0] <= max(A[0], B[0]) and min(A[1], B[1]) <= C[1] <= max(A[1], B[1]):
118
+ return True
119
+ return False
120
 
121
+ # Check if the line segments intersect
122
  ccw1 = ccw(A, B, C)
123
  ccw2 = ccw(A, B, D)
124
  ccw3 = ccw(C, D, A)
125
  ccw4 = ccw(C, D, B)
 
126
 
127
+ if ((ccw1 * ccw2 < 0) and (ccw3 * ccw4 < 0)):
128
+ return True
129
+ elif ccw1 == 0 and on_segment(A, B, C):
130
+ return True
131
+ elif ccw2 == 0 and on_segment(A, B, D):
132
+ return True
133
+ elif ccw3 == 0 and on_segment(C, D, A):
134
+ return True
135
+ elif ccw4 == 0 and on_segment(C, D, B):
136
+ return True
137
+ else:
138
+ return False
139
+
140
+ def is_object_crossing_line(box, line_params):
141
  """
142
+ Determines if an object's bounding box is fully intersected by the user-drawn line.
143
  """
144
+ _, _, line_start, line_end = line_params
 
 
 
145
 
146
+ # Get the bounding box coordinates
147
+ x1, y1, x2, y2 = box
 
 
148
 
149
+ # Define the four edges of the bounding box
150
+ box_edges = [
151
+ ((x1, y1), (x2, y1)), # Top edge
152
+ ((x2, y1), (x2, y2)), # Right edge
153
+ ((x2, y2), (x1, y2)), # Bottom edge
154
+ ((x1, y2), (x1, y1)) # Left edge
155
+ ]
 
 
 
 
 
 
 
 
 
 
156
 
157
+ # Count the number of intersections between the line and the bounding box edges
158
+ intersection_count = 0
159
+ for edge_start, edge_end in box_edges:
160
+ if intersect(line_start, line_end, edge_start, edge_end):
161
+ intersection_count += 1
162
 
163
+ # Only count the object if the line intersects the bounding box at least twice
164
+ return intersection_count >= 2
165
 
166
  def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
167
  """
 
170
  _, _, start_point, end_point = line_params
171
  cv2.line(image, start_point, end_point, color, thickness)
172
 
173
+ def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
174
  """
175
+ Processes the IP camera stream to count objects of the selected classes crossing the line.
176
  """
177
+ global line_params
178
+
179
+ errors = []
180
+
181
+ if line_params is None:
182
+ errors.append("Error: No line drawn. Please draw a line on the first frame.")
183
+ if selected_classes is None or len(selected_classes) == 0:
184
+ errors.append("Error: No classes selected. Please select at least one class to detect.")
185
+ if stream_url is None or stream_url.strip() == "":
186
+ errors.append("Error: No stream URL provided.")
187
+
188
+ if errors:
189
+ return None, "\n".join(errors)
190
+
191
+ logger.info("Connecting to the IP camera stream...")
192
+ cap = cv2.VideoCapture(stream_url)
193
+ if not cap.isOpened():
194
+ errors.append("Error: Could not open stream.")
195
+ return None, "\n".join(errors)
196
+
197
+ model = YOLO(model="yolov8n.pt")
198
+ crossed_objects = {}
199
+ max_tracked_objects = 1000 # Maximum number of objects to track before clearing
200
+
201
+ # Queue to hold frames for processing
202
+ frame_queue = deque(maxlen=10)
203
+
204
+ logger.info("Starting to process the stream...")
205
+ while cap.isOpened():
206
+ ret, frame = cap.read()
207
+ if not ret:
208
+ errors.append("Error: Could not read frame from the stream.")
209
+ break
210
+
211
+ # Add frame to the queue
212
+ frame_queue.append(frame)
213
+
214
+ # Process frames in the queue
215
+ if len(frame_queue) > 0:
216
+ process_frame = frame_queue.popleft()
217
+
218
+ # Perform object tracking with confidence threshold
219
+ results = model.track(process_frame, persist=True, conf=confidence_threshold)
220
+
221
+ if results[0].boxes.id is not None:
222
+ track_ids = results[0].boxes.id.int().cpu().tolist()
223
+ clss = results[0].boxes.cls.cpu().tolist()
224
+ boxes = results[0].boxes.xyxy.cpu()
225
+ confs = results[0].boxes.conf.cpu().tolist()
226
+
227
+ for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
228
+ if conf >= confidence_threshold and model.names[cls] in selected_classes:
229
+ # Check if the object crosses the line
230
+ if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
231
+ crossed_objects[t_id] = True
232
+
233
+ # Clear the dictionary if it gets too large
234
+ if len(crossed_objects) > max_tracked_objects:
235
+ crossed_objects.clear()
236
+
237
+ # Visualize the results with bounding boxes, masks, and IDs
238
+ annotated_frame = results[0].plot()
239
+
240
+ # Draw the angled line on the frame
241
+ draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
242
+
243
+ # Display the count on the frame with a modern look
244
+ count = len(crossed_objects)
245
+ (text_width, text_height), _ = cv2.getTextSize(f"COUNT: {count}", cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
246
+
247
+ # Calculate the position for the middle of the top
248
+ margin = 10 # Margin from the top
249
+ x = (annotated_frame.shape[1] - text_width) // 2 # Center-align the text horizontally
250
+ y = text_height + margin # Top-align the text
251
+
252
+ # Draw the black background rectangle
253
+ cv2.rectangle(annotated_frame, (x - margin, y - text_height - margin), (x + text_width + margin, y + margin), (0, 0, 0), -1)
254
+
255
+ # Draw the text
256
+ cv2.putText(annotated_frame, f"COUNT: {count}", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
257
+
258
+ # Yield the annotated frame to Gradio
259
+ yield annotated_frame, ""
260
+
261
+ cap.release()
262
+ logger.info("Stream processing completed.")
263
 
264
  # Define the Gradio interface
265
  with gr.Blocks() as demo:
266
  gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
267
  gr.Markdown("## https://github.com/SanshruthR/CCTV_SENTRY_YOLO11")
268
+
269
  # Step 1: Enter the IP Camera Stream URL
270
  stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s104.ipcamlive.com/streams/68idokwtondsqpmkr/stream.m3u8", visible=False)
271
 
 
275
  if first_frame is None:
276
  gr.Markdown(f"**Error:** {status}")
277
  else:
278
+ # Image component for displaying the first frame
279
  image = gr.Image(value=first_frame, label="First Frame of Stream", type="pil")
280
+
281
  line_info = gr.Textbox(label="Line Coordinates", value="Line Coordinates:\nStart: None, End: None")
282
  image.select(update_line, inputs=image, outputs=[image, line_info])
283
 
284
  # Step 2: Select classes to detect
285
  gr.Markdown("### Step 2: Select Classes to Detect")
286
+ model = YOLO(model="yolov8n.pt") # Load the model to get class names
287
+ class_names = list(model.names.values()) # Get class names
288
  selected_classes = gr.CheckboxGroup(choices=class_names, label="Select Classes to Detect")
289
 
290
+ # Step 3: Adjust confidence threshold
291
  gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
292
  confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
293
 
294
  # Process the stream
295
  process_button = gr.Button("Process Stream")
296
+
297
+ # Output image for real-time frame rendering
298
  output_image = gr.Image(label="Processed Frame", streaming=True)
299
+
300
+ # Error box to display warnings/errors
301
  error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
302
 
303
  # Event listener for processing the video
304
+ process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url], outputs=[output_image, error_box])
 
 
 
 
 
 
 
 
 
305
 
306
  # Launch the interface
307
  demo.launch(debug=True)