-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulator.py
135 lines (116 loc) · 3.19 KB
/
Simulator.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
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
import math
import cv2 as cv
import numpy as np
import colorsys as cs
class Simulator:
def __init__(self):
# Initialises the Simulator object
self.__image_filename = None
self.__image_margin = None
self.__image_height = None
self.__image_width = None
self.__pupil_distance_inter = None
self.__pupil_distance = None
self.__pupil_size = None
self.__coord_right = None
self.__coord_left = None
self.__spectrum = None
self.__output = None
self.__image = None
def render(self, parameters):
# Main render method
self.__set_params(parameters)
self.__image = np.zeros((
self.__image_height,
self.__image_width,
3
), np.uint8)
self.__coord_left = (
(self.__image_width // 2) - (self.__pupil_distance_inter // 2),
self.__image_height - self.__pupil_size - self.__image_margin
)
self.__coord_right = (
(self.__image_width // 2) + (self.__pupil_distance_inter // 2),
self.__image_height - self.__pupil_size - self.__image_margin
)
self.__render_spectra()
self.__render_eyes()
self.__export()
def __set_params(self, parameters):
# Sets simulation parameters
self.__image_filename = parameters['image-filename']
self.__image_margin = parameters['image-margin']
self.__image_height = parameters['image-height']
self.__image_width = parameters['image-width']
self.__pupil_distance_inter = parameters['pupil-distance-inter']
self.__pupil_distance = parameters['pupil-distance']
self.__pupil_size = parameters['pupil-size']
self.__spectrum = parameters['spectrum']
def __render_spectra(self):
# Draws the spectrum
start = self.__image_margin
end = self.__image_height - (2 * self.__image_margin) - (2 * self.__pupil_size)
length = (end - start)
# Draw rainbow according to input spectrum
for i in range(start, end):
count = i - start
fraction = count/length
index = int(len(self.__spectrum.data) * fraction)
value = self.__spectrum.data[index][1]
hue = -(fraction * (270/360)) + (270/360)
rgb = cs.hsv_to_rgb(hue, 1, value)
bgr = (
int(rgb[2] * 255),
int(rgb[1] * 255),
int(rgb[0] * 255),
)
cv.line(
self.__image,
(self.__coord_left[0] - self.__pupil_size, i),
(self.__coord_left[0] + self.__pupil_size, i),
bgr,
1
)
cv.line(
self.__image,
(self.__coord_right[0] - self.__pupil_size, i),
(self.__coord_right[0] + self.__pupil_size, i),
bgr,
1
)
def __render_eyes(self):
# Draws eyes
color = self.__spectrum.rgb
for i in range(self.__pupil_size, 1, -1):
# Find light intensity
s = self.__intensity(i/self.__pupil_size)
hsv = cs.rgb_to_hsv(color[0], color[1], color[2])
rgb = cs.hsv_to_rgb(hsv[0], 1 - s, hsv[2])
bgr = (
int(rgb[2] * 255),
int(rgb[1] * 255),
int(rgb[0] * 255),
)
# Draw left eye
cv.circle(
self.__image,
self.__coord_left,
i,
bgr,
thickness=-1
)
# Draw right eye
cv.circle(
self.__image,
self.__coord_right,
i,
bgr,
thickness=-1
)
def __intensity(self, x):
# Simplified Gaussian function for color intensity
exponent = -(x ** 2)
return math.exp(exponent)
def __export(self):
# Writes output to an image file
cv.imwrite(self.__image_filename, self.__image)