Sanshruth commited on
Commit
29901d7
·
verified ·
1 Parent(s): 38a38c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -239
app.py CHANGED
@@ -1,6 +1,4 @@
1
- ##############
2
-
3
- #Maximize CPU usage
4
  import multiprocessing
5
  import cv2
6
 
@@ -14,7 +12,7 @@ cv2.setNumThreads(cpu_cores)
14
  print(f"OpenCV using {cv2.getNumThreads()} threads out of {cpu_cores} available cores")
15
 
16
  ##############
17
- import cv2
18
  import gradio as gr
19
  import numpy as np
20
  from PIL import Image, ImageDraw
@@ -32,284 +30,158 @@ start_point = None
32
  end_point = None
33
  line_params = None # Stores (slope, intercept) of the line
34
 
 
 
 
 
 
 
 
 
 
 
 
35
  def extract_first_frame(stream_url):
36
- """
37
- Extracts the first available frame from the IP camera stream and returns it as a PIL image.
38
- """
39
- logger.info("Attempting to extract the first frame from the stream...")
40
  cap = cv2.VideoCapture(stream_url)
41
  if not cap.isOpened():
42
- logger.error("Error: Could not open stream.")
43
  return None, "Error: Could not open stream."
44
 
45
  ret, frame = cap.read()
46
  cap.release()
47
 
48
  if not ret:
49
- logger.error("Error: Could not read the first frame.")
50
- return None, "Error: Could not read the first frame."
51
 
52
- # Convert the frame to a PIL image
53
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
54
- pil_image = Image.fromarray(frame_rgb)
55
-
56
- logger.info("First frame extracted successfully.")
57
- return pil_image, "First frame extracted successfully."
58
 
59
  def update_line(image, evt: gr.SelectData):
60
- """
61
- Updates the line based on user interaction (click and drag).
62
- """
63
  global start_point, end_point, line_params
64
 
65
- # If it's the first click, set the start point and show it on the image
66
  if start_point is None:
67
  start_point = (evt.index[0], evt.index[1])
68
-
69
- # Draw the start point on the image
70
  draw = ImageDraw.Draw(image)
71
- draw.ellipse(
72
- (start_point[0] - 5, start_point[1] - 5, start_point[0] + 5, start_point[1] + 5),
73
- fill="blue", outline="blue"
74
- )
75
 
76
- return image, f"Line Coordinates:\nStart: {start_point}, End: None"
77
-
78
- # If it's the second click, set the end point and draw the line
79
  end_point = (evt.index[0], evt.index[1])
 
 
 
 
80
 
81
- # Calculate the slope (m) and intercept (b) of the line: y = mx + b
82
- if start_point[0] != end_point[0]: # Avoid division by zero
83
  slope = (end_point[1] - start_point[1]) / (end_point[0] - start_point[0])
84
  intercept = start_point[1] - slope * start_point[0]
85
- line_params = (slope, intercept, start_point, end_point) # Store slope, intercept, and points
86
  else:
87
- # Vertical line (special case)
88
  line_params = (float('inf'), start_point[0], start_point, end_point)
89
 
90
- # Draw the line and end point on the image
91
- draw = ImageDraw.Draw(image)
92
- draw.line([start_point, end_point], fill="red", width=2)
93
- draw.ellipse(
94
- (end_point[0] - 5, end_point[1] - 5, end_point[0] + 5, end_point[1] + 5),
95
- fill="green", outline="green"
96
- )
97
-
98
- # Return the updated image and line info
99
- line_info = f"Line Coordinates:\nStart: {start_point}, End: {end_point}\nLine Equation: y = {line_params[0]:.2f}x + {line_params[1]:.2f}"
100
-
101
- # Reset the points for the next interaction
102
  start_point = None
103
- end_point = None
104
-
105
- return image, line_info
106
 
