-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
232 lines (183 loc) · 9.85 KB
/
test.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import copy, os, random, time, cv2, numpy as np
from PIL import Image
class ImageDrawingEvolutionary:
def __init__(self, image_array):
self.original_image_np_array = image_array
self.original_shape = self.original_image_np_array.shape
self.copy_gene_array = self.generate_random_rgb(self.original_shape[0], self.original_shape[1])
self.fitness = self.calculate_fitness()
def generate_random_rgb(self, dim1, dim2):
img = np.zeros((dim1, dim2, 3), np.uint8)
img[:, :] = np.array([0, 0, 0])
# if not os.path.exists('monaliza'):
# os.mkdir('monaliza')
# img = cv2.imread("monaliza/im35000.JPG")
return img
@classmethod
def generate_np_from_img(self, image_name):
img = cv2.imread(image_name)
array = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return array
def calculate_fitness(self):
fitness = np.sum(np.absolute(self.original_image_np_array - self.copy_gene_array))
return fitness
def draw_image(self, save=False, filename=None):
cv2.destroyAllWindows()
cv2.imshow("image", self.copy_gene_array)
if save:
cv2.imwrite(f"monaliza/{filename}", self.copy_gene_array)
cv2.waitKey(4)
@classmethod
def select_best(cls, population, parent_count):
population = sorted(population, key=lambda x: x.fitness)
return population[:parent_count]
@classmethod
def select_random(cls, population, parent_count):
pop_random = copy.deepcopy(population)
random.shuffle(pop_random)
return pop_random[:parent_count]
@classmethod
def crossover(cls, parents):
child_1 = copy.deepcopy(parents[0])
child_2 = copy.deepcopy(parents[1])
if random.choice(['r', 'c']) == 'r':
random_row_index_1 = random.randint(0, parents[0].original_shape[0]-1)
random_row_index_2 = random.randint(0, parents[0].original_shape[0]-1)
if random_row_index_1 > random_row_index_2:
random_row_index_1, random_row_index_2 = random_row_index_2, random_row_index_1
# random_col_index_1 = random.randint(0,parents[0].original_shape[1]-1)
# random_col_index_2 = random.randint(0,parents[0].original_shape[1]-1)
# if random_col_index_1>random_col_index_2:
# random_col_index_1,random_col_index_2=random_col_index_2,random_col_index_1
# child_1.copy_gene_array[random_row_index_1][random_col_index_1:random_col_index_2] = parents[1].copy_gene_array[random_row_index_1][random_col_index_1:random_col_index_2]
# child_2.copy_gene_array[random_row_index_1][random_col_index_1:random_col_index_2] = parents[0].copy_gene_array[random_row_index_1][random_col_index_1:random_col_index_2]
child_1.copy_gene_array[random_row_index_1:random_row_index_2] = parents[1].copy_gene_array[random_row_index_1:random_row_index_2]
child_2.copy_gene_array[random_row_index_1:random_row_index_2] = parents[0].copy_gene_array[random_row_index_1:random_row_index_2]
child_1.fitness = child_1.calculate_fitness()
child_2.fitness = child_2.calculate_fitness()
return child_1, child_2
else:
random_col_index_1 = random.randint(0, parents[0].original_shape[1] - 1)
random_col_index_2 = random.randint(0, parents[0].original_shape[1] - 1)
if random_col_index_1 > random_col_index_2:
random_col_index_1, random_col_index_2 = random_col_index_2, random_col_index_1
# random_col_index_1 = random.randint(0,parents[0].original_shape[1]-1)
# random_col_index_2 = random.randint(0,parents[0].original_shape[1]-1)
# if random_col_index_1>random_col_index_2:
# random_col_index_1,random_col_index_2=random_col_index_2,random_col_index_1
# child_1.copy_gene_array[random_col_index_1][random_col_index_1:random_col_index_2] = parents[1].copy_gene_array[random_col_index_1][random_col_index_1:random_col_index_2]
# child_2.copy_gene_array[random_col_index_1][random_col_index_1:random_col_index_2] = parents[0].copy_gene_array[random_col_index_1][random_col_index_1:random_col_index_2]
child_1.copy_gene_array[:][random_col_index_1:random_col_index_2] = parents[1].copy_gene_array[:][
random_col_index_1:random_col_index_2]
child_2.copy_gene_array[:][random_col_index_1:random_col_index_2] = parents[0].copy_gene_array[:][
random_col_index_1:random_col_index_2]
child_1.fitness = child_1.calculate_fitness()
child_2.fitness = child_2.calculate_fitness()
return child_1, child_2
@classmethod
def mutation(cls, chromosome):
if random.random() < 0.5:
np_arr = copy.deepcopy(chromosome.copy_gene_array)
rand_ind1 = random.randint(0, len(np_arr)-1)
rand_ind2 = random.randint(0, len(np_arr)-1)
rand_ind3 = random.randint(0, len(np_arr[0])-1)
rand_ind4 = random.randint(0, len(np_arr[0])-1)
if rand_ind1 > rand_ind2:
rand_ind1, rand_ind2 = rand_ind2, rand_ind1
if rand_ind3 > rand_ind4:
rand_ind3, rand_ind4 = rand_ind4, rand_ind3
# adding solid colors
# choice = np.array([random.randint(1,255),random.randint(1,255),random.randint(1,255)])
# for i in range(rand_ind1,rand_ind2):
# for j in range(rand_ind3,rand_ind4):
# np_arr[i][j]=choice
# adding changes in colors
rgb = random.choice(['r', 'g', 'b'])
if rgb == 'r':
if random.choice([0, 1]) == 0:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][0] + 15 < 256:
np_arr[i][j][0] += 15
else:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][0] - 15 > 256:
np_arr[i][j][0] -= 15
elif rgb == 'g':
if random.choice([0, 1]) == 0:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][1] + 15 < 256:
np_arr[i][j][1] += 15
else:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][1] - 15 > 256:
np_arr[i][j][1] -= 15
else:
if random.choice([0, 1]) == 0:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][2] + 15 < 256:
np_arr[i][j][2] += 15
else:
for i in range(rand_ind1, rand_ind2):
for j in range(rand_ind3, rand_ind4):
if np_arr[i][j][2] - 15 > 256:
np_arr[i][j][2] -= 15
chromosome.copy_gene_array = np_arr
chromosome.fitness = chromosome.calculate_fitness()
else:
np_arr = copy.deepcopy(chromosome.copy_gene_array)
rand_ind1 = random.randint(0, len(np_arr)-1)
rand_ind4 = random.randint(0, len(np_arr[0])-1)
np_arr[rand_ind1, rand_ind4] = np.array([random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)])
chromosome.copy_gene_array = np_arr
chromosome.fitness = chromosome.calculate_fitness()
return chromosome
if __name__ == '__main__':
image_name = "IMG_9059.JPG"
image_np_array = ImageDrawingEvolutionary.generate_np_from_img(image_name)
image_np_array = cv2.cvtColor(image_np_array, cv2.COLOR_BGR2RGB)
population = []
generation = 0
for i in range(10):
population.append(ImageDrawingEvolutionary(image_np_array))
print("-------------After Select----------------")
best = ImageDrawingEvolutionary.select_best(population, 1)[0]
best.draw_image(True, "im"+str(generation)+".JPG")
print(best.fitness)
# generation = 35001
while best.fitness > 100:
new_population = []
for i in range(0, len(population), 2):
# a= time.time()
random_parents = ImageDrawingEvolutionary.select_random(population, 2)
# b=time.time()
# print(b-a)
# random_parents = ImageDrawingEvolutionary.select_binary_tour(population, 3, 2)
# a= time.time()
child_1, child_2 = ImageDrawingEvolutionary.crossover(random_parents)
# b=time.time()
# print(b-a)
# a= time.time()
child_1, child_2 = ImageDrawingEvolutionary.mutation(child_1), ImageDrawingEvolutionary.mutation(child_2)
# b=time.time()
# print(b-a)
new_population.append(child_1)
new_population.append(child_2)
population = ImageDrawingEvolutionary.select_best(population+new_population, len(population))
print("-------------After Select----------------")
print(f"Generation : {generation}")
for each in population:
print(each.fitness)
print("Best:-")
best = ImageDrawingEvolutionary.select_best(population, 1)[0]
if generation % 100 == 0:
best.draw_image(True, "im"+str(generation)+".JPG")
else:
best.draw_image()
# 2131890257
print(best.fitness)
generation += 1