81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import datetime
|
|
import os.path
|
|
import random
|
|
|
|
import munch
|
|
import numpy as np
|
|
from tqdm import tqdm
|
|
|
|
from analyzer import Analyzer
|
|
from vidgear.gears.stabilizer import Stabilizer
|
|
import cv2
|
|
|
|
|
|
DEBUG_MODE = True
|
|
|
|
analyzer = Analyzer(DEBUG_MODE)
|
|
|
|
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 calibrate_camera():
|
|
cameras = os.listdir(os.path.join("recordings", "San Francisco"))
|
|
camera = random.choice(cameras)
|
|
days = [d for d in os.listdir(os.path.join("recordings", "San Francisco", camera))
|
|
if os.path.isdir(os.path.join("recordings", "San Francisco", camera, d))]
|
|
days = sorted(days)
|
|
video_files = dict()
|
|
for d in days:
|
|
videos = sorted(os.listdir(os.path.join("recordings", "San Francisco", camera, d)))
|
|
video_files[d] = [os.path.join("recordings", "San Francisco", camera, d, f) for f in videos]
|
|
videos_to_process = [f for d in days for f in video_files[d]][10:11]
|
|
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)
|
|
mask = analyzer.get_roads()
|
|
mask = (mask.copy() * 255).astype(np.uint8)
|
|
cv2.imshow('Confidence Mask', mask)
|
|
cv2.waitKey()
|
|
|
|
|
|
def main():
|
|
calibrate_camera()
|
|
# analyze_videos()
|
|
# analyze_livestream()
|
|
pass
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |