81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
|
|
class Analyzer:
|
|
def __init__(self, debug_mode=False):
|
|
self.gray_prev = None
|
|
self.motion_accumulator = None
|
|
self.bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=50, detectShadows=False)
|
|
self.stable_mask = None
|
|
self.confidence = None
|
|
self.init = False
|
|
self.frame_count = 0
|
|
self.mask = None
|
|
self.debug = debug_mode
|
|
|
|
def analyze_frame(self, frame):
|
|
if not self.init:
|
|
self.init = True
|
|
if self.gray_prev is None:
|
|
self.gray_prev = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
if self.motion_accumulator is None:
|
|
self.motion_accumulator = np.zeros_like(self.gray_prev, dtype=np.uint16)
|
|
if self.stable_mask is None:
|
|
self.stable_mask = np.zeros_like(self.gray_prev, dtype=np.uint8)
|
|
if self.confidence is None:
|
|
self.confidence = np.zeros_like(self.gray_prev, dtype=np.float32)
|
|
if self.mask is None:
|
|
self.mask = np.zeros_like(self.gray_prev, dtype=np.float32)
|
|
self.frame_count += 1
|
|
|
|
fg_mask = self.bg_subtractor.apply(frame)
|
|
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
flow = cv2.calcOpticalFlowFarneback(
|
|
self.gray_prev, gray, None,
|
|
pyr_scale=0.5, levels=3, winsize=15,
|
|
iterations=3, poly_n=5, poly_sigma=1.2, flags=0
|
|
)
|
|
self.gray_prev = gray.copy()
|
|
|
|
magnitude, _ = cv2.cartToPolar(flow[..., 0], flow[..., 1])
|
|
motion_mask = (magnitude > 1.0).astype(np.uint8) * 255
|
|
combined_mask = cv2.bitwise_and(fg_mask, motion_mask)
|
|
self.motion_accumulator = cv2.add(self.motion_accumulator, combined_mask.astype(np.uint16))
|
|
self.motion_accumulator = cv2.subtract(self.motion_accumulator, 1) # decay by 1
|
|
self.motion_accumulator = np.clip(self.motion_accumulator, 0, 255)
|
|
|
|
final_mask = (self.motion_accumulator > 20).astype(np.uint8) * 255
|
|
final_mask = cv2.morphologyEx(final_mask, cv2.MORPH_OPEN, np.ones((5, 5), np.uint8))
|
|
self.stable_mask |= final_mask
|
|
ones = np.zeros_like(final_mask, dtype=np.uint8)
|
|
ones[final_mask == 255] = 1
|
|
self.confidence += (ones - self.confidence) / self.frame_count
|
|
max_val = np.max(self.confidence)
|
|
if max_val < 0.001:
|
|
return
|
|
self.mask = self.confidence.copy()
|
|
self.mask /= np.max(self.mask)
|
|
self.mask = np.clip(self.mask, 0, 1)
|
|
_, self.mask = cv2.threshold(self.mask, 0.1, 1, cv2.THRESH_TOZERO)
|
|
|
|
if self.debug:
|
|
output = frame.copy().astype(np.float32) / 255.0
|
|
alpha_mask = cv2.merge([self.mask, self.mask, self.mask])
|
|
overlay_color = np.array([0.0, 1.0, 0.0], dtype=np.float32)
|
|
overlay = np.ones_like(output) * overlay_color
|
|
output = (1.0 - alpha_mask) * output + alpha_mask * overlay
|
|
cv2.imshow("Detected Motion", output)
|
|
cv2.imshow("Final Mask", final_mask)
|
|
|
|
def get_roads(self):
|
|
return self.mask
|
|
|
|
def reset(self):
|
|
self.bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=50, detectShadows=False)
|
|
self.gray_prev = None
|
|
self.motion_accumulator = None
|
|
self.stable_mask = None
|
|
self.init = False
|