added more variables to analyzer

This commit is contained in:
2025-06-04 07:34:48 +02:00
parent 9a0b224ffb
commit 2be1029b73
+34 -20
View File
@@ -4,6 +4,13 @@ import numpy as np
class Analyzer:
def __init__(self, debug_mode=False):
self.overlay = None
self.overlay_color = None
self.alpha_mask = None
self.alpha_channel = None
self.combined_mask = None
self.gray = None
self.fg_mask = None
self.gray_prev = None
self.motion_accumulator = None
self.bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=50, detectShadows=False)
@@ -105,29 +112,35 @@ class Analyzer:
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)
if self.fg_mask is None:
self.fg_mask = np.zeros_like(self.gray_prev, dtype=np.uint8)
if self.gray is None:
self.gray = np.zeros_like(self.gray_prev, dtype=np.uint8)
if self.combined_mask is None:
self.combined_mask = np.zeros_like(self.gray_prev, dtype=np.uint8)
self.frame_count += 1
fg_mask = self.bg_subtractor.apply(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
self.bg_subtractor.apply(frame, self.fg_mask)
cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY, self.gray)
flow = cv2.calcOpticalFlowFarneback(
self.gray_prev, gray, None,
self.gray_prev, self.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()
self.gray_prev = self.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, 50) # decay by 1
self.motion_accumulator = np.clip(self.motion_accumulator, 0, 255)
cv2.bitwise_and(self.fg_mask, motion_mask, self.combined_mask)
cv2.add(self.motion_accumulator, self.combined_mask.astype(np.uint16), self.motion_accumulator)
cv2.subtract(self.motion_accumulator, 50, self.motion_accumulator)
np.clip(self.motion_accumulator, 0, 255, self.motion_accumulator)
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))
m = cv2.bitwise_and(final_mask, roads)
contours, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.morphologyEx(final_mask, cv2.MORPH_OPEN, np.ones((5, 5), np.uint8), final_mask)
cv2.bitwise_and(final_mask, roads, final_mask)
contours, _ = cv2.findContours(final_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:
a = [cv2.contourArea(k) for k in contours]
avg = sum(a) / len(a)
@@ -135,11 +148,12 @@ class Analyzer:
contours = [k for k in contours if cv2.contourArea(k) > m_a]
if self.debug:
output = frame.copy().astype(np.float32) / 255.0
alpha_channel = roads.copy().astype(np.float32) / 1024.0
alpha_mask = cv2.merge([alpha_channel, alpha_channel, alpha_channel])
overlay_color = np.array([1.0, 0.0, 0.0], dtype=np.float32)
overlay = np.ones_like(output) * overlay_color
output = (1.0 - alpha_mask) * output + alpha_mask * overlay
output = cv2.drawContours(output, contours, -1, (0, 255, 0), 2)
cv2.imshow("roads", output)
frame = frame.astype(np.float32) / 255.0
if self.alpha_mask is None:
alpha_channel = roads.copy().astype(np.float32) / 1024.0
self.alpha_mask = cv2.merge([alpha_channel, alpha_channel, alpha_channel])
self.overlay_color = np.array([1.0, 0.0, 0.0], dtype=np.float32)
self.overlay = np.ones_like(frame) * self.overlay_color
frame = (1.0 - self.alpha_mask) * frame + self.alpha_mask * self.overlay
frame = cv2.drawContours(frame, contours, -1, (0, 255, 0), 2)
cv2.imshow("cars", frame)