-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
157 lines (112 loc) · 4 KB
/
main2.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
import cv2 as cv
import numpy as np
import random
import heapq
import time
TIME = int(input("TIME TO RUN (min): "))
GENERATIONS = int(input("GENERATIONS PER LINE: "))
POPULATION = int(input("POPULATION SIZE: "))
SAVE = int(input("SAVE EVERY Nth IMAGE: "))
IMAGE_NAME = str(input("IMAGE NAME: "))
def addLine(image, start, end, color, thickness, alpha):
overlay = image.copy()
line = cv.line(overlay, start, end, color, thickness)
image_new = cv.addWeighted(overlay, alpha, image, 1 - alpha, 0)
return(image_new)
def getRandomLine(image):
h, w, _ = image.shape
start = (random.randrange(0, w), random.randrange(0, h))
end = (random.randrange(0, w), random.randrange(0, h))
color = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
thickness = random.randrange(1, max(w, h))
alpha = random.random()
return (start, end, color, thickness, alpha)
def addRandomLine(image):
return addLine(*((image,) + getRandomLine(image, i)))
def getScore(image1, image2):
image1 = image1 - image2
avg_color_per_row = np.sum(image1, axis=0)
avg_color = np.sum(avg_color_per_row, axis=0)
return sum(avg_color)
def getBest(heap, count):
return heapq.nsmallest(count, heap)
def mutateLine(line, min_multiplier, max_multiplier):
start, end, color, thickness, alpha = line
def randomize(value, cast=int):
return cast(value * random.uniform(min_multiplier, max_multiplier))
def randomize_tuple(tpl, cast=int):
return tuple(randomize(v, cast) for v in tpl)
#line = randomize(line, float)
start = randomize_tuple(start)
end = randomize_tuple(end)
color = randomize_tuple(color)
thickness = max(randomize(thickness), 1)
alpha = randomize(alpha, float)
return start, end, color, thickness, alpha
def getNextGen(heap, original_image, blank_image):
for i in range(len(heap)):
mutated_line = mutateLine(heap[i][1], 0.7, 1.3)
score = getScore(original_image, addLine(blank_image, *mutated_line))
heapq.heappush(heap, (score, mutated_line))
return heap
original_image = cv.imread(IMAGE_NAME)
h, w, _ = original_image.shape
blank_image = np.zeros((h, w, 3), np.uint8)
avg_color_per_row = np.sum(cv.bitwise_not(original_image), axis=0)
avg_color = np.sum(avg_color_per_row, axis=0)
stop = time.time() + 60 * TIME
for j in range(1000000):
heap = []
for i in range(POPULATION):
random_line = getRandomLine(blank_image)
score = getScore(original_image, addLine(blank_image, *random_line))
heapq.heappush(heap, (score, random_line))
for i in range(GENERATIONS):
heap = heapq.nsmallest(POPULATION//2, heap)
heap = getNextGen(heap, original_image, blank_image)
if i%SAVE==0:
cv.imwrite(f'GENERATION-{str(j).zfill(4)}{str(i).zfill(4)}.jpg', addLine(blank_image, *heap[0][1]))
if time.time() >= stop:
print(heap[0][0])
#cv.imwrite(f'evo/{heap[0][0]}.jpg', blank_image)
exit()
print(i, j, 100-( 100 * heap[0][0]/sum(avg_color)))
final_image = addLine(blank_image, *heap[0][1])
blank_image = final_image
if j%10000 == 0:
cv.imwrite(f'evo/GENERATION-{str(j).zfill(4)}.jpg', final_image)
"""
if j%1==0:
"""
"""
for j in range(1000):
lines = []
scores = []
for i in range(500):
# makes arguments for a random line
random_line = getRandomLine(blank_image)
# adds the line to a black image and gets its score
score = getScore(original_image, addLine(blank_image, *random_line))
# append line and score to list
lines.append(random_line)
scores.append(score)
for i in range(50):
# remove 50 worst lines
lines = getBest(lines, scores, len(lines)//2)
# add 50 mutated lines
lines = getNextGen(lines)
scores = []
for line in lines:
# gets scores for each new line
score = getScore(original_image, addLine(blank_image, *line))
scores.append(score)
print(i, j, min(scores))
# adds the best line to the blank image
final_image = addLine(*((blank_image,) + getBest(lines, scores, 1)[0]))
# final image becomes black image for tne next line
blank_image = final_image
if j%5==0:
cv.imshow('window', final_image)
cv.waitKey(0)
cv.destroyAllWindows()
"""