MCPcopy Create free account
hub / github.com/Semporia/Scripts / recognize

Method recognize

JDJRValidator_Pure.js:74–127  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

72 }
73
74 recognize() {
75 const {ctx, w: width, bg} = this;
76 const {width: patchWidth, height: patchHeight} = this.patch;
77 const posY = this.y + PUZZLE_PAD + ((patchHeight - PUZZLE_PAD) / 2) - (PUZZLE_GAP / 2);
78 // const cData = ctx.getImageData(0, a.y + 10 + 20 - 4, 360, 8).data;
79 const cData = bg.getImageData(0, posY, width, PUZZLE_GAP).data;
80 const lumas = [];
81
82 for (let x = 0; x < width; x++) {
83 var sum = 0;
84
85 // y xais
86 for (let y = 0; y < PUZZLE_GAP; y++) {
87 var idx = x * 4 + y * (width * 4);
88 var r = cData[idx];
89 var g = cData[idx + 1];
90 var b = cData[idx + 2];
91 var luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
92
93 sum += luma;
94 }
95
96 lumas.push(sum / PUZZLE_GAP);
97 }
98
99 const n = 2; // minium macroscopic image width (px)
100 const margin = patchWidth - PUZZLE_PAD;
101 const diff = 20; // macroscopic brightness difference
102 const radius = PUZZLE_PAD;
103 for (let i = 0, len = lumas.length - 2 * 4; i < len; i++) {
104 const left = (lumas[i] + lumas[i + 1]) / n;
105 const right = (lumas[i + 2] + lumas[i + 3]) / n;
106 const mi = margin + i;
107 const mLeft = (lumas[mi] + lumas[mi + 1]) / n;
108 const mRigth = (lumas[mi + 2] + lumas[mi + 3]) / n;
109
110 if (left - right > diff && mLeft - mRigth < -diff) {
111 const pieces = lumas.slice(i + 2, margin + i + 2);
112 const median = pieces.sort((x1, x2) => x1 - x2)[20];
113 const avg = Math.avg(pieces);
114
115 // noise reducation
116 if (median > left || median > mRigth) return;
117 if (avg > 100) return;
118 // console.table({left,right,mLeft,mRigth,median});
119 // ctx.fillRect(i+n-radius, 0, 1, 360);
120 // console.log(i+n-radius);
121 return i + n - radius;
122 }
123 }
124
125 // not found
126 return -1;
127 }
128
129 runWithCanvas() {
130 const {createCanvas, Image} = require('canvas');

Callers 1

runMethod · 0.95

Calls 1

getImageDataMethod · 0.45

Tested by

no test coverage detected