forked from phl/pareidoloop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pareidoloop.html
281 lines (233 loc) · 6.29 KB
/
pareidoloop.html
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<html>
<head>
<title>pareidoloop</title>
<script type="text/javascript" src="ccv.js"></script>
<script type="text/javascript" src="face.js"></script>
<script type="text/javascript" src="pareidoloop.js"></script>
<style type="text/css">
body {
padding: 24px;
font-family: helvetica;
}
div.gen {
padding: 0 0 8px 0;
margin-bottom: 8px;
}
.score {
font-size: 10px;
}
div#output {
padding: 0;
}
div#canvases {
padding: 0;
}
#output img {
margin : 0 4px 4px 0;
}
canvas {
float: left;
margin: 0 6px 0 0;
padding: 0;
background-color: #000;
}
.clear {
clear: both;
margin: 0;
padding: 0;
}
div.hr {
border-top: 1px solid #ccc;
margin-top: 8px;
margin-bottom: 8px;
padding: 4px;
}
div.input {
padding: 4px 0px 4px 0px;
}
div.input span {
padding-left: 6px;
}
.label {
font-weight: 100;
font-size: 18px;
}
.tip {
font-size: 10px;
}
#controls input {
width: 40px;
font-size: 10px;
float: left;
border: 1px solid #000;
padding: 4px;
}
h3 {
font-weight: 100;
margin-top: 0;
margin-bottom: 12px;
font-size: 18px;
}
h2 {
font-weight: 100;
font-size: 36px;
}
button {
width: 100px;
background: #000;
color: #fff;
font-weight: 100;
border-color: #000;
padding: 8px;
margin-left: 0px;
}
button:active {
border-color: #fff #fff #fff #fff;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked + label span.y {
display: inline;
}
input[type=checkbox]:checked + label span.n {
display: none;
}
input[type=checkbox] + label span.n {
display: inline;
}
input[type=checkbox] + label span.y {
display: none;
}
label span.u {
text-decoration: underline;
}
a {
color: black;
}
</style>
</head>
<body>
<h2>pareidoloop</h2>
<div id="controls">
<div class="input">
<input id="input-image-size" type="text" value="50"/>
<span class="label">output size</span><span class="tip">(to render finished faces)</span>
<div class="clear"></div>
</div>
<div class="input">
<input id="input-target-fitness" type="text" value="27.5"/>
<span class="label">target fitness</span><span class="tip">(lower is faster, max 35)</span>
<div class="clear"></div>
</div>
<div class="input">
<input id="input-max-gens" type="text" value="6000"/>
<span class="label">max generations</span><span class="tip">(cut-off if target not reached)</span>
<div class="clear"></div>
</div>
<div class="input">
<button id="input-button" type="button">commence</button>
<div class="clear"></div>
</div>
</div>
<div class="hr"></div>
<div id="canvases">
<div class="gen">
<canvas id="canvasB" width="50" height="50"></canvas>
<div>
<h3>current generation</h3>
<span id="scoreB" class="score"></span>
</div>
<div class="clear"></div>
</div>
<div class="gen">
<canvas id="canvasA" width="50" height="50"></canvas>
<div>
<h3>fittest generation</h3>
<span id="scoreA" class="score"></span>
</div>
<div class="clear"></div>
</div>
</div>
<div class="hr"></div>
<div id="output"></div>
<span class="tip"><a href="https://github.com/phl/pareidoloop/blob/master/README.md">about</a> |
<input id="save-checkbox" type="checkbox"/><label for="save-checkbox"><span class="u">autosave images:</span> <span class="y">yes (chrome only)</span><span class="n">no</span></label></span>
<script type="text/javascript">
var inputImageSize = document.getElementById("input-image-size");
var inputTargetFitness = document.getElementById("input-target-fitness");
var inputMaxGens = document.getElementById("input-max-gens");
var running = false;
var inputButton = document.getElementById("input-button");
var saveCheckbox = document.getElementById("save-checkbox");
var output = document.getElementById("output");
inputButton.onclick = function() {
if (running) {
Pareidoloop.stop();
running = false;
inputButton.innerHTML = "commence";
} else {
var go = true;
var imageSize = parseInt(inputImageSize.value);
if (isNaN(imageSize) || imageSize < 1) {
inputImageSize.style.borderColor = "#f00";
go = false;
} else {
inputImageSize.style.borderColor = "#000";
}
var targetFitness = parseFloat(inputTargetFitness.value);
if (isNaN(targetFitness) || targetFitness <= 0 || targetFitness > 35) {
inputTargetFitness.style.borderColor = "#f00";
go = false;
} else {
inputTargetFitness.style.borderColor = "#000";
}
var maxGens = parseInt(inputMaxGens.value);
if (isNaN(maxGens) || maxGens <= 0) {
inputMaxGens.style.borderColor = "#f00";
go = false;
} else {
inputMaxGens.style.borderColor = "#000";
}
if (go) {
Pareidoloop.start({outputCallback: renderImage, outputSize: imageSize, confidenceThreshold: targetFitness, maxGenerations: maxGens});
inputButton.innerHTML = "cease";
running = true;
}
}
}
function renderImage(image) {
output.appendChild(image);
if (!saveCheckbox.checked) return;
var downloader = document.getElementById('downloader');
if (!downloader) {
downloader = document.createElement('div');
downloader.id = 'downloader';
downloader.style.display = 'none';
}
document.body.appendChild(downloader);
while (downloader.children.length > 0) {
downloader.removeChild(downloader.children[downloader.children.length - 1]);
}
var anchor = document.createElement('a');
anchor.href = image.src;
anchor.target = "_blank";
anchor.download = getDefaultFilename(image);
downloader.appendChild(anchor);
anchor.click();
document.body.removeChild(downloader);
}
function getDefaultFilename(img) {
var hash = 0;
var str = img.src;
if (!str || str.length == 0) return "face";
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = (hash & hash) >>> 0;
}
return "face-" + img.width + "x" + img.height + "-" + hash.toString(36) + ".png";
}
</script>
</body>
</html>