-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrico_imgGA.py
More file actions
175 lines (131 loc) · 6.66 KB
/
rico_imgGA.py
File metadata and controls
175 lines (131 loc) · 6.66 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
172
173
174
175
import random
import cv2
import numpy as np
class Population:
def __init__(self, population_size, init_fn, fitness_fn, crossover_fn, mutation_fn, p_survive, p_reproduce,
minimize_fitness=False):
self.init_fn = init_fn
self.population = [init_fn() for _ in range(0, population_size)]
self.population_size = population_size
self.fitness_fn = fitness_fn
self.crossover_fn = crossover_fn
self.mutation_fn = mutation_fn
self.num_survivors = int(p_survive * population_size)
# The cutoff point past for individuals to reproduce
self.num_parents = int(p_reproduce * population_size)
self.minimize_fitness = minimize_fitness
def evaluate(self):
# Survivors do not need to be evaluated.
for individual in self.population:
if individual.fitness is None:
individual.fitness = self.fitness_fn(individual)
self.population.sort(key=lambda x: x.fitness, reverse=not self.minimize_fitness)
def breed(self):
child_dna = [None] * (self.population_size - self.num_survivors)
for i in range(self.num_survivors, self.population_size):
# 2 parents produce one child. We choose these parents from the fittest individuals.
parent1, parent2 = random.choices(self.population[0:self.num_parents], k=2)
dna = self.crossover_fn(parent1.dna, parent2.dna)
dna = self.mutation_fn(dna)
child_dna[i-self.num_survivors] = dna
for i in range(self.num_survivors, self.population_size):
self.population[i].clear()
self.population[i].dna = child_dna[i - self.num_survivors]
def display(self, block=True):
for i, individual in enumerate(self.population):
individual.display(str(i), block)
class IndividualPoly:
def __init__(self, num_polygons, num_vertices):
self.num_polygons = num_polygons
self.num_vertices = num_vertices
self.fitness = None
self.img = None
self.dna = None
def randomize(self):
self.dna = np.random.random(self.num_vertices * self.num_polygons * 2 + self.num_polygons * 4)
def zero(self):
self.dna = np.zeros(self.num_vertices * self.num_polygons * 2 + self.num_polygons * 4)
def zerocolonly(self):
self.dna = np.random.random(self.num_vertices * self.num_polygons * 2 + self.num_polygons * 4)
self.dna[self.num_vertices * self.num_polygons * 2::] = 0
def draw(self, shape, dtype=np.uint8, colorspace="RGB"):
ptsarray = np.array(self.dna[0:self.num_vertices * self.num_polygons * 2].reshape([self.num_polygons, self.num_vertices, 2]))
colarray = np.array(self.dna[self.num_vertices * self.num_polygons * 2::].reshape([self.num_polygons, 4]))
ptsarray[:, :, 0] = ptsarray[:, :, 0] * shape[0]
ptsarray[:, :, 1] = ptsarray[:, :, 1] * shape[1]
ptsarray = ptsarray.astype(int)
colarray[:, :3] = colarray[:, :3] * 255
colarray[:, :3] = colarray[:, :3].astype(int)
img = np.zeros((shape[1], shape[0], 3), dtype=dtype)
for i in range(self.num_polygons):
overlay = img.copy()
pts = ptsarray[i]
color = tuple(colarray[i, 0:3].tolist())
alpha = colarray[i, 3] / 1.2
cv2.fillPoly(overlay, [pts], color)
img = cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0)
if colorspace == "HSV":
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
elif colorspace == "LAB":
img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
self.img = img
def display(self, window_name, block=True):
cv2.imshow(window_name, self.img)
if block:
cv2.waitKey()
else:
cv2.waitKey(1)
def clear(self):
self.fitness = None
self.img = None
self.dna = None
def save(self, filename):
cv2.imwrite(filename, self.img)
class IndividualRectangle(IndividualPoly):
def __init__(self, polynum):
IndividualPoly.__init__(self, polynum, 2)
def draw(self, shape, dtype=np.uint8, colorspace="RGB"):
ptsarray = np.array(self.dna[0:self.num_vertices * self.num_polygons * 2].reshape([self.num_polygons, self.num_vertices, 2]))
colarray = np.array(self.dna[self.num_vertices * self.num_polygons * 2::].reshape([self.num_polygons, 4]))
ptsarray[:, :, 0] = ptsarray[:, :, 0] * shape[0]
ptsarray[:, :, 1] = ptsarray[:, :, 1] * shape[1]
ptsarray = ptsarray.astype(int)
colarray[:, :3] = colarray[:, :3] * 255
colarray[:, :3] = colarray[:, :3].astype(int)
img = np.zeros([shape[1], shape[0], 3], dtype=dtype)
for i in range(0, self.num_polygons):
overlay = img.copy()
pts = ptsarray[i]
color = tuple(colarray[i, 0:3].tolist())
alpha = colarray[i, 3] / 1.2
cv2.rectangle(overlay, tuple(pts[0]), tuple(pts[1]), color, -1)
img = cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0)
if colorspace == "HSV":
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
elif colorspace == "LAB":
img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
self.img = img
class IndividualCircle(IndividualPoly):
def __init__(self, polynum):
IndividualPoly.__init__(self, polynum, 2)
def draw(self, shape, dtype=np.uint8, colorspace="RGB"):
ptsarray = np.array(self.dna[0:self.num_vertices * self.num_polygons * 2].reshape([self.num_polygons, self.num_vertices, 2]))
colarray = np.array(self.dna[self.num_vertices * self.num_polygons * 2::].reshape([self.num_polygons, 4]))
ptsarray[:, :, 0] = ptsarray[:, :, 0] * shape[0]
ptsarray[:, :, 1] = ptsarray[:, :, 1] * shape[1]
ptsarray = ptsarray.astype(int)
colarray[:, :3] = colarray[:, :3] * 255
colarray[:, :3] = colarray[:, :3].astype(int)
img = np.zeros([shape[1], shape[0], 3], dtype=dtype)
for i in range(0, self.num_polygons):
overlay = img.copy()
pts = ptsarray[i]
color = tuple(colarray[i, 0:3].tolist())
alpha = colarray[i, 3] / 1.2
cv2.circle(overlay, tuple(pts[0]), int(pts[1, 0] / 2), color, -1)
img = cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0)
if colorspace == "HSV":
img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)
elif colorspace == "LAB":
img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB)
self.img = img