107
- def reset_line():
108
- """
109
- Resets the line coordinates.
110
- """
111
- global start_point, end_point, line_params
112
- start_point = None
113
- end_point = None
114
- line_params = None
115
- return None, "Line reset. Click to draw a new line."
116
-
117
- def intersect(A, B, C, D):
118
- """
119
- Determines if two line segments AB and CD intersect.
120
- """
121
- def ccw(A, B, C):
122
- return (C[1] - A[1]) * (B[0] - A[0]) - (B[1] - A[1]) * (C[0] - A[0])
123
-
124
- def on_segment(A, B, C):
125
- 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]):
126
- return True
127
- return False
128
-
129
- # Check if the line segments intersect
130
- ccw1 = ccw(A, B, C)
131
- ccw2 = ccw(A, B, D)
132
- ccw3 = ccw(C, D, A)
133
- ccw4 = ccw(C, D, B)
134
-
135
- if ((ccw1 * ccw2 < 0) and (ccw3 * ccw4 < 0)):
136
- return True
137
- elif ccw1 == 0 and on_segment(A, B, C):
138
- return True
139
- elif ccw2 == 0 and on_segment(A, B, D):
140
- return True
141
- elif ccw3 == 0 and on_segment(C, D, A):
142
- return True
143
- elif ccw4 == 0 and on_segment(C, D, B):
144
- return True
145
- else:
146
- return False
147
-
148
- def is_object_crossing_line(box, line_params):
149
- """
150
- Determines if an object's bounding box is fully intersected by the user-drawn line.
151
- """
152
- _, _, line_start, line_end = line_params
153
-
154
- # Get the bounding box coordinates
155
- x1, y1, x2, y2 = box
156
-
157
- # Define the four edges of the bounding box
158
- box_edges = [
159
- ((x1, y1), (x2, y1)), # Top edge
160
- ((x2, y1), (x2, y2)), # Right edge
161
- ((x2, y2), (x1, y2)), # Bottom edge
162
- ((x1, y2), (x1, y1)) # Left edge
163
- ]
164
-
165
- # Count the number of intersections between the line and the bounding box edges
166
- intersection_count = 0
167
- for edge_start, edge_end in box_edges:
168
- if intersect(line_start, line_end, edge_start, edge_end):
169
- intersection_count += 1
170
-
171
- # Only count the object if the line intersects the bounding box at least twice
172
- return intersection_count >= 2
173
-
174
- def draw_angled_line(image, line_params, color=(0, 255, 0), thickness=2):
175
- """
176
- Draws the user-defined line on the frame.
177
- """
178
- _, _, start_point, end_point = line_params
179
- cv2.line(image, start_point, end_point, color, thickness)
180
 
181
  def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
182
- """
183
- Processes the IP camera stream to count objects of the selected classes crossing the line.
184
- """
185
  global line_params
186
 
187
- errors = []
188
-
189
- if line_params is None:
190
- errors.append("Error: No line drawn. Please draw a line on the first frame.")
191
- if selected_classes is None or len(selected_classes) == 0:
192
- errors.append("Error: No classes selected. Please select at least one class to detect.")
193
- if stream_url is None or stream_url.strip() == "":
194
- errors.append("Error: No stream URL provided.")
195
 
196
- if errors:
197
- return None, "\n".join(errors)
198
-
199
- logger.info("Connecting to the IP camera stream...")
200
  cap = cv2.VideoCapture(stream_url)
201
  if not cap.isOpened():
202
- errors.append("Error: Could not open stream.")
203
- return None, "\n".join(errors)
204
-
205
- model = YOLO(model="yolo11n.pt")
206
- crossed_objects = {}
207
- max_tracked_objects = 1000 # Maximum number of objects to track before clearing
208
 
209
- logger.info("Starting to process the stream...")
210
  while cap.isOpened():
211
  ret, frame = cap.read()
212
  if not ret:
213
- errors.append("Error: Could not read frame from the stream.")
214
  break
