-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscience_script.py
More file actions
90 lines (70 loc) · 2.42 KB
/
science_script.py
File metadata and controls
90 lines (70 loc) · 2.42 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import cv2
import numpy as np
import subprocess
from pathlib import Path
# EXPOSURES = [50, 150, 300]
# EXPOSURES = list(range(50, 625, 50))
EXPOSURES = [11, 83, 250, 500, 830, 5000]
WARMUP_FRAMES = 5
def camera_is_usable(device):
camera = cv2.VideoCapture(str(device))
try:
return camera.isOpened()
finally:
camera.release()
def find_cameras():
devices = sorted(Path("/dev").glob("video*"))
return [device for device in devices if camera_is_usable(device)]
def set_exposure(device, exposure):
commands = [
["v4l2-ctl", "-d", str(device), "-c", "auto_exposure=1", "-c", f"exposure_time_absolute={exposure}"],
["v4l2-ctl", "-d", str(device), "-c", "auto_exposure=3", "-c", f"exposure_time_absolute={exposure}"],
]
last_error = None
for command in commands:
result = subprocess.run(command, capture_output=True, text=True)
if result.returncode == 0:
return
last_error = result.stderr.strip() or result.stdout.strip() or "unknown v4l2-ctl error"
raise RuntimeError(f"Failed to set exposure {exposure} on {device}: {last_error}")
def capture_photo(device):
camera = cv2.VideoCapture(str(device))
if not camera.isOpened():
raise RuntimeError(f"Could not open camera {device}")
try:
for _ in range(WARMUP_FRAMES):
camera.read()
success, frame = camera.read()
if not success:
raise RuntimeError(f"Failed to capture frame from {device}")
return frame.copy()
finally:
camera.release()
def combine_images(images):
merge_mertens = cv2.createMergeMertens()
hdr_fusion = merge_mertens.process(images)
hdr_8bit = np.clip(hdr_fusion * 255, 0, 255).astype('uint8')
return hdr_8bit
def main():
photos_dir = Path("photos")
photos_dir.mkdir(exist_ok=True)
cameras = find_cameras()
if not cameras:
raise RuntimeError("No usable cameras found under /dev/video*")
resulting_images = []
for camera in cameras:
for exposure in EXPOSURES:
set_exposure(camera, exposure)
resulting_images.append(capture_photo(camera))
print(f"Captured photo from {camera} with exposure {exposure}")
# clear photos dir
for file in photos_dir.glob("*.jpg"):
file.unlink()
# Combine images incrementally and save results
for i in range(len(resulting_images)):
hdr_image = combine_images(resulting_images[:i+1])
avg_brightness = np.mean(hdr_image)
print(f"Combined {i+1} images, average brightness: {avg_brightness:.2f}")
cv2.imwrite(str(photos_dir / f"hdr_image_{i+1}.jpg"), hdr_image)
if __name__ == "__main__":
main()