Spaces:
Sleeping
Sleeping
File size: 19,006 Bytes
f69cf27 b94851e f69cf27 b94851e f69cf27 b94851e f69cf27 b94851e f69cf27 b94851e f69cf27 b94851e f69cf27 95aa979 f69cf27 44960ec f69cf27 44960ec f69cf27 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | import cv2
import numpy as np
import gradio as gr
import time
from collections import deque
import matplotlib.pyplot as plt
from ultralytics import YOLO
import os
# Dummy comment to test push
def compare_images_optical_flow(img1, img2):
"""
Compares two images and returns a grayscale image of flow magnitude normalized to 0 - 1.
Args:
Returns:
A grayscale image of flow magnitude normalized to 0 - 1, or None if an error occurs.
"""
# Convert images to grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# Calculate optical flow using Farneback method
flow = cv2.calcOpticalFlowFarneback(gray1, gray2, None, 0.5, 3, 15, 3, 5, 1.2, 0)
# Calculate the magnitude of the optical flow
flow_magnitude = np.sqrt(flow[..., 0]**2 + flow[..., 1]**2)
# # Normalize the magnitude to the range 0-1
# flow_magnitude_normalized = cv2.normalize(flow_magnitude, None, 0, 1, cv2.NORM_MINMAX, cv2.CV_32F)
#The output is already a grayscale image. No need to convert it.
return flow_magnitude
model = YOLO("yolov8s-world.pt")
# Define custom classes
CUSTOM_CLASSES = ["one bird", "one airplane", "one kite","a flying object","sky"]
model.set_classes(CUSTOM_CLASSES)
def detect_birds(image):
results = model(image,
conf = 0.1,
verbose=False,
)
return results[0].plot()
optical_flow_runtime = []
object_detection_runtime = []
change_detection_runtime = []
example_videos_folder = "./example_videos"
EXAMPLE_VIDEOS_LIST = os.listdir(example_videos_folder)
EXAMPLE_VIDEOS_LIST = [os.path.join(example_videos_folder, v)
for v in EXAMPLE_VIDEOS_LIST]
HEIGHT_STANDARD = 480
WIDTH_STANDARD = 640
frame_stack = deque(maxlen=2)
detection_stack = deque(maxlen=1)
fall_back_frame = np.zeros((256, 256, 3), dtype=np.uint8) + 127
flow_magnitude_normalized = np.zeros((256, 256), dtype=np.uint8)
FLAGS = {
"OBJECT_DETECTING": False,
}
CAP = []
# Function to compute optical flow
def compute_optical_flow(mean_norm = None):
global FLAGS, flow_magnitude_normalized, frame_stack
if mean_norm is None:
mean_norm = .4
else:
mean_norm = float(mean_norm)
FLAGS["OBJECT_DETECTING"] = False
while True:
if (len(frame_stack) > 1) and not(FLAGS["OBJECT_DETECTING"]): #
prev_frame, curr_frame = frame_stack
original_height, original_width = curr_frame.shape[:2]
start_time = time.time() # Start timing
prev_frame_resized, curr_frame_resized = [
cv2.resize(
frame,
(original_width // 4, original_height // 4)
) for frame in [prev_frame, curr_frame]
]
flow_magnitude = compare_images_optical_flow(prev_frame_resized,
curr_frame_resized)
end_time = time.time() # End timing
optical_flow_runtime.append(end_time - start_time) # Append the elapsed time
flow_magnitude_normalized = cv2.normalize(flow_magnitude, None, 0, 1, cv2.NORM_MINMAX, cv2.CV_32F)
flow_magnitude_normalized = cv2.resize(
flow_magnitude_normalized,
(original_width, original_height)
)
yield flow_magnitude_normalized
if flow_magnitude_normalized.mean() < mean_norm:
detection_stack.append((curr_frame,prev_frame, flow_magnitude_normalized))
else:
yield np.stack((flow_magnitude_normalized,flow_magnitude_normalized*0, flow_magnitude_normalized*0), axis=-1)
# Function to perform object detection
def object_detection_stream(classes = ""):
if classes.strip() == "":
classes = "one bird, one airplane, one kite,a flying object,sky"
classes_list = classes.split(",")
global FLAGS, fall_back_frame, model
model.set_classes(classes_list)
detected_frame = fall_back_frame.copy()
while True:
if len(detection_stack)>0:
FLAGS["OBJECT_DETECTING"] = True
curr_frame, prev_frame, flow_magnitude_normalized = detection_stack.pop()
frame = curr_frame
start_time = time.time() # Start timing
detected_frame = detect_birds(frame)
end_time = time.time() # End timing
object_detection_runtime.append(end_time - start_time) # Append the elapsed time
FLAGS["OBJECT_DETECTING"] = False
yield detected_frame
FLAGS["OBJECT_DETECTING"] = False
def change_detection_stream(useless_var = None):
detected_frame = fall_back_frame.copy()
while True:
if len(detection_stack)>0:
FLAGS["OBJECT_DETECTING"] = True
curr_frame, prev_frame, flow_magnitude_normalized = detection_stack.pop()
frame = curr_frame
start_time = time.time() # Start timing
ret, thresh = cv2.threshold((flow_magnitude_normalized*255).astype(np.uint8),
127, 255, 0)
contours_tuple= cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours_tuple[0] if len(contours_tuple) == 2 else contours_tuple[1]
detected_frame = frame.copy()
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(detected_frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
end_time = time.time() # End timing
change_detection_runtime.append(end_time - start_time) # Append the elapsed time
FLAGS["OBJECT_DETECTING"] = False
yield detected_frame
FLAGS["OBJECT_DETECTING"] = False
def video_stream(frame_rate = ""):
if frame_rate.strip() == "":
frame_rate = 2.0
else:
frame_rate = float(frame_rate)
if len(CAP) > 0:
while True:
cap = cv2.VideoCapture(CAP[-1])
ret, frame = cap.read()
while ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_stack.append(
cv2.resize(
frame,
(WIDTH_STANDARD, HEIGHT_STANDARD) # Resize the frame
)
)
yield frame
ret, frame = cap.read()
time.sleep(1/frame_rate)
else:
yield fall_back_frame
def yield_frame(s):
while True:
yield frame_stack[0]
def video_stream_HIKvision(video_address, frame_rate = ""):
if frame_rate.strip() == "":
frame_rate = 2.0
else:
frame_rate = float(frame_rate)
cap = cv2.VideoCapture(video_address, cv2.CAP_FFMPEG)
while True:
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_stack.append(
cv2.resize(
frame,
(WIDTH_STANDARD, HEIGHT_STANDARD) # Resize the frame
)
)
yield frame
ret, frame = cap.read()
time.sleep(1/frame_rate)
else:
yield fall_back_frame
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("### Birds Detection Real Time (suitable for CPU Embedded Systems)")
with gr.Tab("Using a custom Video"):
with gr.Row():
with gr.Column():
with gr.Row():
video = gr.Video(label="Video Source")
with gr.Row():
examples = gr.Examples(
examples=EXAMPLE_VIDEOS_LIST,
inputs=[video],
)
with gr.Column():
webcam_img = gr.Interface(video_stream,
inputs=gr.Textbox(label="Acquisition: Enter the frame rate", value = 2.0), #
outputs="image")
with gr.Row():
with gr.Column():
optical_flow_img = gr.Interface(compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs=gr.Image(),#,"image",
)
with gr.Column():
detection_img = gr.Interface(object_detection_stream,
inputs=gr.Textbox(label="Classes: Enter the classes", value = "one bird, one airplane, one kite,a flying object,sky"),
outputs="image")
video.change(
fn=lambda video: CAP.append(video),
inputs=[video],
)
with gr.Tab("Using a custom Video (Change Detection)"):
with gr.Row():
with gr.Column():
with gr.Row():
video_CD = gr.Video(label="Video Source")
with gr.Row():
examples_CD = gr.Examples(
examples=EXAMPLE_VIDEOS_LIST,
inputs=[video_CD],
)
with gr.Column():
webcam_img_CD = gr.Interface(video_stream,
inputs=gr.Textbox(label="Acquisition: Enter the frame rate", value = 2.0), #
outputs="image")
with gr.Row():
with gr.Column():
optical_flow_img_CD = gr.Interface(compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs=gr.Image(),#,"image",
)
with gr.Column():
detection_img_CD = gr.Interface(change_detection_stream,
inputs=gr.Textbox(label="Change detection", value = "DUMMY"),
outputs="image")
video_CD.change(
fn=lambda video: CAP.append(video),
inputs=[video_CD],
)
with gr.Tab("Using a Real Time Camera"):
with gr.Row():
with gr.Column():
webcam_img_RT = gr.Image(label="Webcam", sources="webcam")
webcam_img_RT.stream(lambda s: frame_stack.append(
cv2.resize(
s,
(WIDTH_STANDARD, HEIGHT_STANDARD)
)
),
webcam_img_RT,
time_limit=15, stream_every=1.0,
concurrency_limit=30
)
with gr.Column():
optical_flow_img_RT = gr.Interface(compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs="image",
)
with gr.Row():
with gr.Column():
detection_img_RT = gr.Interface(object_detection_stream,
inputs=gr.Textbox(label="Classes: Enter the classes",
value = "one bird, one airplane, one kite,a flying object,sky"),
outputs="image")
with gr.Tab("Using a Real Time Camera (Change Detection)"):
with gr.Row():
with gr.Column():
webcam_img_RT_CD = gr.Image(label="Webcam", sources="webcam")
webcam_img_RT_CD.stream(lambda s: frame_stack.append(
cv2.resize(
s,
(WIDTH_STANDARD, HEIGHT_STANDARD)
)
),
webcam_img_RT_CD,
time_limit=15, stream_every=1.0,
concurrency_limit=30
)
with gr.Column():
optical_flow_img_RT_CD = gr.Interface(compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs="image",
)
with gr.Row():
with gr.Column():
detection_img_RT_CD = gr.Interface(change_detection_stream,
inputs=gr.Textbox(label="Changes will be detected here",
value = "DUMMY"),
outputs="image")
with gr.Tab("Using a Hikvision Camera"):
with gr.Row():
with gr.Column():
with gr.Row():
video_address = gr.Textbox(label="Video Source Address")
with gr.Row():
example_addresses = gr.Examples(
examples=EXAMPLE_VIDEOS_LIST+[
'rtsp://admin:Admin123@192.168.254.200:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.201:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.202:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.203:554/Streaming/Channels/101'
],
inputs=[video_address],
)
with gr.Column():
webcam_img_HIKvision = gr.Interface(video_stream_HIKvision,
inputs=[video_address, gr.Textbox(label="Acquisition: Enter the frame rate", value = 2.0)], #
outputs="image")
with gr.Row():
with gr.Column():
optical_flow_img = gr.Interface(compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs=gr.Image(),#,"image",
)
with gr.Column():
detection_img = gr.Interface(object_detection_stream,
inputs=gr.Textbox(label="Classes: Enter the classes", value = "one bird, one airplane, one kite,a flying object,sky"),
outputs="image")
with gr.Tab("Using a Hikvision Camera (Change Detection)"):
with gr.Row():
with gr.Column():
with gr.Row():
video_address_CD = gr.Textbox(label="Hikvision Camera Address (RTSP)")
with gr.Row():
example_addresses_CD = gr.Examples(
examples=EXAMPLE_VIDEOS_LIST+[
'rtsp://admin:Admin123@192.168.254.200:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.201:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.202:554/Streaming/Channels/101',
'rtsp://admin:Admin123@192.168.254.203:554/Streaming/Channels/101'
],
inputs=[video_address_CD],
)
with gr.Column():
hikvision_stream_CD = gr.Interface(
video_stream_HIKvision,
inputs=[
video_address_CD,
gr.Textbox(label="Acquisition: Enter the frame rate", value=2.0)
],
outputs="image"
)
with gr.Row():
with gr.Column():
optical_flow_img_HIK_CD = gr.Interface(
compute_optical_flow,
inputs=gr.Slider(label="Optical Flow: Noise Tolerance", minimum=0.0, maximum=1.0, value=0.4),
outputs="image"
)
with gr.Column():
detection_img_HIK_CD = gr.Interface(
change_detection_stream,
inputs=gr.Textbox(label="Changes will be detected here", value="DUMMY"),
outputs="image"
)
with gr.Tab("Runtime Histograms"):
def plot_histogram(data, title, color):
plt.figure(figsize=(9, 5))
plt.hist(data, bins=30, color=color, alpha=0.7)
plt.title(title)
plt.xlabel('Runtime (seconds)')
plt.ylabel('Frequency')
plt.grid(True)
plt.tight_layout()
filename = title.replace(" ", "_").lower() + ".png"
plt.savefig(filename)
if os.path.exists(filename):
img_plt = cv2.imread(filename)
return img_plt
else:
return np.zeros((256, 256, 3), dtype=np.uint8) + 127
def update_optical_flow_plot():
return plot_histogram(np.array(optical_flow_runtime), 'Histogram of Optical Flow Runtime', 'blue')
def update_object_detection_plot():
return plot_histogram(object_detection_runtime, 'Histogram of Object Detection Runtime', 'green')
def update_change_detection_plot():
return plot_histogram(change_detection_runtime, 'Histogram of Change Detection Runtime', 'red')
with gr.Row():
optical_flow_image = gr.Image(update_optical_flow_plot, label="Optical Flow Runtime Histogram")
with gr.Row():
optical_flow_button = gr.Button("Update Optical Flow Histogram")
optical_flow_button.click(fn=update_optical_flow_plot, outputs=optical_flow_image)
with gr.Row():
object_detection_image = gr.Image(update_object_detection_plot, label="Object Detection Runtime Histogram")
with gr.Row():
object_detection_button = gr.Button("Update Object Detection Histogram")
object_detection_button.click(fn=update_object_detection_plot, outputs=object_detection_image)
with gr.Row():
change_detection_image = gr.Image(update_change_detection_plot, label="Change Detection Runtime Histogram")
with gr.Row():
change_detection_button = gr.Button("Update Change Detection Histogram")
change_detection_button.click(fn=update_change_detection_plot, outputs=change_detection_image)
demo.launch(debug=True)
|