215
-
216
- # Perform object tracking with confidence threshold
217
- results = model.track(frame, persist=True, conf=confidence_threshold)
218
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  if results[0].boxes.id is not None:
220
- track_ids = results[0].boxes.id.int().cpu().tolist()
221
- clss = results[0].boxes.cls.cpu().tolist()
222
- boxes = results[0].boxes.xyxy.cpu()
223
- confs = results[0].boxes.conf.cpu().tolist()
224
-
225
- for box, cls, t_id, conf in zip(boxes, clss, track_ids, confs):
226
- if conf >= confidence_threshold and model.names[cls] in selected_classes:
227
- # Check if the object crosses the line
228
- if is_object_crossing_line(box, line_params) and t_id not in crossed_objects:
229
- crossed_objects[t_id] = True
230
-
231
- # Clear the dictionary if it gets too large
232
- if len(crossed_objects) > max_tracked_objects:
233
- crossed_objects.clear()
234
-
235
- # Visualize the results with bounding boxes, masks, and IDs
236
  annotated_frame = results[0].plot()
237
-
238
- # Draw the angled line on the frame
239
- draw_angled_line(annotated_frame, line_params, color=(0, 255, 0), thickness=2)
240
-
241
- # Display the count on the frame with a modern look
242
- count = len(crossed_objects)
243
- (text_width, text_height), _ = cv2.getTextSize(f"COUNT: {count}", cv2.FONT_HERSHEY_SIMPLEX, 1, 2)
244
-
245
- # Calculate the position for the middle of the top
246
- margin = 10 # Margin from the top
247
- x = (annotated_frame.shape[1] - text_width) // 2 # Center-align the text horizontally
248
- y = text_height + margin # Top-align the text
249
-
250
- # Draw the black background rectangle
251
- cv2.rectangle(annotated_frame, (x - margin, y - text_height - margin), (x + text_width + margin, y + margin), (0, 0, 0), -1)
252
-
253
- # Draw the text
254
- cv2.putText(annotated_frame, f"COUNT: {count}", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
255
-
256
- # Yield the annotated frame to Gradio
257
  yield annotated_frame, ""
258
 
259
  cap.release()
260
- logger.info("Stream processing completed.")
261
-
262
- # Define the Gradio interface
263
- with gr.Blocks() as demo:
264
- gr.Markdown("<h1>Real-time monitoring, object tracking, and line-crossing detection for CCTV camera streams.</h1></center>")
265
- gr.Markdown("## https://github.com/SanshruthR/CCTV_SENTRY_YOLO11")
266
-
267
-
268
-
269
- # Step 1: Enter the IP Camera Stream URL
270
- # gr.Markdown("### Step 0: Enter the IP Camera Stream URL")
271
- # stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s103.ipcamlive.com/streams/67n4ojknye7lkxpmf/stream.m3u8", visible=False)
272
- stream_url = gr.Textbox(label="Enter IP Camera Stream URL", value="https://s104.ipcamlive.com/streams/68idokwtondsqpmkr/stream.m3u8", visible=False)
273
-
274
- # Step 1: Extract the first frame from the stream
275
- gr.Markdown("### Step 1: Click on the frame to draw a line, the objects crossing it would be counted in real-time.")
276
- first_frame, status = extract_first_frame(stream_url.value)
277
- if first_frame is None:
278
- gr.Markdown(f"**Error:** {status}")
279
- else:
280
- # Image component for displaying the first frame
281
- image = gr.Image(value=first_frame, label="First Frame of Stream", type="pil")
282
-
283
-
284
- line_info = gr.Textbox(label="Line Coordinates", value="Line Coordinates:\nStart: None, End: None")
285
- image.select(update_line, inputs=image, outputs=[image, line_info])
286
-
287
- # Reset the line (optional)
288
- # gr.Markdown("### Step 4: Reset the Line (Optional)")
289
- # reset_button = gr.Button("Reset Line")
290
- # reset_button.click(reset_line, inputs=None, outputs=[image, line_info])
291
-
292
- # Step 2: Select classes to detect
293
- gr.Markdown("### Step 2: Select Classes to Detect")
294
- model = YOLO(model="yolo11n.pt") # Load the model to get class names
295
- class_names = list(model.names.values()) # Get class names
296
- selected_classes = gr.CheckboxGroup(choices=class_names, label="Select Classes to Detect")
297
-
298
- # Step 3: Adjust confidence threshold
299
- gr.Markdown("### Step 3: Adjust Confidence Threshold (Optional)")
300
- confidence_threshold = gr.Slider(minimum=0.0, maximum=1.0, value=0.2, label="Confidence Threshold")
301
-
302
- #process the stream
303
- process_button = gr.Button("Process Stream")
304
-
305
- # Output image for real-time frame rendering
306
- output_image = gr.Image(label="Processed Frame", streaming=True)
307
-
308
- # Error box to display warnings/errors
309
- error_box = gr.Textbox(label="Errors/Warnings", interactive=False)
310
-
311
- # Event listener for processing the video
312
- process_button.click(process_video, inputs=[confidence_threshold, selected_classes, stream_url], outputs=[output_image, error_box])
313
 
314
- # Launch the interface
315
- demo.launch(debug=True)
 
1
+ # Maximize CPU usage and GPU utilization
 
 
2
  import multiprocessing
3
  import cv2
4
 
 
12
  print(f"OpenCV using {cv2.getNumThreads()} threads out of {cpu_cores} available cores")
13
 
14
  ##############
15
+ import torch
16
  import gradio as gr
17
  import numpy as np
18
  from PIL import Image, ImageDraw
 
30
  end_point = None
31
  line_params = None # Stores (slope, intercept) of the line
32
 
33
+ # Initialize model once
34
+ model = YOLO('yolov8n.pt') # Use smaller model if needed
35
+ # Check for GPU availability
36
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
37
+ model.to(device)
38
+ logger.info(f"Using device: {device}")
39
+
40
+ # Video processing parameters
41
+ FRAME_SKIP = 1 # Process every nth frame
42
+ FRAME_SCALE = 0.5 # Scale factor for input frames
43
+
44
  def extract_first_frame(stream_url):
45
+ """Extracts the first available frame from the IP camera stream."""
46
+ logger.info("Extracting first frame...")
 
 
47
  cap = cv2.VideoCapture(stream_url)
48
  if not cap.isOpened():
49
+ logger.error("Could not open stream.")
50
  return None, "Error: Could not open stream."
51
 
52
  ret, frame = cap.read()
53
  cap.release()
54
 
55
  if not ret:
56
+ logger.error("Could not read frame.")
57
+ return None, "Error: Could not read frame."
58
 
 
59
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
60
+ return Image.fromarray(frame_rgb), "First frame extracted."
 
 
 
61
 
62
  def update_line(image, evt: gr.SelectData):
63
+ """Updates the line based on user interaction."""
 
 
64
  global start_point, end_point, line_params
65
 
 
66
  if start_point is None:
67
  start_point = (evt.index[0], evt.index[1])
 
 
68
  draw = ImageDraw.Draw(image)
69
+ draw.ellipse((start_point[0]-5, start_point[1]-5, start_point[0]+5, start_point[1]+5),
70
+ fill="blue", outline="blue")
71
+ return image, f"Line Start: {start_point}"
 
72
 
 
 
 
73
  end_point = (evt.index[0], evt.index[1])
74
+ draw = ImageDraw.Draw(image)
75
+ draw.line([start_point, end_point], fill="red", width=2)
76
+ draw.ellipse((end_point[0]-5, end_point[1]-5, end_point[0]+5, end_point[1]+5),
77
+ fill="green", outline="green")
78
 
79
+ # Calculate line parameters
80
+ if start_point[0] != end_point[0]:
81
  slope = (end_point[1] - start_point[1]) / (end_point[0] - start_point[0])
82
  intercept = start_point[1] - slope * start_point[0]
83
+ line_params = (slope, intercept, start_point, end_point)
84
  else:
 
85
  line_params = (float('inf'), start_point[0], start_point, end_point)
86
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  start_point = None
88
+ return image, f"Line: {line_params[0]:.2f}x + {line_params[1]:.2f}"
 
 
89
 
90
+ def optimized_intersection_check(box, line_params):
91
+ """Optimized line-box intersection check using vector math."""
92
+ _, _, (x1, y1), (x2, y2) = line_params
93
+ box_x1, box_y1, box_x2, box_y2 = box
94
+
95
+ # Convert line to parametric form
96
+ dx = x2 - x1
97
+ dy = y2 - y1
98
+
99
+ # Check if any box edge intersects the line
100
+ t_near = -float('inf')
101
+ t_far = float('inf')
102
+
103
+ for i in range(2):
104
+ if dx == 0 and dy == 0:
105
+ continue
106
+
107
+ if i == 0: # X-axis
108
+ t0 = (box_x1 - x1) / dx if dx != 0 else 0
109
+ t1 = (box_x2 - x1) / dx if dx != 0 else 0
110
+ else: # Y-axis
111
+ t0 = (box_y1 - y1) / dy if dy != 0 else 0
112
+ t1 = (box_y2 - y1) / dy if dy != 0 else 0
113
+
114
+ t_min = min(t0, t1)
115
+ t_max = max(t0, t1)
116
+
117
+ if t_min > t_near: t_near = t_min
118
+ if t_max < t_far: t_far = t_max
119
+
120
+ return t_near <= t_far and t_near <= 1 and t_far >= 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  def process_video(confidence_threshold=0.5, selected_classes=None, stream_url=None):
123
+ """Optimized video processing pipeline."""
 
 
124
  global line_params
125
 
126
+ # Validation checks
127
+ if not line_params or not selected_classes or not stream_url:
128
+ return None, "Missing configuration parameters"
 
 
 
 
 
129
 
130
+ # Convert to set for faster lookups
131
+ selected_classes = set(selected_classes)
132
+
133
+ # Video capture setup
134
  cap = cv2.VideoCapture(stream_url)
135
  if not cap.isOpened():
136
+ return None, "Error opening stream"
137
+
138
+ crossed_objects = set()
139
+ frame_count = 0
 
 
140
 
 
141
  while cap.isOpened():
142
  ret, frame = cap.read()
143
  if not ret:
 
144
  break
145
+
146
+ frame_count += 1
147
+ if frame_count % FRAME_SKIP != 0:
148
+ continue
149
+
150
+ # Preprocess frame
151
+ frame = cv2.resize(frame, None, fx=FRAME_SCALE, fy=FRAME_SCALE)
152
+
153
+ # Object detection
154
+ results = model.track(
155
+ frame,
156
+ persist=True,
157
+ conf=confidence_threshold,
158
+ verbose=False,
159
+ device=device,
160
+ tracker="botsort.yaml" # Use optimized tracker config
161
+ )
162
+
163
+ # Process detections
164
  if results[0].boxes.id is not None:
165
+ boxes = results[0].boxes.xyxy.cpu().numpy()
166
+ track_ids = results[0].boxes.id.int().cpu().numpy()
167
+ classes = results[0].boxes.cls.cpu().numpy()
168
+
169
+ for box, track_id, cls in zip(boxes, track_ids, classes):
170
+ if model.names[int(cls)] not in selected_classes:
171
+ continue
172
+
173
+ if optimized_intersection_check(box, line_params) and track_id not in crossed_objects:
174
+ crossed_objects.add(track_id)
175
+ if len(crossed_objects) > 1000:
176
+ crossed_objects.clear()
177
+
178
+ # Annotation
 
 
179
  annotated_frame = results[0].plot()
180
+ cv2.line(annotated_frame, line_params[2], line_params[3], (0,255,0), 2)
181
+ cv2.putText(annotated_frame, f"COUNT: {len(crossed_objects)}",
182
+ (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
183
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  yield annotated_frame, ""
185
 
186
  cap.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187