-
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) · 5.04 KB
/
vision_engine.js
File metadata and controls
149 lines (124 loc) · 5.04 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';
// Configuracion de la API de CapSolver
const CAPSOLVER_API_KEY = process.env.CAPSOLVER_API_KEY;
const CREATE_TASK_URL = 'https://api.capsolver.com/createTask';
// Modulos de Vision Engine
const MODULES = {
slider_1: 'Captcha slider - requiere imagen e imagenFondo',
rotate_1: 'Deteccion de rotacion - requiere imagen e imagenFondo',
rotate_2: 'Rotacion independiente - requiere solo imagen',
shein: 'Seleccion multi-objeto con coordenadas - requiere imagen y pregunta'
};
/**
* Resolver captcha de slider.
* @param {string} imageBase64 - Imagen de la pieza del slider codificada en Base64
* @param {string} backgroundBase64 - Imagen del fondo codificada en Base64
* @returns {Promise<Object>} Solucion conteniendo la distancia a deslizar
*/
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(`Error al resolver: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Resolver captcha de rotacion.
* @param {string} imageBase64 - Imagen a rotar codificada en Base64
* @param {string} backgroundBase64 - Fondo codificado en Base64 (requerido para rotate_1)
* @param {string} module - "rotate_1" (con fondo) o "rotate_2" (independiente)
* @returns {Promise<Object>} Solucion conteniendo el angulo a rotar
*/
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(`Error al resolver: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Resolver captcha de seleccion multi-objeto estilo Shein.
* @param {string} imageBase64 - Imagen codificada en Base64
* @param {string} question - El tipo de objeto a identificar
* @returns {Promise<Object>} Solucion conteniendo rects (arrays de coordenadas)
*/
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(`Error al resolver: ${result.errorDescription}`);
}
return result.solution || {};
}
/**
* Funcion auxiliar para cargar imagen como base64.
* @param {string} imagePath - Ruta del archivo
* @returns {string} Imagen codificada en Base64
*/
function loadImageAsBase64(imagePath) {
const imageBuffer = readFileSync(imagePath);
return imageBuffer.toString('base64');
}
async function main() {
if (!CAPSOLVER_API_KEY) {
console.log('Error: CAPSOLVER_API_KEY no encontrada en el archivo .env');
console.log('Por favor, crea un archivo .env con tu clave API:');
console.log('CAPSOLVER_API_KEY=tu_clave_api_aqui');
return;
}
console.log('Vision Engine - Resolutor de Captcha Slider/Rotacion');
console.log('\nModulos disponibles:');
for (const [module, desc] of Object.entries(MODULES)) {
console.log(` ${module}: ${desc}`);
}
console.log('\nEjemplo de uso:');
console.log('\n // Captcha slider:');
console.log(' const sliderImg = loadImageAsBase64("slider.png");');
console.log(' const bgImg = loadImageAsBase64("fondo.png");');
console.log(' const solution = await solveSlider(sliderImg, bgImg);');
console.log(' // Devuelve: { distance: 150 } // pixeles a deslizar');
console.log('\n // Captcha de rotacion (con fondo):');
console.log(" const solution = await solveRotate(imageBase64, backgroundBase64, 'rotate_1');");
console.log(' // Devuelve: { angle: 45 } // grados a rotar');
console.log('\n // Captcha de rotacion (independiente):');
console.log(" const solution = await solveRotate(imageBase64, null, 'rotate_2');");
console.log(' // Devuelve: { angle: 90 }');
console.log('\n // Seleccion multi-objeto Shein:');
console.log(' const solution = await solveShein(imageBase64, "Selecciona todos los zapatos");');
console.log(' // Devuelve: { rects: [[x1,y1,x2,y2], [x1,y1,x2,y2]] } // coordenadas de objetos');
}
main();
export { solveSlider, solveRotate, solveShein, loadImageAsBase64, MODULES };