Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: j-isolate removed and new training data #6

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added scripts/images/07pruu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/0yce8w.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/3f4kx6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/3m0y55.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/4thho3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/53bajm
Binary file not shown.
Binary file removed scripts/images/58omry
Binary file not shown.
Binary file added scripts/images/5uo4v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/7cphau
Binary file not shown.
Binary file added scripts/images/8yhn5j.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/90r68s
Binary file not shown.
Binary file added scripts/images/98qvx4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/c4mq1e.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/coj1g8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/d0s7b7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/dypcre.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/ebuxw9
Binary file not shown.
Binary file added scripts/images/g4pqnh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/gewxcn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/gmypjq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/hmq57a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/hqextb
Binary file not shown.
Binary file removed scripts/images/j3yjiv
Binary file not shown.
Binary file added scripts/images/ki2y2u.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/kj64tw
Binary file not shown.
Binary file removed scripts/images/ns7gvg
Binary file not shown.
Binary file added scripts/images/o1kqtu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/ohx30s.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/ph1rav.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/pq0bf1
Binary file not shown.
Binary file removed scripts/images/pv42m7
Binary file not shown.
Binary file added scripts/images/qcenjg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/r3oemy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/r99xiv
Binary file not shown.
Binary file added scripts/images/rmpvgd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added scripts/images/t6dk52.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/uoy1oe
Binary file not shown.
Binary file added scripts/images/uq3n51.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed scripts/images/vqx1dt
Binary file not shown.
Binary file added scripts/images/x2g0yn.png
Binary file added scripts/images/x3qvbg.png
59 changes: 34 additions & 25 deletions src/content-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {Image} from "image-js";
import data from "./data.json";
import {evaluate_captcha} from "./evaluate";

let factors = [ 1, 3, 2, 8, 3 ];
const FACTORS = [ 1, 3, 2, 8, 3 ];

let reload_limit = 15;
const RELOAD_LIMIT = 15;

let old_urls: Array<any> = new Array();

Expand All @@ -21,14 +21,16 @@ async function async_sleep(time: number) {
}

function reload_captcha() {
document?.querySelector('[aria-label="Reload captcha"]')
?.dispatchEvent(new Event("click"));
setTimeout(() => {
document?.querySelector('[aria-label="Reload captcha"]')
?.dispatchEvent(new Event("click"));
}, 100);
}

