reports/iftomm_dach/code/bin/mec.deepmech.js
/**
             * Add handwritten nodes to mec
             * @param {object} image (tensor) which contains the image on the canvas. 
             * @param {object} nodeDetector model which detects nodes.
             */
Add handwritten nodes to mec
@param {object} image (tensor) which contains the image on the canvas.
@param {object} nodeDetector model which detects nodes.
Add handwritten nodes to mec @param { object } image ( tensor ) which contains the image on the canvas . @param { object } nodeDetector model which detects nodes .
detectNodes(image, nodeDetector) {
                const prediction = nodeDetector.predict(image, { batch_size: 1 }).arraySync()[0];
                function extractInterestingInfo(pred, idx) {
                    const y = idx * 4;
                    return pred.flatMap((inner, i) => {
                        const max = Math.max(...inner);
                        const maxIndex = inner.indexOf(max);
                        const x = i * 4;
                        return { x, y, max, maxIndex, b: 32, h: 32 }
                    });
                }
                function removeOverlaps(boxes) {
                    const mostAccurate = [];
                    while (boxes.length) {
                        const max = boxes.pop();
                        mostAccurate.push(max);
                        boxes = boxes.filter(rec =>
                            Math.abs(rec.x - max.x) >= rec.b ||
                            Math.abs(rec.y - max.y) >= rec.h);
                    }
                    return mostAccurate;
                }
                const nodes = removeOverlaps(prediction
                    .flatMap(extractInterestingInfo)
                    .filter(e => e.maxIndex).sort((a, b) => a.max - b.max));
                return nodes;
            }
detectNodes ( image , nodeDetector ) { const prediction = nodeDetector . predict ( image , { batch_size : 1 } ) . arraySync ( ) [ 0 ] ; function extractInterestingInfo ( pred , idx ) { const y = idx * 4 ; return pred . flatMap ( ( inner , i ) => { const max = Math . max ( ... inner ) ; const maxIndex = inner . indexOf ( max ) ; const x = i * 4 ; return { x , y , max , maxIndex , b : 32 , h : 32 } } ) ; } function removeOverlaps ( boxes ) { const mostAccurate = [ ] ; while ( boxes . length ) { const max = boxes . pop ( ) ; mostAccurate . push ( max ) ; boxes = boxes . filter ( rec => Math . abs ( rec . x - max . x ) >= rec . b || Math . abs ( rec . y - max . y ) >= rec . h ) ; } return mostAccurate ; } const nodes = removeOverlaps ( prediction . flatMap ( extractInterestingInfo ) . filter ( e => e . maxIndex ) . sort ( ( a , b ) => a . max - b . max ) ) ; return nodes ; }
Add handwritten nodes to mec
 @param {object} image (tensor) which contains the image on the canvas.
Add handwritten nodes to mec @param { object } image ( tensor ) which contains the image on the canvas .