-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight_calibration.py
More file actions
171 lines (130 loc) · 4.27 KB
/
light_calibration.py
File metadata and controls
171 lines (130 loc) · 4.27 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import xled
import io
import struct
import numpy as np
import time
import math
import random
import json
from CameraController import CameraController
from LEDController import LEDController
camera_test = False
led_test = False
calibration_folder = 'calibration_images'
face_mapping = {
"left": LEDController.LEFT,
"right": LEDController.RIGHT,
"top": LEDController.TOP,
"back": LEDController.BACK
}
with open('cube/color_configs_test.json', 'r') as file:
data = json.load(file)
# Initialize the camera and LED controllers
camera = CameraController(led_test=led_test)
led = LEDController(camera_test=camera_test)
for color_set, sides in data.items():
print(f"\nColor set: {color_set}")
num_leds = led.device_data["number_of_led"]
faces = {
'top' : led.get_empty_pattern(),
'back' : led.get_empty_pattern(),
'left' : led.get_empty_pattern(),
'right' : led.get_empty_pattern(),
}
for side, values in sides.items():
filename = f"{calibration_folder}/{color_set}_{side}.jpg"
# print(f"Filename: {filename}")
r = values[0] * 255
g = values[1] * 255
b = values[2] * 255
face_pattern = led.create_face_pattern(face_mapping[side], r, g, b, 255)
faces[side] = face_pattern
if not camera_test:
# Set the LED pattern
led.display_pattern(face_pattern)
# Wait for the LED to stabilize
time.sleep(2)
# Capture the image
try:
# Open the camera
try:
camera.open_camera()
print("Camera opened successfully.")
except Exception as e:
print(f"Error opening camera: {e}")
camera.capture_image()
time.sleep(1) # Delay to ensure the image is saved on the card
try:
# Close the camera session
camera.close_camera()
except Exception as e:
print(f"Error closing camera: {e}")
except Exception as e:
print(f"Error capturing image: {e}")
# Download the image
try:
# Open the camera
try:
camera.open_camera()
print("Camera opened successfully.")
except Exception as e:
print(f"Error opening camera: {e}")
camera.download_image(filename)
try:
# Close the camera session
camera.close_camera()
except Exception as e:
print(f"Error closing camera: {e}")
except Exception as e:
print(f"Error downloading image: {e}")
filename = f"{calibration_folder}/{color_set}_combined.jpg"
# Combine the patterns for all faces
combined_pattern = led.combine_patterns(
left=faces['left'],
right=faces['right'],
top=faces['top'],
back=faces['back']
)
if not camera_test:
# Set the LED pattern
led.display_pattern(combined_pattern)
# Wait for the LED to stabilize
time.sleep(2)
# Capture the image
try:
# Open the camera
try:
camera.open_camera()
print("Camera opened successfully.")
except Exception as e:
print(f"Error opening camera: {e}")
camera.capture_image()
time.sleep(1) # Delay to ensure the image is saved on the card
try:
# Close the camera session
camera.close_camera()
except Exception as e:
print(f"Error closing camera: {e}")
except Exception as e:
print(f"Error capturing image: {e}")
# Download the image
try:
# Open the camera
try:
camera.open_camera()
print("Camera opened successfully.")
except Exception as e:
print(f"Error opening camera: {e}")
camera.download_image(filename)
try:
# Close the camera session
camera.close_camera()
except Exception as e:
print(f"Error closing camera: {e}")
except Exception as e:
print(f"Error downloading image: {e}")
if not led_test:
# Close the camera session
camera.close_camera()
# Terminate the SDK
camera.terminate_sdk()