Files
TrafficAnalyzer/main.py
T
2025-06-03 15:17:26 +02:00

135 lines
4.1 KiB
Python

import datetime
import os.path
import diskcache
from tqdm import tqdm
from analyzer import Analyzer
from vidgear.gears.stabilizer import Stabilizer
import cv2
DEBUG_MODE = True
analyzer = Analyzer(DEBUG_MODE)
cache = diskcache.Cache("./cache")
def calibrate_video(f):
name = os.path.basename(f)
date = datetime.datetime(int(name[0:4]), int(name[4:6]), int(name[6:8]), int(name[8:10]), int(name[10:12]), int(name[12:14]))
cap = cv2.VideoCapture(f)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
stabilizer = Stabilizer()
analyzer.reset()
if not cap.isOpened():
raise RuntimeError("Error opening video stream or file")
limit = min(1000, length)
pbar = tqdm(total=limit, desc=f"Processing video recorded on {date}", position=1,
leave=False, ncols=125)
while cap.isOpened() and pbar.n < limit:
ret, frame = cap.read()
if not ret:
break
stab_frame = stabilizer.stabilize(frame)
if DEBUG_MODE:
cv2.imshow('Frame', frame)
if stab_frame is not None:
if DEBUG_MODE:
cv2.imshow('Stabilized Frame', stab_frame)
analyzer.analyze_frame(stab_frame)
if DEBUG_MODE:
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
if key == 27:
raise KeyboardInterrupt()
pbar.update(1)
cap.release()
pbar.close()
def get_videos():
city = "Sacramento"
cameras = os.listdir(os.path.join("recordings", city))
camera = cameras[1] # random.choice(cameras)
days = [d for d in os.listdir(os.path.join("recordings", city, camera))
if os.path.isdir(os.path.join("recordings", city, camera, d))]
days = sorted(days)
video_files = dict()
for d in days:
videos = sorted(os.listdir(os.path.join("recordings", city, camera, d)))
video_files[d] = [os.path.join("recordings", city, camera, d, f) for f in videos]
return camera, [f for d in days for f in video_files[d]]
def calibrate_roads():
camera, videos = get_videos()
if camera in cache:
return
videos_to_process = videos[10:12]
with tqdm(total=len(videos_to_process), desc="Calibrating camera: " + camera, position=0, ncols=125) as outer_bar:
for video in videos_to_process:
calibrate_video(video)
outer_bar.update(1)
roads = analyzer.get_roads()
if roads is not None:
cv2.imshow('roads', roads)
cache[camera] = roads
cv2.waitKey()
def estimate_max_speed(camera, f):
name = os.path.basename(f)
date = datetime.datetime(int(name[0:4]), int(name[4:6]), int(name[6:8]), int(name[8:10]), int(name[10:12]),
int(name[12:14]))
cap = cv2.VideoCapture(f)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
stabilizer = Stabilizer()
analyzer.reset()
roads = cache[camera]
if not cap.isOpened():
raise RuntimeError("Error opening video stream or file")
limit = min(1000, length)
pbar = tqdm(total=limit, desc=f"Processing video recorded on {date}", position=1,
leave=False, ncols=125)
while cap.isOpened() and pbar.n < limit:
ret, frame = cap.read()
if not ret:
break
stab_frame = stabilizer.stabilize(frame)
if DEBUG_MODE:
cv2.imshow('Frame', frame)
if stab_frame is not None:
if DEBUG_MODE:
cv2.imshow('Stabilized Frame', stab_frame)
analyzer.estimate_speed(stab_frame, roads)
if DEBUG_MODE:
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
if key == 27:
raise KeyboardInterrupt()
pbar.update(1)
cap.release()
pbar.close()
def calibrate_speed():
camera, videos = get_videos()
videos_to_process = videos[15:20]
if camera not in cache:
raise RuntimeError("Camera not found")
for video in videos_to_process:
estimate_max_speed(camera, video)
def main():
calibrate_roads()
calibrate_speed()
# analyze_video()
# analyze_livestream()
if __name__ == '__main__':
main()