-
Notifications
You must be signed in to change notification settings - Fork 15
/
demo.html
232 lines (201 loc) · 5.13 KB
/
demo.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
<style>
.hover {
background-color: green;
}
img {
width: 300px;
}
div {
margin: 20px;
padding: 10px;
background: #c0ffee;
}
</style>
<body>
<script src="phash.js"></script>
<input type="range" min="1" step="1" max="32" value="20" oninput="set_thresh(this.value)" style="width: 100%">
<script>
document.body.ondragover = function () { this.className = 'hover'; return false; };
document.body.ondragend = function () { this.className = ''; return false; };
document.body.ondrop = function (e) {
this.className = '';
e.preventDefault();
var files = e.dataTransfer.files;
for(var i = 0; i < files.length; i++){
load_file(files[i]);
}
return false;
};
var images = [];
function load_file(file){
var reader = new FileReader();
reader.onload = function (event) {
var img = new Image()
img.src = reader.result;
var hash = pHash(img);
img.hash = hash;
img.pos = images.length;
img.vel = 0;
img.acc = 0;
document.body.appendChild(img);
// for(var i = 0; i < images.length; i++){
// var other = images[i].hash;
// var diff = distance(other, hash)
// }
images.push(img);
};
// console.log(file);
reader.readAsDataURL(file);
}
// var dict = "abcdefghijklmnopqrstuwxyz"
// function distance(a, b){
// return Math.abs(a - b)
// }
// dict.split('').sort(function(a,b){
// return Math.random() - 0.5; // oh knuth gods strike me down
// }).map(function(el, i){
// var span = document.createElement('span')
// span.innerHTML = el;
// span.hash = dict.indexOf(el);
// span.pos = i;
// span.vel = 0;
// document.body.appendChild(span)
// images.push(span)
// })
function one_force(){
var energy = 0;
for(var i = 0; i < images.length; i++){
images[i].acc = 0;
}
for(var i = 0; i < images.length; i++){
for(var j = 0; j < images.length; j++){
if(i == j) continue;
var diff = distance(images[i].hash, images[j].hash);
var dist = images[j].pos - images[i].pos;
energy += Math.abs(dist) * diff;
// diff ~ repulsion
// similarity ~ attraction
images[i].acc += 0.01 * dist;
// console.log(i, j, diff)
}
}
for(var i = 0; i < images.length; i++){
images[i].vel += images[i].acc;
}
for(var i = 0; i < images.length; i++){
images[i].pos += images[i].vel;
}
images.sort(function(a, b){
return a.pos - b.pos
}).forEach(function(e){
document.body.appendChild(e)
})
console.log(energy)
}
function set_thresh(val){
var divs = document.getElementsByTagName('div');
while(divs[0]){
divs[0].parentNode.removeChild(divs[0])
}
equivalence_classes(images, function(a, b){
// console.log(distance(a.hash, b.hash))
return distance(a.hash, b.hash) < val
}).sort(function(b, a){
return a.length - b.length
}).forEach(function(group){
var div = document.createElement('div');
group.forEach(function(img){
div.appendChild(img)
})
document.body.appendChild(div)
})
}
// this is a port of something from libccv which
// is a port of something from opencv which is
// a port of an algorithm in some textbook from
// somewhere
// it has rough functional parity with ccv_array_group
// and cvSeqPartition and the union-find algorithm
// except rather than returning a list of list
// indicies in case one is so inclined to construct
// a list, it actually just returns the list
// this is a quadratic algorithm as far as I'm aware
// which means that the is_equal function will be called
// n(n - 1) times where n is the length of your elements
// array. For things with large numbers of elements,
// this can become very slow.
// it might be wise because of this to inform the
// algorithm with some notion of geometry. i.e.
// "these elements are really really far apart
// so they probably don't have anything to do with
// each other so lets just kind of let them do
// their thing and have incestuous relations with
// people closer to them"
function equivalence_classes(elements, is_equal){
var node = []
for(var i = 0; i < elements.length; i++){
node.push({
parent: 0,
element: elements[i],
rank: 0
})
}
for(var i = 0; i < node.length; i++){
var root = node[i]
while(root.parent){
root = root.parent;
}
for(var j = 0; j < node.length; j++){
if(i == j) continue;
if(!is_equal(node[i].element, node[j].element)) continue;
var root2 = node[j];
while(root2.parent){
root2 = root2.parent;
}
if(root2 != root){
if(root.rank > root2.rank){
root2.parent = root;
}else{
root.parent = root2;
if(root.rank == root2.rank){
root2.rank++
}
root = root2;
}
var node2 = node[j];
while(node2.parent){
var temp = node2;
node2 = node2.parent;
temp.parent = root;
}
var node2 = node[i];
while(node2.parent){
var temp = node2;
node2 = node2.parent;
temp.parent = root;
}
}
}
}
var index = 0;
var clusters = [];
for(var i = 0; i < node.length; i++){
var j = -1;
var node1 = node[i]
while(node1.parent){
node1 = node1.parent
}
if(node1.rank >= 0){
node1.rank = ~index++;
}
j = ~node1.rank;
if(clusters[j]){
clusters[j].push(elements[i])
}else{
clusters[j] = [elements[i]]
}
}
return clusters;
}
</script>
</body>