document?.querySelector('[aria-label="Reload captcha"]')
?.addEventListener("click", async () => {
reload_counter++;
if (reload_counter < reload_limit) {
if (reload_counter < RELOAD_LIMIT) {
while (old_urls.includes(
document.getElementsByClassName("captcha-image-dimension")[0]
.getAttribute("src"))) {
Expand All @@ -53,34 +55,41 @@ async function solve_captcha() {
let captcha_img = await Image.load(captcha_blob_url);
captcha_value = await evaluate_captcha(captcha_img);

if (captcha_value.length == 6) {
let captcha = "";
captcha_value.map((item) => {
let sim: Array<[ string, number ]> = Object.entries(data).map((item2) => {
let absSum = 0;
item.map((indvItem, index) => {
absSum += factors[index] * Math.abs(item2[1][index] - indvItem);
});
return [ item2[0], absSum ];
let captcha = "";
captcha_value.map((item) => {
let sim: Array<[ string, number ]> = Object.entries(data).map((item2) => {
let absSum = 0;
item.map((indvItem, index) => {
absSum += FACTORS[index] * Math.abs(item2[1][index] - indvItem);
});
return [ item2[0], absSum ];
});

let sorted_values = sim.sort((a, b) => a[1] - b[1]);
let sorted_values = sim.sort((a, b) => a[1] - b[1]);

if (sorted_values[0][1] > 60 ||
sorted_values[1][1] - sorted_values[0][1] < 5) {
reload_captcha();
return false;
}
captcha += sorted_values[0][0];
});
if (sorted_values[0][1] > 60 ||
sorted_values[1][1] - sorted_values[0][1] < 5) {
console.log(`Confidence is low! Got value '${captcha}'. Reloading!`);
reload_captcha();
return false;
}
captcha += sorted_values[0][0];
});

console.log(captcha);
if (captcha_value.length == 6) {
let captcha_field =
document?.getElementById("captchaEnter") as HTMLInputElement;

captcha_field.value = captcha;

document?.getElementById("captchaEnter")?.setAttribute("value", captcha);
document?.getElementById("captchaEnter")?.dispatchEvent(new Event("input"));
captcha_field?.dispatchEvent(new Event("input"));

return true;
} else {
console.log(
`Failed to solve! Length < 6. Obtained value '${captcha}'. Reloading!`,
);
await async_sleep(100);
reload_captcha();
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/data.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"0":[133.4765625,69.228515625,83.671875,49.8046875,15],"1":[84.66796875,56.11328125,46.15234375,38.515625,11],"2":[112.55859375,56.77734375,57.7734375,54.78515625,14],"3":[105.087890625,38.84765625,66.240234375,38.84765625,13],"4":[115.048828125,39.84375,67.236328125,47.8125,15],"5":[112.55859375,54.78515625,75.703125,36.85546875,13],"6":[129.4921875,73.212890625,81.181640625,48.310546875,15],"7":[84.66796875,48.4765625,62.75390625,21.9140625,13],"8":[155.390625,79.6875,98.61328125,56.77734375,15],"9":[129.4921875,62.0068359375,94.62890625,34.86328125,15],"b":[133.7255859375,86.4111328125,78.193359375,55.5322265625,14],"a":[107.080078125,53.7890625,41.8359375,65.244140625,14],"j":[80.68359375,9.9609375,31.875,48.80859375,9],"m":[163.359375,92.138671875,93.134765625,70.224609375,21],"r":[58.76953125,50.80078125,35.361328125,23.408203125,9],"s":[80.185546875,44.326171875,40.341796875,39.84375,11],"c":[62.75390625,43.828125,30.87890625,31.875,11],"p":[130.48828125,83.33984375,57.109375,73.37890625,14],"h":[125.5078125,76.201171875,78.69140625,46.81640625,13],"u":[106.25,51.46484375,46.15234375,60.09765625,13],"q":[131.81640625,54.12109375,55.1171875,76.69921875,14],"e":[94.62890625,55.44921875,60.09765625,34.53125,13],"x":[74.70703125,41.5869140625,36.357421875,38.349609375,14],"t":[68.73046875,56.4453125,42.16796875,26.5625,8],"y":[88.65234375,58.76953125,39.84375,48.80859375,16],"i":[56.77734375,37.8515625,32.87109375,23.90625,3],"v":[74.90625,46.01953125,40.04296875,34.86328125,15],"w":[151.904296875,79.6875,78.193359375,73.7109375,23],"n":[105.5859375,54.78515625,58.76953125,46.81640625,13],"g":[146.923828125,69.228515625,56.279296875,90.64453125,14],"k":[106.58203125,85.6640625,59.765625,46.81640625,15],"f":[82.67578125,67.734375,61.7578125,20.91796875,11],"d":[138.45703125,56.77734375,78.69140625,59.765625,14],"o":[95.625,51.796875,47.314453125,48.310546875,14]}
{"0":[149.580078125,77.03125,97.451171875,52.12890625,15],"1":[107.9765625,71.91796875,57.97265625,50.00390625,13],"2":[132.6796875,68.33203125,76.69921875,55.98046875,14],"3":[123.37332589285714,55.21205357142857,76.13002232142857,47.24330357142857,14],"4":[133.974609375,55.283203125,87.98828125,45.986328125,16],"5":[136.18024553571428,78.12220982142857,91.21372767857143,44.966517857142854,14],"6":[158.876953125,88.154296875,107.080078125,51.796875,15],"7":[98.3642578125,50.302734375,75.9521484375,22.412109375,14],"8":[167.841796875,89.6484375,110.56640625,57.275390625,15],"9":[158.37890625,78.69140625,114.55078125,43.828125,15],"y":[117.91259765625,78.81591796875,56.90185546875,61.0107421875,15.875],"c":[90.64453125,55.78125,47.48046875,43.1640625,13],"e":[135.80078125,74.375,90.478515625,45.322265625,15],"w":[153.896484375,82.177734375,91.640625,62.255859375,22],"p":[147.421875,92.802734375,70.72265625,76.69921875,15],"r":[64.1484375,52.79296875,43.23046875,20.91796875,10],"u":[110.234375,56.4453125,52.12890625,58.10546875,14],"m":[169.169921875,94.9609375,107.24609375,61.923828125,23],"o":[114.384765625,60.263671875,62.08984375,52.294921875,15],"v":[94.03125,51.3984375,54.984375,39.046875,16],"t":[98.9453125,69.7265625,62.75390625,36.19140625,11],"h":[131.62667410714286,80.96819196428571,90.50223214285714,41.124441964285715,14],"f":[98.61328125,70.72265625,77.6953125,20.91796875,11],"k":[133.2275390625,98.61328125,84.9169921875,48.310546875,14],"x":[106.58203125,57.939453125,55.947265625,50.634765625,16],"q":[146.97916666666666,60.540364583333336,70.27994791666667,76.69921875,15],"n":[110.56640625,59.931640625,69.7265625,40.83984375,14],"j":[76.69921875,32.87109375,34.86328125,41.8359375,6],"g":[166.47216796875,76.69921875,68.73046875,97.74169921875,15],"d":[147.919921875,59.765625,89.8974609375,58.0224609375,15],"s":[100.107421875,59.267578125,57.7734375,42.333984375,12],"b":[146.923828125,92.63671875,89.6484375,57.275390625,15],"i":[54.78515625,36.85546875,33.8671875,20.91796875,3],"a":[133.974609375,68.232421875,74.70703125,59.267578125,14]}
77 changes: 18 additions & 59 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import {Image} from "image-js";

let SAMPLE_J = "assets/sample-j.png";
let EMPTY = "assets/empty.jpg";

if (typeof window === "object") {
SAMPLE_J = chrome.runtime.getURL(SAMPLE_J);
EMPTY = chrome.runtime.getURL(EMPTY);
} else {
SAMPLE_J = `./src/${SAMPLE_J}`;
EMPTY = `./src/${EMPTY}`;
}

/*
Image is first splitted into individual characters by finding empty line between
characters.

Then each character is evalauated based on 5 factors:
- Average Pixel Value
- Horizontal Length of Image
- Average Pixel of Vertical Left Half of Image
- Average Pixel of Horizontal Top Half of Image
- Average Pixel of Horizontal Bottom Half of Image
*/
async function evaluate_captcha(img: Image): Promise<Array<Array<number>>> {
let jd = (await Image.load(SAMPLE_J)).grey();

let cleaned = await clean_image(img);

let counter = 0;
let matrix = [];
let matrix_list = [];

// Splitting images by characters
for (let i = 0; i < 130; i++) {
let columnEmpty = true;
for (let j = 0; j < 35; j++) {
Expand Down Expand Up @@ -53,38 +60,13 @@ async function evaluate_captcha(img: Image): Promise<Array<Array<number>>> {
let hAvg = htopavg(temp_img);
let hbtAvg = hbotavg(temp_img);

if (average > 165) {
let clean_char = to_image(cutoff_j(char_mat)).rotateRight().flipX();

let clean_char_sum_avg =
clean_char.getSum().reduce((acc, val) => {return acc + val}) / 256;

averages.push([
clean_char_sum_avg,
vavg(clean_char),
htopavg(clean_char),
hbotavg(clean_char),
clean_char.width,
]);

let jd_char_sum_avg =
jd.getSum().reduce((acc, val) => {return acc + val}) / 256;

averages.push([
jd_char_sum_avg,
vavg(jd),
htopavg(jd),
hbotavg(jd),
jd.width,
]);
} else {
averages.push([ average, vAvg, hAvg, hbtAvg, char_mat.length / 35 ]);
}
averages.push([ average, vAvg, hAvg, hbtAvg, char_mat.length / 35 ]);
});

return averages;
}

// Pixel array to Image
function to_image(matrix: Array<number>, width = 35) {
let image = new Image(width, matrix.length / width).grey();

Expand All @@ -97,6 +79,7 @@ function to_image(matrix: Array<number>, width = 35) {
return image;
}

// Subtract the background noise image
async function clean_image(img: Image) {
let empty = (await Image.load(EMPTY)).grey();
let data = img.grey();
Expand All @@ -115,33 +98,7 @@ async function clean_image(img: Image) {
return cleaned;
}

function cutoff_j(matrix: Array<number>) {
matrix.splice(matrix.length - 105, 105);
for (let i = 0; i <= 5; i++) {
for (let j = 0; j <= 6; j++) {
matrix[matrix.length - 35 * j - i - 1] = 0;
}
}

let col_counter = 0;
for (let i = 0; i < matrix.length / 35; i++) {
let columnEmpty = true;
for (let j = 0; j < 35; j++) {
if (matrix[matrix.length - i * 35 - j - 1]) {
columnEmpty = false;
break;
}
}
if (!columnEmpty) {
break;
}
col_counter++;
}

matrix.splice(matrix.length - col_counter * 35, col_counter * 35);
return matrix;
}

// Average pixel value of horizontal top half
function htopavg(char_img: Image) {
let temp_img = char_img.crop({
y : 0,
Expand All @@ -153,6 +110,7 @@ function htopavg(char_img: Image) {
return (temp_img.getSum().reduce((acc, val) => acc + val) / 256);
}

// Average pixel value of horizontal bottom half
function hbotavg(char_img: Image) {
let temp_img = char_img.crop({
y : Math.ceil(char_img.height / 2 + 1),
Expand All @@ -164,6 +122,7 @@ function hbotavg(char_img: Image) {
return (temp_img.getSum().reduce((acc, val) => acc + val) / 256);
}

// Average pixel value of vertical half
function vavg(char_img: Image) {
let transformed_image = char_img.rotateRight();

Expand Down