Commit a403f608 by Иван Кубота

config thresh

parent 6ac370f0
...@@ -212,6 +212,7 @@ function rgbToHex(r, g, b) { ...@@ -212,6 +212,7 @@ function rgbToHex(r, g, b) {
throw "Invalid color component"; throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16); return ((r << 16) | (g << 8) | b).toString(16);
} }
var binds = [];
var extractor = function(partName, ctx, from, k, debug) { var extractor = function(partName, ctx, from, k, debug) {
extracting++; extracting++;
var colors = extractColors(ctx, from, k, debug) var colors = extractColors(ctx, from, k, debug)
...@@ -219,8 +220,11 @@ var extractor = function(partName, ctx, from, k, debug) { ...@@ -219,8 +220,11 @@ var extractor = function(partName, ctx, from, k, debug) {
return {color: hsvToRgb.apply(null, m.color).map(function(a){return a|0}), hsv: m.color, distance: m.distance}; return {color: hsvToRgb.apply(null, m.color).map(function(a){return a|0}), hsv: m.color, distance: m.distance};
}); });
binds.forEach(function(b) {
b();
});
binds = [];
return D.div({cls: 'colors'}, return D.div({cls: 'colors'},
colors.map(function(m){ colors.map(function(m){
return D.span({style: 'background:rgb('+m.color.join(',')+');', cls: 'color-preview'}, return D.span({style: 'background:rgb('+m.color.join(',')+');', cls: 'color-preview'},
...@@ -230,50 +234,57 @@ var extractor = function(partName, ctx, from, k, debug) { ...@@ -230,50 +234,57 @@ var extractor = function(partName, ctx, from, k, debug) {
); );
}), }),
function(draw) { function(draw) {
getData(partName, function(res) { getData(partName, function(res){
var b = store.sub( [ 'threshold', 'maxCount' ], function( threshold, maxCount ){
var list = []; var list = [];
res.products.forEach(function(product) { res.products.forEach( function( product ){
product.variants.forEach(function(variant) { product.variants.forEach( function( variant ){
var color = variant.option2.substr(1), var color = variant.option2.substr( 1 ),
r = parseInt(color[0]+color[1], 16), r = parseInt( color[ 0 ] + color[ 1 ], 16 ),
g = parseInt(color[2]+color[3], 16), g = parseInt( color[ 2 ] + color[ 3 ], 16 ),
b = parseInt(color[4]+color[4], 16); b = parseInt( color[ 4 ] + color[ 4 ], 16 );
list.push({ list.push( {
pid: product.id, pid: product.id,
vid: variant.id, vid: variant.id,
description: product.body_html, description: product.body_html,
title: product.title, title: product.title,
color: variant.option2, color: variant.option2,
colorName: variant.option1, colorName: variant.option1,
img: variant.featured_image && variant.featured_image.src || (product.images[0] || {}).src, img: variant.featured_image && variant.featured_image.src || ( product.images[ 0 ] || {} ).src,
sku: variant.option3, sku: variant.option3,
hsv: rgbToHsv(r,g,b) hsv: rgbToHsv( r, g, b )
}); } );
}); } );
}); } );
var used = {}; var used = {};
var all = [].concat.apply([], var all = [].concat.apply( [],
colors.map(function(clr) { colors.map( function( clr ){
var epsilon = 0.033; var epsilon = threshold;
var matched = list.map(function(i) { var matched = list.map( function( i ){
return {i: i, d: hsvDistance(i.hsv, clr.hsv), c: clr}; return { i: i, d: hsvDistance( i.hsv, clr.hsv ), c: clr };
}) } )
.filter(function(i) { .filter( function( i ){
return i.d<epsilon; return Math.max( 0, Math.min( 100, ( 1 - i.d*3 ) * 100 ) ) >= threshold;
}) } )
.sort(function(a,b) { .sort( function( a, b ){
return a.d-b.d; return a.d - b.d;
}); } );
if(maxCount === 0)
return matched;
var lastMatched = matched; var lastMatched = matched;
while(matched.length > 5){ while( matched.length > maxCount ){
epsilon = epsilon/3*2; epsilon = 100-(100-epsilon) / 3 * 2;
matched = matched.filter(function(i) { matched = matched.filter( function( i ){
return i.d<epsilon; return Math.max( 0, Math.min( 100, ( 1 - i.d*3 ) * 100 ) ) >= epsilon;
}); } );
if(matched.length===0){ if( matched.length === 0 ){
matched = lastMatched; matched = lastMatched.slice(0,maxCount);
break; break;
} }
lastMatched = matched; lastMatched = matched;
...@@ -288,34 +299,38 @@ var extractor = function(partName, ctx, from, k, debug) { ...@@ -288,34 +299,38 @@ var extractor = function(partName, ctx, from, k, debug) {
return matched.slice(0, 2); return matched.slice(0, 2);
}*/ }*/
return matched; return matched;
})).filter(function(item) { } ) ).filter( function( item ){
if(!(item.i.vid in used)){ if( !( item.i.vid in used ) ){
used[item.i.vid] = true; used[ item.i.vid ] = true;
return true; return true;
} }
return false; return false;
}); } );
var count = all.length; var count = all.length;
draw(D.div({cls: 'matched'}, draw( D.div( { cls: 'matched opened' },
D.span({cls:'action', onClick: function() { D.span( {
this.parentNode.classList.toggle('opened'); cls: 'action', onClick: function(){
}}, 'Matched: '+count+' '+(count===1?'item':'items')), this.parentNode.classList.toggle( 'opened' );
D.div({cls: 'collapsed'}, }
all.map(function(item) { }, 'Matched: ' + count + ' ' + ( count === 1 ? 'item' : 'items' ) ),
return D.div({cls: 'row'}, D.div( { cls: 'collapsed' },
D.div({cls: 'cp1', style: {background: 'rgb('+item.c.color+')'}}), all.map( function( item ){
D.div({cls: 'cp2', style: {background: item.i.color}}), return D.div( { cls: 'row' },
D.h('img', {cls: 'preview-img', src: item.i.img}), D.div( { cls: 'cp1', style: { background: 'rgb(' + item.c.color + ')' } } ),
D.div({cls: 'name'}, item.i.title), D.div( { cls: 'cp2', style: { background: item.i.color } } ),
D.div({cls: 'accuracy'}, (Math.max(0,Math.min(100,(1-item.d*8)*100))).toFixed(2)+'%') D.h( 'img', { cls: 'preview-img', src: item.i.img } ),
D.div( { cls: 'name' }, item.i.title ),
D.div( { cls: 'accuracy' }, ( Math.max( 0, Math.min( 100, ( 1 - item.d * 3 ) * 100 ) ) ).toFixed( 2 ) + '%' )
) )
}) } )
) )
) );
} );
)); console.log(b)
binds.push(b);
}); });
......
...@@ -10,7 +10,26 @@ ...@@ -10,7 +10,26 @@
<script src="js/facemarks.js"></script> <script src="js/facemarks.js"></script>
<script src="js/extract-colors.js"></script> <script src="js/extract-colors.js"></script>
<script src="js/data.js"></script> <script src="js/data.js"></script>
<script src="js/Observer.js"></script>
<script src="js/Store.js"></script>
<style> <style>
#settings {
position: absolute;
top: 16px;
right: 128px;
}
label {
display: block;
height:32px;
}
input.field__input {
position: absolute;
left: 96px;
width: 4em;
border: 0;
border-bottom: 1px solid #999;
text-align: center;
}
.color-preview { .color-preview {
display: inline-block; display: inline-block;
font-family: monospace; font-family: monospace;
...@@ -81,6 +100,11 @@ ...@@ -81,6 +100,11 @@
vertical-align: super; vertical-align: super;
margin: 0 16px; margin: 0 16px;
} }
.block {
display: inline-block;
width: 46%;
vertical-align: top;
}
</style> </style>
<script src="js/DOM.js"></script> <script src="js/DOM.js"></script>
...@@ -89,9 +113,10 @@ ...@@ -89,9 +113,10 @@
<video onloadedmetadata="" id="inputVideo" style="opacity: 0;height:480px" autoplay muted playsinline></video> <video onloadedmetadata="" id="inputVideo" style="opacity: 0;height:480px" autoplay muted playsinline></video>
<div id="settings"></div>
<canvas id="playback" style="position: absolute;left:0"></canvas> <canvas id="playback" style="position: absolute;left:0"></canvas>
<canvas id="overlay" style="position: absolute;left:0"></canvas> <canvas id="overlay" style="position: absolute;left:0"></canvas>
<canvas id="ccolors" style="position: absolute;left:700px" width="100" height="480"></canvas> <canvas id="ccolors" style="position: absolute;left:550px" width="100" height="480"></canvas>
<div id="colors"></div> <div id="colors"></div>
<div> <div>
...@@ -102,7 +127,10 @@ ...@@ -102,7 +127,10 @@
<input type="file" id="input"/> <input type="file" id="input"/>
</div> </div>
<script> <script>
var store = new Store({
threshold: 60,
maxCount: 5
});
...@@ -427,6 +455,28 @@ var videoEl = inputVideo; ...@@ -427,6 +455,28 @@ var videoEl = inputVideo;
f() f()
} }
f0(); f0();
var Input = function(cfg){
var dom = D.h('input', {cls:"field__input", attr: {type: 'number'}});
if(cfg.bind){
cfg.bind.sub(function (val) {
dom.value = val;
});
dom.addEventListener('input', function () {
cfg.bind.set(dom.value);
})
}
return dom;
};
D.div({cls: 'settings', renderTo: document.getElementById('settings')},
D.h('label', {},
'Max count: ', D.h(Input, {bind: store.bind('maxCount')})
),
D.h('label', {},
'Threshold: ', D.h(Input, {bind: store.bind('threshold')})
)
);
</script> </script>
</body> </body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment