@yigitahmetsahin/captcha-solver 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,7 +9,7 @@ AI-powered captcha solver using image preprocessing and OpenAI vision models wit
9
9
  ## Features
10
10
 
11
11
  - **AI Vision OCR** - Uses OpenAI vision models (o3, gpt-4o, etc.) to read distorted captcha text
12
- - **Image Preprocessing** - PIL-based pipeline: grayscale, blur, upscale, contrast/sharpness enhancement, cropping
12
+ - **Image Preprocessing** - Sharp/libvips pipeline: grayscale, blur, upscale, contrast/sharpness enhancement, cropping
13
13
  - **Majority Voting** - Runs multiple attempts and uses character-level majority voting for accuracy
14
14
  - **Configurable** - Adjustable model, attempt count, expected length, and verbosity
15
15
  - **TypeScript** - Full type safety with strict mode
@@ -17,7 +17,6 @@ AI-powered captcha solver using image preprocessing and OpenAI vision models wit
17
17
  ## Prerequisites
18
18
 
19
19
  - Node.js >= 18
20
- - Python 3 with PIL/Pillow (`pip install Pillow`)
21
20
  - OpenAI API key
22
21
 
23
22
  ## Installation
@@ -82,7 +81,7 @@ npm run benchmark
82
81
 
83
82
  ## How It Works
84
83
 
85
- 1. **Preprocessing** - The image is processed through a PIL pipeline:
84
+ 1. **Preprocessing** - The image is processed through a sharp (libvips) pipeline:
86
85
  - Convert to grayscale
87
86
  - Apply Gaussian blur to smooth noise
88
87
  - Upscale 4x with Lanczos interpolation
package/dist/index.cjs CHANGED
@@ -41,38 +41,34 @@ var import_openai = __toESM(require("openai"), 1);
41
41
 
42
42
  // src/preprocess.ts
43
43
  var import_fs = __toESM(require("fs"), 1);
44
- var import_child_process = require("child_process");
45
44
  var import_path = __toESM(require("path"), 1);
46
- var PYTHON_SCRIPT = `
47
- import sys, base64, io
48
- from PIL import Image, ImageFilter, ImageEnhance, ImageOps
49
-
50
- image_path = sys.argv[1]
51
- img = Image.open(image_path)
52
- img = ImageOps.grayscale(img)
53
- img = img.filter(ImageFilter.GaussianBlur(radius=1.2))
54
- img = img.resize((img.width * 4, img.height * 4), Image.LANCZOS)
55
- img = ImageEnhance.Contrast(img).enhance(3.0)
56
- img = ImageEnhance.Sharpness(img).enhance(2.0)
57
- w, h = img.size
58
- img = img.crop((int(w * 0.10), int(h * 0.02), int(w * 0.90), int(h * 0.60)))
59
- padded = Image.new('L', (img.width + 60, img.height + 40), 255)
60
- padded.paste(img, (30, 20))
61
- padded = padded.convert('RGB')
62
- buf = io.BytesIO()
63
- padded.save(buf, format='PNG')
64
- sys.stdout.buffer.write(base64.b64encode(buf.getvalue()))
65
- `;
45
+ var import_sharp = __toESM(require("sharp"), 1);
66
46
  async function preprocessCaptcha(imagePath) {
67
47
  const absPath = import_path.default.resolve(imagePath);
68
- const scriptPath = "/tmp/_captcha_preprocess.py";
69
- import_fs.default.writeFileSync(scriptPath, PYTHON_SCRIPT);
70
- const result = (0, import_child_process.execSync)(`python3 "${scriptPath}" "${absPath}"`, {
71
- maxBuffer: 10 * 1024 * 1024,
72
- // 10MB
73
- encoding: "utf-8"
74
- });
75
- return result.trim();
48
+ const metadata = await (0, import_sharp.default)(absPath).metadata();
49
+ const origW = metadata.width;
50
+ const origH = metadata.height;
51
+ const smoothed = await (0, import_sharp.default)(absPath).blur(1.5).greyscale().toBuffer();
52
+ const upscaled = await (0, import_sharp.default)(smoothed).resize(origW * 4, origH * 4, { kernel: "lanczos3" }).toBuffer();
53
+ const stats = await (0, import_sharp.default)(upscaled).stats();
54
+ const mean = stats.channels[0].mean;
55
+ const enhanced = await (0, import_sharp.default)(upscaled).linear(3, mean * (1 - 3)).sharpen({ sigma: 1, m1: 2, m2: 1 }).toBuffer();
56
+ const scaledW = origW * 4;
57
+ const scaledH = origH * 4;
58
+ const cropLeft = Math.floor(scaledW * 0.1);
59
+ const cropTop = Math.floor(scaledH * 0.02);
60
+ const cropRight = Math.floor(scaledW * 0.9);
61
+ const cropBottom = Math.floor(scaledH * 0.6);
62
+ const cropW = cropRight - cropLeft;
63
+ const cropH = cropBottom - cropTop;
64
+ const result = await (0, import_sharp.default)(enhanced).extract({ left: cropLeft, top: cropTop, width: cropW, height: cropH }).extend({
65
+ top: 20,
66
+ bottom: 20,
67
+ left: 30,
68
+ right: 30,
69
+ background: { r: 255, g: 255, b: 255 }
70
+ }).png().toBuffer();
71
+ return result.toString("base64");
76
72
  }
77
73
  function imageToBase64(imagePath) {
78
74
  const buffer = import_fs.default.readFileSync(imagePath);
@@ -82,7 +78,7 @@ function imageToBase64(imagePath) {
82
78
  // src/solver.ts
83
79
  var PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.
84
80
  The text contains uppercase letters A-Z and/or digits 0-9.
85
- A thin vertical stroke is likely the digit 1, not the letter I.
81
+ A thin vertical stroke is the digit 1. Never read it as the letter I or L.
86
82
  A round closed shape is the letter O, not the letter D.
87
83
  Output ONLY the exact characters you read, nothing else.`;
88
84
  async function singleAttempt(client, base64Image, model, maxRetries) {
@@ -126,6 +122,22 @@ async function singleAttempt(client, base64Image, model, maxRetries) {
126
122
  }
127
123
  return null;
128
124
  }
125
+ var CONFUSION_GROUPS = {
126
+ // Thin vertical strokes → digit 1
127
+ "1": "1",
128
+ I: "1",
129
+ L: "1",
130
+ // Round shapes → letter O
131
+ O: "O",
132
+ D: "O",
133
+ "0": "O",
134
+ // Similar curves
135
+ S: "S",
136
+ "5": "S",
137
+ // Straight edges
138
+ Z: "Z",
139
+ "2": "Z"
140
+ };
129
141
  function majorityVote(attempts, expectedLength) {
130
142
  let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;
131
143
  if (filtered.length === 0) {
@@ -153,15 +165,20 @@ function majorityVote(attempts, expectedLength) {
153
165
  const ch = a[pos];
154
166
  charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);
155
167
  }
156
- let bestChar = "";
157
- let bestCharCount = 0;
168
+ const groupCounts = /* @__PURE__ */ new Map();
158
169
  for (const [ch, count] of charCounts) {
159
- if (count > bestCharCount) {
160
- bestChar = ch;
161
- bestCharCount = count;
170
+ const canonical = CONFUSION_GROUPS[ch] ?? ch;
171
+ groupCounts.set(canonical, (groupCounts.get(canonical) ?? 0) + count);
172
+ }
173
+ let bestGroup = "";
174
+ let bestGroupCount = 0;
175
+ for (const [canonical, count] of groupCounts) {
176
+ if (count > bestGroupCount) {
177
+ bestGroup = canonical;
178
+ bestGroupCount = count;
162
179
  }
163
180
  }
164
- result.push(bestChar);
181
+ result.push(bestGroup);
165
182
  }
166
183
  return result.join("");
167
184
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/solver.ts","../src/preprocess.ts"],"sourcesContent":["export { solveCaptchaImage } from './solver.js';\nexport { preprocessCaptcha, imageToBase64 } from './preprocess.js';\n","import OpenAI from 'openai';\nimport { preprocessCaptcha } from './preprocess.js';\n\nconst PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.\nThe text contains uppercase letters A-Z and/or digits 0-9.\nA thin vertical stroke is likely the digit 1, not the letter I.\nA round closed shape is the letter O, not the letter D.\nOutput ONLY the exact characters you read, nothing else.`;\n\ninterface SolverOptions {\n /** OpenAI model to use (default: \"o3\") */\n model?: string;\n /** Number of voting attempts (default: 5) */\n numAttempts?: number;\n /** Expected captcha length — results of other lengths are discarded (default: undefined = no filter) */\n expectedLength?: number;\n /** Max retries per attempt on API failure (default: 2) */\n maxRetries?: number;\n /** Whether to log attempt details (default: true) */\n verbose?: boolean;\n}\n\n/**\n * Make a single API call to read the captcha.\n * Retries up to `maxRetries` times on failure.\n */\nasync function singleAttempt(\n client: OpenAI,\n base64Image: string,\n model: string,\n maxRetries: number\n): Promise<string | null> {\n for (let retry = 0; retry <= maxRetries; retry++) {\n try {\n // Reasoning models (o3, o4-mini) use max_completion_tokens;\n // Standard models (gpt-4o, gpt-4.1, gpt-5.4-mini) use max_tokens.\n const isReasoningModel = model.startsWith('o');\n const tokenParam = isReasoningModel ? { max_completion_tokens: 2000 } : { max_tokens: 256 };\n\n const response = await client.chat.completions.create({\n model,\n messages: [\n {\n role: 'user',\n content: [\n { type: 'text', text: PROMPT },\n {\n type: 'image_url',\n image_url: {\n url: `data:image/png;base64,${base64Image}`,\n },\n },\n ],\n },\n ],\n temperature: 1,\n ...tokenParam,\n });\n\n const raw = response.choices[0]?.message?.content?.trim() ?? '';\n\n // Detect refusals\n const lower = raw.toLowerCase();\n if (\n lower.includes('sorry') ||\n lower.includes(\"can't help\") ||\n lower.includes('cannot help') ||\n lower.includes('unable to') ||\n lower.includes(\"i can't\") ||\n raw.length > 20\n ) {\n return null; // Model refused — don't count as an attempt\n }\n\n // Clean: keep only uppercase letters and digits\n const cleaned = raw.toUpperCase().replace(/[^A-Z0-9]/g, '');\n return cleaned || null;\n } catch (_err) {\n if (retry < maxRetries) {\n // Wait briefly before retry\n await new Promise((r) => setTimeout(r, 1000 * (retry + 1)));\n continue;\n }\n return null;\n }\n }\n return null;\n}\n\n/**\n * Character-level majority vote across multiple attempts.\n */\nfunction majorityVote(attempts: string[], expectedLength?: number): string {\n // Filter to expected length if specified\n let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;\n\n // If length filter removed everything, fall back to most common length\n if (filtered.length === 0) {\n filtered = attempts;\n }\n\n if (filtered.length === 0) return '';\n\n // Find most common length\n const lenCounts = new Map<number, number>();\n for (const a of filtered) {\n lenCounts.set(a.length, (lenCounts.get(a.length) ?? 0) + 1);\n }\n let bestLen = 0;\n let bestCount = 0;\n for (const [len, count] of lenCounts) {\n if (count > bestCount) {\n bestLen = len;\n bestCount = count;\n }\n }\n\n const sameLenAttempts = filtered.filter((a) => a.length === bestLen);\n if (sameLenAttempts.length === 0) return filtered[0];\n\n // Vote per character position\n const result: string[] = [];\n for (let pos = 0; pos < bestLen; pos++) {\n const charCounts = new Map<string, number>();\n for (const a of sameLenAttempts) {\n const ch = a[pos];\n charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);\n }\n let bestChar = '';\n let bestCharCount = 0;\n for (const [ch, count] of charCounts) {\n if (count > bestCharCount) {\n bestChar = ch;\n bestCharCount = count;\n }\n }\n result.push(bestChar);\n }\n\n return result.join('');\n}\n\n/**\n * Solve a captcha image using OpenAI vision + preprocessing + majority voting.\n */\nexport async function solveCaptchaImage(\n imagePath: string,\n options: SolverOptions = {}\n): Promise<string> {\n const { model = 'o3', numAttempts = 5, expectedLength, maxRetries = 2, verbose = true } = options;\n\n const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n // Preprocess the image\n const base64Processed = await preprocessCaptcha(imagePath);\n\n // Run attempts — retry refusals/failures to guarantee numAttempts valid results\n const attempts: string[] = [];\n const maxTotalCalls = numAttempts + 4; // allow up to 4 extra calls for refusals\n let callCount = 0;\n while (attempts.length < numAttempts && callCount < maxTotalCalls) {\n callCount++;\n const result = await singleAttempt(client, base64Processed, model, maxRetries);\n if (result) {\n attempts.push(result);\n if (verbose) console.log(` Attempt ${attempts.length}: ${result}`);\n } else {\n if (verbose) console.log(` Call ${callCount}: (refused/failed, retrying...)`);\n }\n }\n\n if (attempts.length === 0) {\n if (verbose) console.log(' All attempts failed!');\n return '';\n }\n\n // Majority vote\n const answer = majorityVote(attempts, expectedLength);\n return answer;\n}\n","import fs from 'fs';\nimport { execSync } from 'child_process';\nimport path from 'path';\n\n// Inline Python script for image preprocessing\n// Uses PIL which produces optimal results for captcha OCR\nconst PYTHON_SCRIPT = `\nimport sys, base64, io\nfrom PIL import Image, ImageFilter, ImageEnhance, ImageOps\n\nimage_path = sys.argv[1]\nimg = Image.open(image_path)\nimg = ImageOps.grayscale(img)\nimg = img.filter(ImageFilter.GaussianBlur(radius=1.2))\nimg = img.resize((img.width * 4, img.height * 4), Image.LANCZOS)\nimg = ImageEnhance.Contrast(img).enhance(3.0)\nimg = ImageEnhance.Sharpness(img).enhance(2.0)\nw, h = img.size\nimg = img.crop((int(w * 0.10), int(h * 0.02), int(w * 0.90), int(h * 0.60)))\npadded = Image.new('L', (img.width + 60, img.height + 40), 255)\npadded.paste(img, (30, 20))\npadded = padded.convert('RGB')\nbuf = io.BytesIO()\npadded.save(buf, format='PNG')\nsys.stdout.buffer.write(base64.b64encode(buf.getvalue()))\n`;\n\n/**\n * Preprocess a captcha image using PIL (via Python subprocess).\n *\n * Pipeline:\n * 1. Grayscale\n * 2. Gaussian blur (radius=1.2) to smooth dither pattern\n * 3. Upscale 4x with Lanczos\n * 4. Contrast 3x + Sharpness 2x (PIL enhancement — preserves soft gradients)\n * 5. Crop decorative borders\n * 6. Add white padding\n *\n * Returns a base64-encoded PNG string.\n */\nexport async function preprocessCaptcha(imagePath: string): Promise<string> {\n const absPath = path.resolve(imagePath);\n\n // Write the Python script to a temp file\n const scriptPath = '/tmp/_captcha_preprocess.py';\n fs.writeFileSync(scriptPath, PYTHON_SCRIPT);\n\n // Execute Python and capture base64 output\n const result = execSync(`python3 \"${scriptPath}\" \"${absPath}\"`, {\n maxBuffer: 10 * 1024 * 1024, // 10MB\n encoding: 'utf-8',\n });\n\n return result.trim();\n}\n\n/**\n * Read an image file and return its base64-encoded content.\n */\nexport function imageToBase64(imagePath: string): string {\n const buffer = fs.readFileSync(imagePath);\n return buffer.toString('base64');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACAnB,gBAAe;AACf,2BAAyB;AACzB,kBAAiB;AAIjB,IAAM,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCtB,eAAsB,kBAAkB,WAAoC;AAC1E,QAAM,UAAU,YAAAA,QAAK,QAAQ,SAAS;AAGtC,QAAM,aAAa;AACnB,YAAAC,QAAG,cAAc,YAAY,aAAa;AAG1C,QAAM,aAAS,+BAAS,YAAY,UAAU,MAAM,OAAO,KAAK;AAAA,IAC9D,WAAW,KAAK,OAAO;AAAA;AAA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BAAgB,aAAa,GAAG,UAAU,KAAK,IAAI;AAE1F,QAAM,SAAS,IAAI,cAAAC,QAAO,EAAE,QAAQ,QAAQ,IAAI,eAAe,CAAC;AAGhE,QAAM,kBAAkB,MAAM,kBAAkB,SAAS;AAGzD,QAAM,WAAqB,CAAC;AAC5B,QAAM,gBAAgB,cAAc;AACpC,MAAI,YAAY;AAChB,SAAO,SAAS,SAAS,eAAe,YAAY,eAAe;AACjE;AACA,UAAM,SAAS,MAAM,cAAc,QAAQ,iBAAiB,OAAO,UAAU;AAC7E,QAAI,QAAQ;AACV,eAAS,KAAK,MAAM;AACpB,UAAI,QAAS,SAAQ,IAAI,aAAa,SAAS,MAAM,KAAK,MAAM,EAAE;AAAA,IACpE,OAAO;AACL,UAAI,QAAS,SAAQ,IAAI,UAAU,SAAS,iCAAiC;AAAA,IAC/E;AAAA,EACF;AAEA,MAAI,SAAS,WAAW,GAAG;AACzB,QAAI,QAAS,SAAQ,IAAI,wBAAwB;AACjD,WAAO;AAAA,EACT;AAGA,QAAM,SAAS,aAAa,UAAU,cAAc;AACpD,SAAO;AACT;","names":["path","fs","OpenAI"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/solver.ts","../src/preprocess.ts"],"sourcesContent":["export { solveCaptchaImage } from './solver.js';\nexport { preprocessCaptcha, imageToBase64 } from './preprocess.js';\n","import OpenAI from 'openai';\nimport { preprocessCaptcha } from './preprocess.js';\n\nconst PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.\nThe text contains uppercase letters A-Z and/or digits 0-9.\nA thin vertical stroke is the digit 1. Never read it as the letter I or L.\nA round closed shape is the letter O, not the letter D.\nOutput ONLY the exact characters you read, nothing else.`;\n\ninterface SolverOptions {\n /** OpenAI model to use (default: \"o3\") */\n model?: string;\n /** Number of voting attempts (default: 5) */\n numAttempts?: number;\n /** Expected captcha length — results of other lengths are discarded (default: undefined = no filter) */\n expectedLength?: number;\n /** Max retries per attempt on API failure (default: 2) */\n maxRetries?: number;\n /** Whether to log attempt details (default: true) */\n verbose?: boolean;\n}\n\n/**\n * Make a single API call to read the captcha.\n * Retries up to `maxRetries` times on failure.\n */\nasync function singleAttempt(\n client: OpenAI,\n base64Image: string,\n model: string,\n maxRetries: number\n): Promise<string | null> {\n for (let retry = 0; retry <= maxRetries; retry++) {\n try {\n // Reasoning models (o3, o4-mini) use max_completion_tokens;\n // Standard models (gpt-4o, gpt-4.1, gpt-5.4-mini) use max_tokens.\n const isReasoningModel = model.startsWith('o');\n const tokenParam = isReasoningModel ? { max_completion_tokens: 2000 } : { max_tokens: 256 };\n\n const response = await client.chat.completions.create({\n model,\n messages: [\n {\n role: 'user',\n content: [\n { type: 'text', text: PROMPT },\n {\n type: 'image_url',\n image_url: {\n url: `data:image/png;base64,${base64Image}`,\n },\n },\n ],\n },\n ],\n temperature: 1,\n ...tokenParam,\n });\n\n const raw = response.choices[0]?.message?.content?.trim() ?? '';\n\n // Detect refusals\n const lower = raw.toLowerCase();\n if (\n lower.includes('sorry') ||\n lower.includes(\"can't help\") ||\n lower.includes('cannot help') ||\n lower.includes('unable to') ||\n lower.includes(\"i can't\") ||\n raw.length > 20\n ) {\n return null; // Model refused — don't count as an attempt\n }\n\n // Clean: keep only uppercase letters and digits\n const cleaned = raw.toUpperCase().replace(/[^A-Z0-9]/g, '');\n return cleaned || null;\n } catch (_err) {\n if (retry < maxRetries) {\n // Wait briefly before retry\n await new Promise((r) => setTimeout(r, 1000 * (retry + 1)));\n continue;\n }\n return null;\n }\n }\n return null;\n}\n\n/**\n * Confusion groups: characters the model commonly misreads as each other.\n * Each group maps to its canonical (most likely correct) character.\n */\nconst CONFUSION_GROUPS: Record<string, string> = {\n // Thin vertical strokes → digit 1\n '1': '1',\n I: '1',\n L: '1',\n // Round shapes → letter O\n O: 'O',\n D: 'O',\n '0': 'O',\n // Similar curves\n S: 'S',\n '5': 'S',\n // Straight edges\n Z: 'Z',\n '2': 'Z',\n};\n\n/**\n * Character-level majority vote across multiple attempts.\n *\n * Uses confusion-aware voting: characters that the model commonly\n * confuses (e.g. 1/I/L, O/D/0) are grouped together during counting.\n * The canonical character for the winning group is used.\n */\nfunction majorityVote(attempts: string[], expectedLength?: number): string {\n // Filter to expected length if specified\n let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;\n\n // If length filter removed everything, fall back to most common length\n if (filtered.length === 0) {\n filtered = attempts;\n }\n\n if (filtered.length === 0) return '';\n\n // Find most common length\n const lenCounts = new Map<number, number>();\n for (const a of filtered) {\n lenCounts.set(a.length, (lenCounts.get(a.length) ?? 0) + 1);\n }\n let bestLen = 0;\n let bestCount = 0;\n for (const [len, count] of lenCounts) {\n if (count > bestCount) {\n bestLen = len;\n bestCount = count;\n }\n }\n\n const sameLenAttempts = filtered.filter((a) => a.length === bestLen);\n if (sameLenAttempts.length === 0) return filtered[0];\n\n // Vote per character position with confusion-aware grouping\n const result: string[] = [];\n for (let pos = 0; pos < bestLen; pos++) {\n // Count raw characters\n const charCounts = new Map<string, number>();\n for (const a of sameLenAttempts) {\n const ch = a[pos];\n charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);\n }\n\n // Group by canonical form and sum counts\n const groupCounts = new Map<string, number>();\n for (const [ch, count] of charCounts) {\n const canonical = CONFUSION_GROUPS[ch] ?? ch;\n groupCounts.set(canonical, (groupCounts.get(canonical) ?? 0) + count);\n }\n\n // Pick the group with the highest combined count\n let bestGroup = '';\n let bestGroupCount = 0;\n for (const [canonical, count] of groupCounts) {\n if (count > bestGroupCount) {\n bestGroup = canonical;\n bestGroupCount = count;\n }\n }\n\n result.push(bestGroup);\n }\n\n return result.join('');\n}\n\n/**\n * Solve a captcha image using OpenAI vision + preprocessing + majority voting.\n */\nexport async function solveCaptchaImage(\n imagePath: string,\n options: SolverOptions = {}\n): Promise<string> {\n const { model = 'o3', numAttempts = 5, expectedLength, maxRetries = 2, verbose = true } = options;\n\n const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n // Preprocess the image\n const base64Processed = await preprocessCaptcha(imagePath);\n\n // Run attempts — retry refusals/failures to guarantee numAttempts valid results\n const attempts: string[] = [];\n const maxTotalCalls = numAttempts + 4; // allow up to 4 extra calls for refusals\n let callCount = 0;\n while (attempts.length < numAttempts && callCount < maxTotalCalls) {\n callCount++;\n const result = await singleAttempt(client, base64Processed, model, maxRetries);\n if (result) {\n attempts.push(result);\n if (verbose) console.log(` Attempt ${attempts.length}: ${result}`);\n } else {\n if (verbose) console.log(` Call ${callCount}: (refused/failed, retrying...)`);\n }\n }\n\n if (attempts.length === 0) {\n if (verbose) console.log(' All attempts failed!');\n return '';\n }\n\n // Majority vote\n const answer = majorityVote(attempts, expectedLength);\n return answer;\n}\n","import fs from 'fs';\nimport path from 'path';\nimport sharp from 'sharp';\n\n/**\n * Preprocess a captcha image using sharp (libvips).\n *\n * Pipeline:\n * 1. Gaussian blur in color space (smooths dither pattern)\n * 2. Grayscale conversion\n * 3. Upscale 4× with Lanczos\n * 4. Contrast boost (3× around image mean) + sharpen\n * 5. Crop decorative borders\n * 6. Add white padding\n *\n * Returns a base64-encoded PNG string.\n */\nexport async function preprocessCaptcha(imagePath: string): Promise<string> {\n const absPath = path.resolve(imagePath);\n\n // Read original dimensions for crop/resize calculations\n const metadata = await sharp(absPath).metadata();\n const origW = metadata.width!;\n const origH = metadata.height!;\n\n // Step 1-2: Blur in color space (smooths dither pattern) → greyscale\n // Separate from resize to prevent pipeline reordering\n const smoothed = await sharp(absPath).blur(1.5).greyscale().toBuffer();\n\n // Step 3: Upscale 4× with Lanczos\n const upscaled = await sharp(smoothed)\n .resize(origW * 4, origH * 4, { kernel: 'lanczos3' })\n .toBuffer();\n\n // Step 4: Contrast 3× around actual image mean + sharpen\n // Matches PIL's ImageEnhance.Contrast: output = factor*input + mean*(1-factor)\n const stats = await sharp(upscaled).stats();\n const mean = stats.channels[0].mean;\n const enhanced = await sharp(upscaled)\n .linear(3.0, mean * (1 - 3.0))\n .sharpen({ sigma: 1.0, m1: 2.0, m2: 1.0 })\n .toBuffer();\n\n // Step 5: Crop decorative borders\n // Remove 10% left/right, 2% top, 40% bottom (keep top 60%)\n // Math.floor matches Python's int() truncation\n const scaledW = origW * 4;\n const scaledH = origH * 4;\n const cropLeft = Math.floor(scaledW * 0.1);\n const cropTop = Math.floor(scaledH * 0.02);\n const cropRight = Math.floor(scaledW * 0.9);\n const cropBottom = Math.floor(scaledH * 0.6);\n const cropW = cropRight - cropLeft;\n const cropH = cropBottom - cropTop;\n\n // Step 5-6: Crop → add white padding → output PNG\n const result = await sharp(enhanced)\n .extract({ left: cropLeft, top: cropTop, width: cropW, height: cropH })\n .extend({\n top: 20,\n bottom: 20,\n left: 30,\n right: 30,\n background: { r: 255, g: 255, b: 255 },\n })\n .png()\n .toBuffer();\n\n return result.toString('base64');\n}\n\n/**\n * Read an image file and return its base64-encoded content.\n */\nexport function imageToBase64(imagePath: string): string {\n const buffer = fs.readFileSync(imagePath);\n return buffer.toString('base64');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACAnB,gBAAe;AACf,kBAAiB;AACjB,mBAAkB;AAelB,eAAsB,kBAAkB,WAAoC;AAC1E,QAAM,UAAU,YAAAA,QAAK,QAAQ,SAAS;AAGtC,QAAM,WAAW,UAAM,aAAAC,SAAM,OAAO,EAAE,SAAS;AAC/C,QAAM,QAAQ,SAAS;AACvB,QAAM,QAAQ,SAAS;AAIvB,QAAM,WAAW,UAAM,aAAAA,SAAM,OAAO,EAAE,KAAK,GAAG,EAAE,UAAU,EAAE,SAAS;AAGrE,QAAM,WAAW,UAAM,aAAAA,SAAM,QAAQ,EAClC,OAAO,QAAQ,GAAG,QAAQ,GAAG,EAAE,QAAQ,WAAW,CAAC,EACnD,SAAS;AAIZ,QAAM,QAAQ,UAAM,aAAAA,SAAM,QAAQ,EAAE,MAAM;AAC1C,QAAM,OAAO,MAAM,SAAS,CAAC,EAAE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I,eAAe,CAAC;AAGhE,QAAM,kBAAkB,MAAM,kBAAkB,SAAS;AAGzD,QAAM,WAAqB,CAAC;AAC5B,QAAM,gBAAgB,cAAc;AACpC,MAAI,YAAY;AAChB,SAAO,SAAS,SAAS,eAAe,YAAY,eAAe;AACjE;AACA,UAAM,SAAS,MAAM,cAAc,QAAQ,iBAAiB,OAAO,UAAU;AAC7E,QAAI,QAAQ;AACV,eAAS,KAAK,MAAM;AACpB,UAAI,QAAS,SAAQ,IAAI,aAAa,SAAS,MAAM,KAAK,MAAM,EAAE;AAAA,IACpE,OAAO;AACL,UAAI,QAAS,SAAQ,IAAI,UAAU,SAAS,iCAAiC;AAAA,IAC/E;AAAA,EACF;AAEA,MAAI,SAAS,WAAW,GAAG;AACzB,QAAI,QAAS,SAAQ,IAAI,wBAAwB;AACjD,WAAO;AAAA,EACT;AAGA,QAAM,SAAS,aAAa,UAAU,cAAc;AACpD,SAAO;AACT;","names":["path","sharp","fs","OpenAI"]}
package/dist/index.d.cts CHANGED
@@ -16,13 +16,13 @@ interface SolverOptions {
16
16
  declare function solveCaptchaImage(imagePath: string, options?: SolverOptions): Promise<string>;
17
17
 
18
18
  /**
19
- * Preprocess a captcha image using PIL (via Python subprocess).
19
+ * Preprocess a captcha image using sharp (libvips).
20
20
  *
21
21
  * Pipeline:
22
- * 1. Grayscale
23
- * 2. Gaussian blur (radius=1.2) to smooth dither pattern
24
- * 3. Upscale 4x with Lanczos
25
- * 4. Contrast 3x + Sharpness 2x (PIL enhancement preserves soft gradients)
22
+ * 1. Gaussian blur in color space (smooths dither pattern)
23
+ * 2. Grayscale conversion
24
+ * 3. Upscale with Lanczos
25
+ * 4. Contrast boost ( around image mean) + sharpen
26
26
  * 5. Crop decorative borders
27
27
  * 6. Add white padding
28
28
  *
package/dist/index.d.ts CHANGED
@@ -16,13 +16,13 @@ interface SolverOptions {
16
16
  declare function solveCaptchaImage(imagePath: string, options?: SolverOptions): Promise<string>;
17
17
 
18
18
  /**
19
- * Preprocess a captcha image using PIL (via Python subprocess).
19
+ * Preprocess a captcha image using sharp (libvips).
20
20
  *
21
21
  * Pipeline:
22
- * 1. Grayscale
23
- * 2. Gaussian blur (radius=1.2) to smooth dither pattern
24
- * 3. Upscale 4x with Lanczos
25
- * 4. Contrast 3x + Sharpness 2x (PIL enhancement preserves soft gradients)
22
+ * 1. Gaussian blur in color space (smooths dither pattern)
23
+ * 2. Grayscale conversion
24
+ * 3. Upscale with Lanczos
25
+ * 4. Contrast boost ( around image mean) + sharpen
26
26
  * 5. Crop decorative borders
27
27
  * 6. Add white padding
28
28
  *
package/dist/index.js CHANGED
@@ -3,38 +3,34 @@ import OpenAI from "openai";
3
3
 
4
4
  // src/preprocess.ts
5
5
  import fs from "fs";
6
- import { execSync } from "child_process";
7
6
  import path from "path";
8
- var PYTHON_SCRIPT = `
9
- import sys, base64, io
10
- from PIL import Image, ImageFilter, ImageEnhance, ImageOps
11
-
12
- image_path = sys.argv[1]
13
- img = Image.open(image_path)
14
- img = ImageOps.grayscale(img)
15
- img = img.filter(ImageFilter.GaussianBlur(radius=1.2))
16
- img = img.resize((img.width * 4, img.height * 4), Image.LANCZOS)
17
- img = ImageEnhance.Contrast(img).enhance(3.0)
18
- img = ImageEnhance.Sharpness(img).enhance(2.0)
19
- w, h = img.size
20
- img = img.crop((int(w * 0.10), int(h * 0.02), int(w * 0.90), int(h * 0.60)))
21
- padded = Image.new('L', (img.width + 60, img.height + 40), 255)
22
- padded.paste(img, (30, 20))
23
- padded = padded.convert('RGB')
24
- buf = io.BytesIO()
25
- padded.save(buf, format='PNG')
26
- sys.stdout.buffer.write(base64.b64encode(buf.getvalue()))
27
- `;
7
+ import sharp from "sharp";
28
8
  async function preprocessCaptcha(imagePath) {
29
9
  const absPath = path.resolve(imagePath);
30
- const scriptPath = "/tmp/_captcha_preprocess.py";
31
- fs.writeFileSync(scriptPath, PYTHON_SCRIPT);
32
- const result = execSync(`python3 "${scriptPath}" "${absPath}"`, {
33
- maxBuffer: 10 * 1024 * 1024,
34
- // 10MB
35
- encoding: "utf-8"
36
- });
37
- return result.trim();
10
+ const metadata = await sharp(absPath).metadata();
11
+ const origW = metadata.width;
12
+ const origH = metadata.height;
13
+ const smoothed = await sharp(absPath).blur(1.5).greyscale().toBuffer();
14
+ const upscaled = await sharp(smoothed).resize(origW * 4, origH * 4, { kernel: "lanczos3" }).toBuffer();
15
+ const stats = await sharp(upscaled).stats();
16
+ const mean = stats.channels[0].mean;
17
+ const enhanced = await sharp(upscaled).linear(3, mean * (1 - 3)).sharpen({ sigma: 1, m1: 2, m2: 1 }).toBuffer();
18
+ const scaledW = origW * 4;
19
+ const scaledH = origH * 4;
20
+ const cropLeft = Math.floor(scaledW * 0.1);
21
+ const cropTop = Math.floor(scaledH * 0.02);
22
+ const cropRight = Math.floor(scaledW * 0.9);
23
+ const cropBottom = Math.floor(scaledH * 0.6);
24
+ const cropW = cropRight - cropLeft;
25
+ const cropH = cropBottom - cropTop;
26
+ const result = await sharp(enhanced).extract({ left: cropLeft, top: cropTop, width: cropW, height: cropH }).extend({
27
+ top: 20,
28
+ bottom: 20,
29
+ left: 30,
30
+ right: 30,
31
+ background: { r: 255, g: 255, b: 255 }
32
+ }).png().toBuffer();
33
+ return result.toString("base64");
38
34
  }
39
35
  function imageToBase64(imagePath) {
40
36
  const buffer = fs.readFileSync(imagePath);
@@ -44,7 +40,7 @@ function imageToBase64(imagePath) {
44
40
  // src/solver.ts
45
41
  var PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.
46
42
  The text contains uppercase letters A-Z and/or digits 0-9.
47
- A thin vertical stroke is likely the digit 1, not the letter I.
43
+ A thin vertical stroke is the digit 1. Never read it as the letter I or L.
48
44
  A round closed shape is the letter O, not the letter D.
49
45
  Output ONLY the exact characters you read, nothing else.`;
50
46
  async function singleAttempt(client, base64Image, model, maxRetries) {
@@ -88,6 +84,22 @@ async function singleAttempt(client, base64Image, model, maxRetries) {
88
84
  }
89
85
  return null;
90
86
  }
87
+ var CONFUSION_GROUPS = {
88
+ // Thin vertical strokes → digit 1
89
+ "1": "1",
90
+ I: "1",
91
+ L: "1",
92
+ // Round shapes → letter O
93
+ O: "O",
94
+ D: "O",
95
+ "0": "O",
96
+ // Similar curves
97
+ S: "S",
98
+ "5": "S",
99
+ // Straight edges
100
+ Z: "Z",
101
+ "2": "Z"
102
+ };
91
103
  function majorityVote(attempts, expectedLength) {
92
104
  let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;
93
105
  if (filtered.length === 0) {
@@ -115,15 +127,20 @@ function majorityVote(attempts, expectedLength) {
115
127
  const ch = a[pos];
116
128
  charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);
117
129
  }
118
- let bestChar = "";
119
- let bestCharCount = 0;
130
+ const groupCounts = /* @__PURE__ */ new Map();
120
131
  for (const [ch, count] of charCounts) {
121
- if (count > bestCharCount) {
122
- bestChar = ch;
123
- bestCharCount = count;
132
+ const canonical = CONFUSION_GROUPS[ch] ?? ch;
133
+ groupCounts.set(canonical, (groupCounts.get(canonical) ?? 0) + count);
134
+ }
135
+ let bestGroup = "";
136
+ let bestGroupCount = 0;
137
+ for (const [canonical, count] of groupCounts) {
138
+ if (count > bestGroupCount) {
139
+ bestGroup = canonical;
140
+ bestGroupCount = count;
124
141
  }
125
142
  }
126
- result.push(bestChar);
143
+ result.push(bestGroup);
127
144
  }
128
145
  return result.join("");
129
146
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/solver.ts","../src/preprocess.ts"],"sourcesContent":["import OpenAI from 'openai';\nimport { preprocessCaptcha } from './preprocess.js';\n\nconst PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.\nThe text contains uppercase letters A-Z and/or digits 0-9.\nA thin vertical stroke is likely the digit 1, not the letter I.\nA round closed shape is the letter O, not the letter D.\nOutput ONLY the exact characters you read, nothing else.`;\n\ninterface SolverOptions {\n /** OpenAI model to use (default: \"o3\") */\n model?: string;\n /** Number of voting attempts (default: 5) */\n numAttempts?: number;\n /** Expected captcha length — results of other lengths are discarded (default: undefined = no filter) */\n expectedLength?: number;\n /** Max retries per attempt on API failure (default: 2) */\n maxRetries?: number;\n /** Whether to log attempt details (default: true) */\n verbose?: boolean;\n}\n\n/**\n * Make a single API call to read the captcha.\n * Retries up to `maxRetries` times on failure.\n */\nasync function singleAttempt(\n client: OpenAI,\n base64Image: string,\n model: string,\n maxRetries: number\n): Promise<string | null> {\n for (let retry = 0; retry <= maxRetries; retry++) {\n try {\n // Reasoning models (o3, o4-mini) use max_completion_tokens;\n // Standard models (gpt-4o, gpt-4.1, gpt-5.4-mini) use max_tokens.\n const isReasoningModel = model.startsWith('o');\n const tokenParam = isReasoningModel ? { max_completion_tokens: 2000 } : { max_tokens: 256 };\n\n const response = await client.chat.completions.create({\n model,\n messages: [\n {\n role: 'user',\n content: [\n { type: 'text', text: PROMPT },\n {\n type: 'image_url',\n image_url: {\n url: `data:image/png;base64,${base64Image}`,\n },\n },\n ],\n },\n ],\n temperature: 1,\n ...tokenParam,\n });\n\n const raw = response.choices[0]?.message?.content?.trim() ?? '';\n\n // Detect refusals\n const lower = raw.toLowerCase();\n if (\n lower.includes('sorry') ||\n lower.includes(\"can't help\") ||\n lower.includes('cannot help') ||\n lower.includes('unable to') ||\n lower.includes(\"i can't\") ||\n raw.length > 20\n ) {\n return null; // Model refused — don't count as an attempt\n }\n\n // Clean: keep only uppercase letters and digits\n const cleaned = raw.toUpperCase().replace(/[^A-Z0-9]/g, '');\n return cleaned || null;\n } catch (_err) {\n if (retry < maxRetries) {\n // Wait briefly before retry\n await new Promise((r) => setTimeout(r, 1000 * (retry + 1)));\n continue;\n }\n return null;\n }\n }\n return null;\n}\n\n/**\n * Character-level majority vote across multiple attempts.\n */\nfunction majorityVote(attempts: string[], expectedLength?: number): string {\n // Filter to expected length if specified\n let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;\n\n // If length filter removed everything, fall back to most common length\n if (filtered.length === 0) {\n filtered = attempts;\n }\n\n if (filtered.length === 0) return '';\n\n // Find most common length\n const lenCounts = new Map<number, number>();\n for (const a of filtered) {\n lenCounts.set(a.length, (lenCounts.get(a.length) ?? 0) + 1);\n }\n let bestLen = 0;\n let bestCount = 0;\n for (const [len, count] of lenCounts) {\n if (count > bestCount) {\n bestLen = len;\n bestCount = count;\n }\n }\n\n const sameLenAttempts = filtered.filter((a) => a.length === bestLen);\n if (sameLenAttempts.length === 0) return filtered[0];\n\n // Vote per character position\n const result: string[] = [];\n for (let pos = 0; pos < bestLen; pos++) {\n const charCounts = new Map<string, number>();\n for (const a of sameLenAttempts) {\n const ch = a[pos];\n charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);\n }\n let bestChar = '';\n let bestCharCount = 0;\n for (const [ch, count] of charCounts) {\n if (count > bestCharCount) {\n bestChar = ch;\n bestCharCount = count;\n }\n }\n result.push(bestChar);\n }\n\n return result.join('');\n}\n\n/**\n * Solve a captcha image using OpenAI vision + preprocessing + majority voting.\n */\nexport async function solveCaptchaImage(\n imagePath: string,\n options: SolverOptions = {}\n): Promise<string> {\n const { model = 'o3', numAttempts = 5, expectedLength, maxRetries = 2, verbose = true } = options;\n\n const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n // Preprocess the image\n const base64Processed = await preprocessCaptcha(imagePath);\n\n // Run attempts — retry refusals/failures to guarantee numAttempts valid results\n const attempts: string[] = [];\n const maxTotalCalls = numAttempts + 4; // allow up to 4 extra calls for refusals\n let callCount = 0;\n while (attempts.length < numAttempts && callCount < maxTotalCalls) {\n callCount++;\n const result = await singleAttempt(client, base64Processed, model, maxRetries);\n if (result) {\n attempts.push(result);\n if (verbose) console.log(` Attempt ${attempts.length}: ${result}`);\n } else {\n if (verbose) console.log(` Call ${callCount}: (refused/failed, retrying...)`);\n }\n }\n\n if (attempts.length === 0) {\n if (verbose) console.log(' All attempts failed!');\n return '';\n }\n\n // Majority vote\n const answer = majorityVote(attempts, expectedLength);\n return answer;\n}\n","import fs from 'fs';\nimport { execSync } from 'child_process';\nimport path from 'path';\n\n// Inline Python script for image preprocessing\n// Uses PIL which produces optimal results for captcha OCR\nconst PYTHON_SCRIPT = `\nimport sys, base64, io\nfrom PIL import Image, ImageFilter, ImageEnhance, ImageOps\n\nimage_path = sys.argv[1]\nimg = Image.open(image_path)\nimg = ImageOps.grayscale(img)\nimg = img.filter(ImageFilter.GaussianBlur(radius=1.2))\nimg = img.resize((img.width * 4, img.height * 4), Image.LANCZOS)\nimg = ImageEnhance.Contrast(img).enhance(3.0)\nimg = ImageEnhance.Sharpness(img).enhance(2.0)\nw, h = img.size\nimg = img.crop((int(w * 0.10), int(h * 0.02), int(w * 0.90), int(h * 0.60)))\npadded = Image.new('L', (img.width + 60, img.height + 40), 255)\npadded.paste(img, (30, 20))\npadded = padded.convert('RGB')\nbuf = io.BytesIO()\npadded.save(buf, format='PNG')\nsys.stdout.buffer.write(base64.b64encode(buf.getvalue()))\n`;\n\n/**\n * Preprocess a captcha image using PIL (via Python subprocess).\n *\n * Pipeline:\n * 1. Grayscale\n * 2. Gaussian blur (radius=1.2) to smooth dither pattern\n * 3. Upscale 4x with Lanczos\n * 4. Contrast 3x + Sharpness 2x (PIL enhancement — preserves soft gradients)\n * 5. Crop decorative borders\n * 6. Add white padding\n *\n * Returns a base64-encoded PNG string.\n */\nexport async function preprocessCaptcha(imagePath: string): Promise<string> {\n const absPath = path.resolve(imagePath);\n\n // Write the Python script to a temp file\n const scriptPath = '/tmp/_captcha_preprocess.py';\n fs.writeFileSync(scriptPath, PYTHON_SCRIPT);\n\n // Execute Python and capture base64 output\n const result = execSync(`python3 \"${scriptPath}\" \"${absPath}\"`, {\n maxBuffer: 10 * 1024 * 1024, // 10MB\n encoding: 'utf-8',\n });\n\n return result.trim();\n}\n\n/**\n * Read an image file and return its base64-encoded content.\n */\nexport function imageToBase64(imagePath: string): string {\n const buffer = fs.readFileSync(imagePath);\n return buffer.toString('base64');\n}\n"],"mappings":";AAAA,OAAO,Y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AAM,KAAK,EAAE,GAAG;AAChB,iBAAW,IAAI,KAAK,WAAW,IAAI,EAAE,KAAK,KAAK,CAAC;AAAA,IAClD;AACA,QAAI,WAAW;AACf,QAAI,gBAAgB;AACpB,eAAW,CAAC,IAAI,KAAK,KAAK,YAAY;AACpC,UAAI,QAAQ,eAAe;AACzB,mBAAW;AACX,wBAAgB;AAAA,MAClB;AAAA,IACF;AACA,WAAO,KAAK,QAAQ;AAAA,EACtB;AAEA,SAAO,OAAO,KAAK,EAAE;AACvB;AAKA,eAAsB,kBACpB,WACA,UAAyB,CAAC,GACT;AACjB,QAAM,EAAE,QAAQ,MAAM,cAAc,GAAG,gBAAgB,aAAa,GAAG,UAAU,KAAK,IAAI;AAE1F,QAAM,SAAS,IAAI,OAAO,EAAE,QAAQ,QAAQ,IAAI,eAAe,CAAC;AAGhE,QAAM,kBAAkB,MAAM,kBAAkB,SAAS;AAGzD,QAAM,WAAqB,CAAC;AAC5B,QAAM,gBAAgB,cAAc;AACpC,MAAI,YAAY;AAChB,SAAO,SAAS,SAAS,eAAe,YAAY,eAAe;AACjE;AACA,UAAM,SAAS,MAAM,cAAc,QAAQ,iBAAiB,OAAO,UAAU;AAC7E,QAAI,QAAQ;AACV,eAAS,KAAK,MAAM;AACpB,UAAI,QAAS,SAAQ,IAAI,aAAa,SAAS,MAAM,KAAK,MAAM,EAAE;AAAA,IACpE,OAAO;AACL,UAAI,QAAS,SAAQ,IAAI,UAAU,SAAS,iCAAiC;AAAA,IAC/E;AAAA,EACF;AAEA,MAAI,SAAS,WAAW,GAAG;AACzB,QAAI,QAAS,SAAQ,IAAI,wBAAwB;AACjD,WAAO;AAAA,EACT;AAGA,QAAM,SAAS,aAAa,UAAU,cAAc;AACpD,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../src/solver.ts","../src/preprocess.ts"],"sourcesContent":["import OpenAI from 'openai';\nimport { preprocessCaptcha } from './preprocess.js';\n\nconst PROMPT = `You are an assistant helping a visually impaired person read distorted text from an image.\nThe text contains uppercase letters A-Z and/or digits 0-9.\nA thin vertical stroke is the digit 1. Never read it as the letter I or L.\nA round closed shape is the letter O, not the letter D.\nOutput ONLY the exact characters you read, nothing else.`;\n\ninterface SolverOptions {\n /** OpenAI model to use (default: \"o3\") */\n model?: string;\n /** Number of voting attempts (default: 5) */\n numAttempts?: number;\n /** Expected captcha length — results of other lengths are discarded (default: undefined = no filter) */\n expectedLength?: number;\n /** Max retries per attempt on API failure (default: 2) */\n maxRetries?: number;\n /** Whether to log attempt details (default: true) */\n verbose?: boolean;\n}\n\n/**\n * Make a single API call to read the captcha.\n * Retries up to `maxRetries` times on failure.\n */\nasync function singleAttempt(\n client: OpenAI,\n base64Image: string,\n model: string,\n maxRetries: number\n): Promise<string | null> {\n for (let retry = 0; retry <= maxRetries; retry++) {\n try {\n // Reasoning models (o3, o4-mini) use max_completion_tokens;\n // Standard models (gpt-4o, gpt-4.1, gpt-5.4-mini) use max_tokens.\n const isReasoningModel = model.startsWith('o');\n const tokenParam = isReasoningModel ? { max_completion_tokens: 2000 } : { max_tokens: 256 };\n\n const response = await client.chat.completions.create({\n model,\n messages: [\n {\n role: 'user',\n content: [\n { type: 'text', text: PROMPT },\n {\n type: 'image_url',\n image_url: {\n url: `data:image/png;base64,${base64Image}`,\n },\n },\n ],\n },\n ],\n temperature: 1,\n ...tokenParam,\n });\n\n const raw = response.choices[0]?.message?.content?.trim() ?? '';\n\n // Detect refusals\n const lower = raw.toLowerCase();\n if (\n lower.includes('sorry') ||\n lower.includes(\"can't help\") ||\n lower.includes('cannot help') ||\n lower.includes('unable to') ||\n lower.includes(\"i can't\") ||\n raw.length > 20\n ) {\n return null; // Model refused — don't count as an attempt\n }\n\n // Clean: keep only uppercase letters and digits\n const cleaned = raw.toUpperCase().replace(/[^A-Z0-9]/g, '');\n return cleaned || null;\n } catch (_err) {\n if (retry < maxRetries) {\n // Wait briefly before retry\n await new Promise((r) => setTimeout(r, 1000 * (retry + 1)));\n continue;\n }\n return null;\n }\n }\n return null;\n}\n\n/**\n * Confusion groups: characters the model commonly misreads as each other.\n * Each group maps to its canonical (most likely correct) character.\n */\nconst CONFUSION_GROUPS: Record<string, string> = {\n // Thin vertical strokes → digit 1\n '1': '1',\n I: '1',\n L: '1',\n // Round shapes → letter O\n O: 'O',\n D: 'O',\n '0': 'O',\n // Similar curves\n S: 'S',\n '5': 'S',\n // Straight edges\n Z: 'Z',\n '2': 'Z',\n};\n\n/**\n * Character-level majority vote across multiple attempts.\n *\n * Uses confusion-aware voting: characters that the model commonly\n * confuses (e.g. 1/I/L, O/D/0) are grouped together during counting.\n * The canonical character for the winning group is used.\n */\nfunction majorityVote(attempts: string[], expectedLength?: number): string {\n // Filter to expected length if specified\n let filtered = expectedLength ? attempts.filter((a) => a.length === expectedLength) : attempts;\n\n // If length filter removed everything, fall back to most common length\n if (filtered.length === 0) {\n filtered = attempts;\n }\n\n if (filtered.length === 0) return '';\n\n // Find most common length\n const lenCounts = new Map<number, number>();\n for (const a of filtered) {\n lenCounts.set(a.length, (lenCounts.get(a.length) ?? 0) + 1);\n }\n let bestLen = 0;\n let bestCount = 0;\n for (const [len, count] of lenCounts) {\n if (count > bestCount) {\n bestLen = len;\n bestCount = count;\n }\n }\n\n const sameLenAttempts = filtered.filter((a) => a.length === bestLen);\n if (sameLenAttempts.length === 0) return filtered[0];\n\n // Vote per character position with confusion-aware grouping\n const result: string[] = [];\n for (let pos = 0; pos < bestLen; pos++) {\n // Count raw characters\n const charCounts = new Map<string, number>();\n for (const a of sameLenAttempts) {\n const ch = a[pos];\n charCounts.set(ch, (charCounts.get(ch) ?? 0) + 1);\n }\n\n // Group by canonical form and sum counts\n const groupCounts = new Map<string, number>();\n for (const [ch, count] of charCounts) {\n const canonical = CONFUSION_GROUPS[ch] ?? ch;\n groupCounts.set(canonical, (groupCounts.get(canonical) ?? 0) + count);\n }\n\n // Pick the group with the highest combined count\n let bestGroup = '';\n let bestGroupCount = 0;\n for (const [canonical, count] of groupCounts) {\n if (count > bestGroupCount) {\n bestGroup = canonical;\n bestGroupCount = count;\n }\n }\n\n result.push(bestGroup);\n }\n\n return result.join('');\n}\n\n/**\n * Solve a captcha image using OpenAI vision + preprocessing + majority voting.\n */\nexport async function solveCaptchaImage(\n imagePath: string,\n options: SolverOptions = {}\n): Promise<string> {\n const { model = 'o3', numAttempts = 5, expectedLength, maxRetries = 2, verbose = true } = options;\n\n const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n\n // Preprocess the image\n const base64Processed = await preprocessCaptcha(imagePath);\n\n // Run attempts — retry refusals/failures to guarantee numAttempts valid results\n const attempts: string[] = [];\n const maxTotalCalls = numAttempts + 4; // allow up to 4 extra calls for refusals\n let callCount = 0;\n while (attempts.length < numAttempts && callCount < maxTotalCalls) {\n callCount++;\n const result = await singleAttempt(client, base64Processed, model, maxRetries);\n if (result) {\n attempts.push(result);\n if (verbose) console.log(` Attempt ${attempts.length}: ${result}`);\n } else {\n if (verbose) console.log(` Call ${callCount}: (refused/failed, retrying...)`);\n }\n }\n\n if (attempts.length === 0) {\n if (verbose) console.log(' All attempts failed!');\n return '';\n }\n\n // Majority vote\n const answer = majorityVote(attempts, expectedLength);\n return answer;\n}\n","import fs from 'fs';\nimport path from 'path';\nimport sharp from 'sharp';\n\n/**\n * Preprocess a captcha image using sharp (libvips).\n *\n * Pipeline:\n * 1. Gaussian blur in color space (smooths dither pattern)\n * 2. Grayscale conversion\n * 3. Upscale 4× with Lanczos\n * 4. Contrast boost (3× around image mean) + sharpen\n * 5. Crop decorative borders\n * 6. Add white padding\n *\n * Returns a base64-encoded PNG string.\n */\nexport async function preprocessCaptcha(imagePath: string): Promise<string> {\n const absPath = path.resolve(imagePath);\n\n // Read original dimensions for crop/resize calculations\n const metadata = await sharp(absPath).metadata();\n const origW = metadata.width!;\n const origH = metadata.height!;\n\n // Step 1-2: Blur in color space (smooths dither pattern) → greyscale\n // Separate from resize to prevent pipeline reordering\n const smoothed = await sharp(absPath).blur(1.5).greyscale().toBuffer();\n\n // Step 3: Upscale 4× with Lanczos\n const upscaled = await sharp(smoothed)\n .resize(origW * 4, origH * 4, { kernel: 'lanczos3' })\n .toBuffer();\n\n // Step 4: Contrast 3× around actual image mean + sharpen\n // Matches PIL's ImageEnhance.Contrast: output = factor*input + mean*(1-factor)\n const stats = await sharp(upscaled).stats();\n const mean = stats.channels[0].mean;\n const enhanced = await sharp(upscaled)\n .linear(3.0, mean * (1 - 3.0))\n .sharpen({ sigma: 1.0, m1: 2.0, m2: 1.0 })\n .toBuffer();\n\n // Step 5: Crop decorative borders\n // Remove 10% left/right, 2% top, 40% bottom (keep top 60%)\n // Math.floor matches Python's int() truncation\n const scaledW = origW * 4;\n const scaledH = origH * 4;\n const cropLeft = Math.floor(scaledW * 0.1);\n const cropTop = Math.floor(scaledH * 0.02);\n const cropRight = Math.floor(scaledW * 0.9);\n const cropBottom = Math.floor(scaledH * 0.6);\n const cropW = cropRight - cropLeft;\n const cropH = cropBottom - cropTop;\n\n // Step 5-6: Crop → add white padding → output PNG\n const result = await sharp(enhanced)\n .extract({ left: cropLeft, top: cropTop, width: cropW, height: cropH })\n .extend({\n top: 20,\n bottom: 20,\n left: 30,\n right: 30,\n background: { r: 255, g: 255, b: 255 },\n })\n .png()\n .toBuffer();\n\n return result.toString('base64');\n}\n\n/**\n * Read an image file and return its base64-encoded content.\n */\nexport function imageToBase64(imagePath: string): string {\n const buffer = fs.readFileSync(imagePath);\n return buffer.toString('base64');\n}\n"],"mappings":";AAAA,OAAO,YAAY;;;ACAnB,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,WAAW;AAelB,eAAsB,kBAAkB,WAAoC;AAC1E,QAAM,UAAU,KAAK,QAAQ,SAAS;AAGtC,QAAM,WAAW,MAAM,MAAM,OAAO,EAAE,SAAS;AAC/C,QAAM,QAAQ,SAAS;AACvB,QAAM,QAAQ,SAAS;AAIvB,QAAM,WAAW,MAAM,MAAM,OAAO,EAAE,KAAK,GAAG,EAAE,UAAU,EAAE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AACvB;AAKA,eAAsB,kBACpB,WACA,UAAyB,CAAC,GACT;AACjB,QAAM,EAAE,QAAQ,MAAM,cAAc,GAAG,gBAAgB,aAAa,GAAG,UAAU,KAAK,IAAI;AAE1F,QAAM,SAAS,IAAI,OAAO,EAAE,QAAQ,QAAQ,IAAI,eAAe,CAAC;AAGhE,QAAM,kBAAkB,MAAM,kBAAkB,SAAS;AAGzD,QAAM,WAAqB,CAAC;AAC5B,QAAM,gBAAgB,cAAc;AACpC,MAAI,YAAY;AAChB,SAAO,SAAS,SAAS,eAAe,YAAY,eAAe;AACjE;AACA,UAAM,SAAS,MAAM,cAAc,QAAQ,iBAAiB,OAAO,UAAU;AAC7E,QAAI,QAAQ;AACV,eAAS,KAAK,MAAM;AACpB,UAAI,QAAS,SAAQ,IAAI,aAAa,SAAS,MAAM,KAAK,MAAM,EAAE;AAAA,IACpE,OAAO;AACL,UAAI,QAAS,SAAQ,IAAI,UAAU,SAAS,iCAAiC;AAAA,IAC/E;AAAA,EACF;AAEA,MAAI,SAAS,WAAW,GAAG;AACzB,QAAI,QAAS,SAAQ,IAAI,wBAAwB;AACjD,WAAO;AAAA,EACT;AAGA,QAAM,SAAS,aAAa,UAAU,cAAc;AACpD,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yigitahmetsahin/captcha-solver",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "AI-powered captcha solver using image preprocessing and OpenAI vision models",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",