-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvision_engine.js
More file actions
149 lines (124 loc) · 4.88 KB
/
vision_engine.js
File metadata and controls
149 lines (124 loc) · 4.88 KB
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
import 'dotenv/config';
import axios from 'axios';
import { readFileSync } from 'fs';
// CapSolver API configuration
const CAPSOLVER_API_KEY = process.env.CAPSOLVER_API_KEY;
const CREATE_TASK_URL = 'https://api.capsolver.com/createTask';
// Vision Engine modules
const MODULES = {
slider_1: 'Slider captcha - requires image and imageBackground',
rotate_1: 'Rotation detection - requires image and imageBackground',
rotate_2: 'Standalone rotation - requires only image',
shein: 'Multi-object selection with coordinates - requires image and question'
};
/**
* Solve slider captcha.
* @param {string} imageBase64 - Base64 encoded slider piece image
* @param {string} backgroundBase64 - Base64 encoded background image
* @returns {Promise<Object>} Solution containing distance to slide
*/
async function solveSlider(imageBase64, backgroundBase64) {
const payload = {
clientKey: CAPSOLVER_API_KEY,
task: {
type: 'VisionEngine',
module: 'slider_1',
image: imageBase64,
imageBackground: backgroundBase64
}
};
const response = await axios.post(CREATE_TASK_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Failed to solve: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Solve rotation captcha.
* @param {string} imageBase64 - Base64 encoded image to rotate
* @param {string} backgroundBase64 - Base64 encoded background (required for rotate_1)
* @param {string} module - "rotate_1" (with background) or "rotate_2" (standalone)
* @returns {Promise<Object>} Solution containing angle to rotate
*/
async function solveRotate(imageBase64, backgroundBase64 = null, module = 'rotate_1') {
const payload = {
clientKey: CAPSOLVER_API_KEY,
task: {
type: 'VisionEngine',
module: module,
image: imageBase64
}
};
if (backgroundBase64 && module === 'rotate_1') {
payload.task.imageBackground = backgroundBase64;
}
const response = await axios.post(CREATE_TASK_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Failed to solve: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Solve Shein-style multi-object selection captcha.
* @param {string} imageBase64 - Base64 encoded image
* @param {string} question - The object type to identify
* @returns {Promise<Object>} Solution containing rects (coordinate arrays)
*/
async function solveShein(imageBase64, question) {
const payload = {
clientKey: CAPSOLVER_API_KEY,
task: {
type: 'VisionEngine',
module: 'shein',
image: imageBase64,
question: question
}
};
const response = await axios.post(CREATE_TASK_URL, payload);
const result = response.data;
if (result.errorId !== 0) {
throw new Error(`Failed to solve: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Helper to load image as base64.
* @param {string} imagePath - File path
* @returns {string} Base64 encoded image
*/
function loadImageAsBase64(imagePath) {
const imageBuffer = readFileSync(imagePath);
return imageBuffer.toString('base64');
}
async function main() {
if (!CAPSOLVER_API_KEY) {
console.log('Error: CAPSOLVER_API_KEY not found in .env file');
console.log('Please create a .env file with your API key:');
console.log('CAPSOLVER_API_KEY=your_api_key_here');
return;
}
console.log('Vision Engine - Slider/Rotate Captcha Solver');
console.log('\nAvailable modules:');
for (const [module, desc] of Object.entries(MODULES)) {
console.log(` ${module}: ${desc}`);
}
console.log('\nExample usage:');
console.log('\n // Slider captcha:');
console.log(' const sliderImg = loadImageAsBase64("slider.png");');
console.log(' const bgImg = loadImageAsBase64("background.png");');
console.log(' const solution = await solveSlider(sliderImg, bgImg);');
console.log(' // Returns: { distance: 150 } // pixels to slide');
console.log('\n // Rotation captcha (with background):');
console.log(" const solution = await solveRotate(imageBase64, backgroundBase64, 'rotate_1');");
console.log(' // Returns: { angle: 45 } // degrees to rotate');
console.log('\n // Rotation captcha (standalone):');
console.log(" const solution = await solveRotate(imageBase64, null, 'rotate_2');");
console.log(' // Returns: { angle: 90 }');
console.log('\n // Shein multi-object selection:');
console.log(' const solution = await solveShein(imageBase64, "Select all shoes");');
console.log(' // Returns: { rects: [[x1,y1,x2,y2], [x1,y1,x2,y2]] } // object coordinates');
}
main();
export { solveSlider, solveRotate, solveShein, loadImageAsBase64, MODULES };