135 lines
4.9 KiB
Python
135 lines
4.9 KiB
Python
import os
|
|
|
|
import numpy as np
|
|
import cv2
|
|
from detection import CombinedDetector
|
|
|
|
|
|
class Jam_Detection:
|
|
def __init__(self, road_mask, max_cars):
|
|
self.detector = CombinedDetector()
|
|
self.road_mask = road_mask
|
|
self.max_cars = max_cars
|
|
#self.detector.flow.road_mask = self.road_mask # Direkt setzen
|
|
|
|
def compute_jam_score(self, car_ratio, detections):
|
|
# TODO: Write Jam_Score logic
|
|
|
|
if self.max_cars == 0:
|
|
jam_score = 0
|
|
else:
|
|
scaler = 0.85
|
|
exponent = detections / self.max_cars
|
|
jam_score = min(1.0, (np.exp(exponent*scaler)-1))
|
|
#jam_score = math.exp(exponent)-1
|
|
|
|
# Pfad zur Log-Datei
|
|
log_path = "jam_log.csv"
|
|
|
|
# Prüfen, ob Datei existiert und leer ist
|
|
file_exists = os.path.exists(log_path)
|
|
file_empty = not file_exists or os.stat(log_path).st_size == 0
|
|
|
|
# Log-Daten in Datei schreiben
|
|
with open(log_path, "a") as f:
|
|
if file_empty:
|
|
f.write("car_ratio,jam_score,number_of_detections\n")
|
|
f.write(f"{car_ratio},{jam_score},{detections}\n")
|
|
|
|
return jam_score
|
|
|
|
def detect_frame(self, frame):
|
|
# YOLO-Detektion
|
|
detections = self.detector.yolo.detect(frame)
|
|
|
|
# Maske für erkannte Autos erstellen
|
|
car_mask = np.zeros(frame.shape[:2], dtype=np.uint8)
|
|
for det in detections:
|
|
x1, y1, x2, y2 = map(int, det[:4])
|
|
car_mask[y1:y2, x1:x2] = 255
|
|
|
|
# Road-Maske vorbereiten
|
|
road_mask = self.road_mask
|
|
if road_mask.shape != car_mask.shape:
|
|
road_mask = cv2.resize(road_mask, (car_mask.shape[1], car_mask.shape[0]), interpolation=cv2.INTER_NEAREST)
|
|
|
|
# Analyse: Autos auf der Straße
|
|
road_area = (road_mask == 255)
|
|
car_on_road = np.logical_and(road_area, car_mask == 255)
|
|
|
|
total_road_pixels = np.sum(road_area)
|
|
car_pixels = np.sum(car_on_road)
|
|
car_ratio = car_pixels / total_road_pixels if total_road_pixels > 0 else 0
|
|
|
|
if total_road_pixels == 0:
|
|
print("Road mask hat keine 255-Werte")
|
|
return
|
|
|
|
# Da Flow raus ist, setzen wir flow_ratio auf 0
|
|
flow_ratio = 0
|
|
|
|
car_count = len(detections)
|
|
|
|
# Berechne den Stau-Score nur anhand von Autos
|
|
jam_score = self.compute_jam_score(car_ratio, car_count)
|
|
|
|
|
|
# Visualisierung
|
|
self.visualize_overlay(frame, np.zeros_like(car_mask), car_ratio, car_count, jam_score, detections=detections)
|
|
|
|
|
|
def visualize_overlay(self, frame, final_mask, car_coverage_ratio, car_count, jam_score, detections=None):
|
|
overlay = frame.copy()
|
|
|
|
if self.road_mask.shape != frame.shape[:2]:
|
|
road_mask = cv2.resize(self.road_mask, (frame.shape[1], frame.shape[0]), interpolation=cv2.INTER_NEAREST)
|
|
else:
|
|
road_mask = self.road_mask
|
|
|
|
color_mask = np.zeros_like(frame)
|
|
color_mask[road_mask == 255] = (255, 0, 0) # Blau für Straße
|
|
|
|
overlay = cv2.addWeighted(overlay, 1.0, color_mask, 0.4, 0)
|
|
|
|
motion_mask = np.zeros_like(frame)
|
|
motion_mask[final_mask == 255] = (0, 255, 0) # Grün für Bewegung
|
|
overlay = cv2.addWeighted(overlay, 1.0, motion_mask, 1.0, 0)
|
|
|
|
car_ratio = f"Coverage: {car_coverage_ratio * 100:.1f}%"
|
|
jam_text = f"Jam-score: {jam_score * 100:.2f}"
|
|
car_count_text = f"Count: {car_count:.2f}"
|
|
max_cars = f"Max: {self.max_cars:.2f}"
|
|
|
|
|
|
cv2.putText(overlay, car_ratio, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
|
|
cv2.putText(overlay, car_ratio, (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
|
|
|
|
cv2.putText(overlay, jam_text, (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
|
|
cv2.putText(overlay, jam_text, (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
|
|
|
|
cv2.putText(overlay, car_count_text, (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
|
|
cv2.putText(overlay, car_count_text, (20, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
|
|
|
|
cv2.putText(overlay, max_cars, (20, 160), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 3, cv2.LINE_AA)
|
|
cv2.putText(overlay, max_cars, (20, 160), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1, cv2.LINE_AA)
|
|
|
|
|
|
if detections is not None:
|
|
for det in detections:
|
|
x1, y1, x2, y2 = map(int, det[:4])
|
|
conf = det[4] if len(det) > 4 else None
|
|
class_id = int(det[5]) if len(det) > 5 else None
|
|
|
|
# Box zeichnen (Rot)
|
|
cv2.rectangle(overlay, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
|
|
|
cv2.imshow("Stau-Overlay", overlay)
|
|
|
|
def saveMask(self):
|
|
if self.road_mask is not None:
|
|
cv2.imwrite("road_mask.png", self.road_mask)
|
|
|
|
def setMask(self, mask):
|
|
self.road_mask = mask
|
|
print(f"mask set: {mask.shape}")
|