-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate_cameras.py
71 lines (51 loc) · 2.4 KB
/
calibrate_cameras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import cv2
import logging
import numpy as np
from pathlib import Path
from scripts.python.show_calibrated_cameras import send_discord_calibration
from src.calibration.calibrate import CameraCalibrator
from src.config import config
from src.constants import CameraResolution
from src.utils.video_stream import VideoStream
def calibrate_images(images: list[np.ndarray]) -> None:
"""Calibrate the cameras using the images.
:param images The images to use for the calibration.
"""
calibrator = CameraCalibrator(images, input_shape=CameraResolution.NHD)
calibrator.calibrate()
save_dir = Path(config["calibration"]["save_dir"])
history_file = calibrator.save(save_dir)
logging.info("Saved the calibration results to %s. Output shape: %s", history_file, calibrator.output_shape)
# Save the used images to the images dir.
images_dir = save_dir / "images" / history_file.stem
images_dir.mkdir(exist_ok=True, parents=True)
cv2.imwrite(str(images_dir / "left.png"), images[0])
cv2.imwrite(str(images_dir / "center.png"), images[1])
cv2.imwrite(str(images_dir / "right.png"), images[2])
# Send an example to Discord.
send_discord_calibration()
def calibrate_cameras() -> None:
"""A script to calibrate the cameras."""
if len({config["camera_ids"]["left"], config["camera_ids"]["center"], config["camera_ids"]["right"]}) < 3:
raise ValueError("Not all camera ids are unique, calibration may not work as expected")
# Initialize the camera streams.
cam_left = VideoStream(config["camera_ids"]["left"], resolution=CameraResolution.FHD)
cam_center = VideoStream(config["camera_ids"]["center"], resolution=CameraResolution.FHD)
cam_right = VideoStream(config["camera_ids"]["right"], resolution=CameraResolution.FHD)
cam_left.start()
cam_center.start()
cam_right.start()
if not cam_left.has_next() or not cam_center.has_next() or not cam_right.has_next():
raise ValueError("Could not capture images from cameras")
# Calibrate the cameras.
left_image = cam_left.next()
center_image = cam_center.next()
right_image = cam_right.next()
# Stop the camera streams.
cam_left.stop(detach=True)
cam_center.stop(detach=True)
cam_right.stop(detach=True)
calibrate_images([left_image, center_image, right_image])
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
calibrate_cameras()