captchaforge 0.2.39

Captcha detection and solving for Firefox and BiDi-driven browsers. Detection, vendor solver scaffolding, trusted cross-origin click delivery into nested OOPIFs, and stealth personas are implemented and tested; broad live-vendor solve rates are not yet benchmarked.
Documentation
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! YOLOv8 object detection for CAPTCHA challenge screenshots.
//!
//! Runs ONNX inference via `ort` with CUDA/TensorRT acceleration
//! and automatic CPU fallback. Targets reCAPTCHA / hCaptcha image
//! grids where the challenge asks the user to select tiles containing
//! a specific object class.

use anyhow::{Context, Result};
use image::{imageops, DynamicImage, GenericImageView};
use std::path::Path;
use tracing::{debug, info};

pub const YOLO_INPUT_SIZE: u32 = 640;
const CONF_THRESHOLD: f32 = 0.25;
const NMS_THRESHOLD: f32 = 0.45;

/// A single object detection result.
#[derive(Debug, Clone, PartialEq)]
pub struct Detection {
    /// Normalized bounding box [x1, y1, x2, y2] in 0..1 range.
    pub bbox: [f32; 4],
    /// COCO class name (e.g. "traffic light", "bus", "bicycle").
    pub class: String,
    /// Detection confidence 0.0–1.0.
    pub confidence: f32,
}

/// YOLOv8 detector backed by ONNX Runtime.
pub struct YoloDetector {
    session: ort::session::Session,
}

impl YoloDetector {
    /// Load a YOLOv8 ONNX model from disk.
    pub fn load(model_path: &Path) -> Result<Self> {
        let session = ort::session::Session::builder()
            .map_err(|e| anyhow::anyhow!("create ONNX session builder: {e}"))?
            .with_optimization_level(ort::session::builder::GraphOptimizationLevel::Level3)
            .map_err(|e| anyhow::anyhow!("set graph optimization: {e}"))?
            .with_execution_providers([
                ort::execution_providers::CUDAExecutionProvider::default().build(),
                ort::execution_providers::TensorRTExecutionProvider::default().build(),
                ort::execution_providers::CPUExecutionProvider::default().build(),
            ])
            .map_err(|e| anyhow::anyhow!("set execution providers: {e}"))?
            .commit_from_file(model_path)
            .map_err(|e| anyhow::anyhow!("load ONNX model from {}: {e}", model_path.display()))?;

        info!(model = %model_path.display(), "YOLOv8 detector loaded");
        Ok(Self { session })
    }

    /// Run detection on an image, returning all detections above the
    /// confidence threshold after NMS.
    pub fn detect(&mut self, image: &DynamicImage) -> Result<Vec<Detection>> {
        let (orig_w, orig_h) = image.dimensions();

        // Resize to YOLO input size with padding (letterbox).
        let (resized, pad_x, pad_y, scale) = letterbox(image, YOLO_INPUT_SIZE);
        let rgb = resized.to_rgb8();

        // NCHW float tensor, normalized to [0, 1].
        let (w, h) = (YOLO_INPUT_SIZE as usize, YOLO_INPUT_SIZE as usize);
        let mut pixel_data = vec![0.0f32; 3 * h * w];
        for y in 0..h {
            for x in 0..w {
                let pixel = rgb.get_pixel(x as u32, y as u32);
                pixel_data[y * w + x] = pixel[0] as f32 / 255.0;
                pixel_data[h * w + y * w + x] = pixel[1] as f32 / 255.0;
                pixel_data[2 * h * w + y * w + x] = pixel[2] as f32 / 255.0;
            }
        }

        let shape = vec![1_i64, 3, h as i64, w as i64];
        let input =
            ort::value::Tensor::from_array((shape, pixel_data)).context("build input tensor")?;

        let outputs = self
            .session
            .run(ort::inputs! { "images" => input })
            .context("run YOLO inference")?;

        let output_view = outputs[0]
            .try_extract_array::<f32>()
            .context("extract output tensor")?;

        let output_flat: Vec<f32> = output_view.iter().copied().collect();
        let shape = output_view.shape().to_vec();

        let raw =
            Self::parse_raw_detections(&output_flat, &shape, pad_x, pad_y, scale, orig_w, orig_h)?;
        debug!(raw = raw.len(), "YOLO raw detections");

        let filtered = nms(&raw);
        debug!(after_nms = filtered.len(), "YOLO after NMS");

        Ok(filtered)
    }

    /// Parse the raw YOLOv8 output tensor.
    ///
    /// YOLOv8 ONNX export produces [1, 84, 8400] (transposed) where:
    /// - 84 = 4 box coords (center_x, center_y, width, height) + 80 COCO class scores
    /// - 8400 = number of anchor boxes
    fn parse_raw_detections(
        flat: &[f32],
        shape: &[usize],
        pad_x: f32,
        pad_y: f32,
        scale: f32,
        orig_w: u32,
        orig_h: u32,
    ) -> Result<Vec<Detection>> {
        if shape.len() != 3 {
            anyhow::bail!("expected 3D output, got {:?}", shape);
        }

        let (_batch, dim_a, dim_b) = (shape[0], shape[1], shape[2]);
        let (num_detections, num_outputs) = if dim_a > dim_b {
            // [1, 8400, 84] format
            (dim_a, dim_b)
        } else {
            // [1, 84, 8400] transposed
            (dim_b, dim_a)
        };

        let is_transposed = dim_a < dim_b;
        let num_classes = num_outputs.saturating_sub(4);

        let mut detections = Vec::with_capacity(num_detections.min(100));

        for i in 0..num_detections {
            let get = |j: usize| -> f32 {
                if is_transposed {
                    flat[j * num_detections + i]
                } else {
                    flat[i * num_outputs + j]
                }
            };

            let cx = get(0);
            let cy = get(1);
            let bw = get(2);
            let bh = get(3);

            // Find best class.
            let mut best_class = 0;
            let mut best_score = 0.0f32;
            for c in 0..num_classes {
                let score = get(4 + c);
                if score > best_score {
                    best_score = score;
                    best_class = c;
                }
            }

            if best_score < CONF_THRESHOLD {
                continue;
            }

            // Convert from padded/letterboxed coordinates back to original image.
            let x1 = ((cx - bw / 2.0) - pad_x) / scale;
            let y1 = ((cy - bh / 2.0) - pad_y) / scale;
            let x2 = ((cx + bw / 2.0) - pad_x) / scale;
            let y2 = ((cy + bh / 2.0) - pad_y) / scale;

            // Normalize to 0..1.
            let nx1 = (x1 / orig_w as f32).clamp(0.0, 1.0);
            let ny1 = (y1 / orig_h as f32).clamp(0.0, 1.0);
            let nx2 = (x2 / orig_w as f32).clamp(0.0, 1.0);
            let ny2 = (y2 / orig_h as f32).clamp(0.0, 1.0);

            let class_name = coco_class_name(best_class);
            detections.push(Detection {
                bbox: [nx1, ny1, nx2, ny2],
                class: class_name.to_string(),
                confidence: best_score,
            });
        }

        Ok(detections)
    }
}

/// Letterbox resize: scale image to fit inside `target_size` while
/// maintaining aspect ratio, then pad with gray.
fn letterbox(image: &DynamicImage, target_size: u32) -> (DynamicImage, f32, f32, f32) {
    let (orig_w, orig_h) = image.dimensions();
    let scale = (target_size as f32 / orig_w as f32).min(target_size as f32 / orig_h as f32);

    let new_w = (orig_w as f32 * scale) as u32;
    let new_h = (orig_h as f32 * scale) as u32;

    let resized = image.resize_exact(new_w, new_h, imageops::FilterType::Triangle);

    let mut padded = DynamicImage::new_rgb8(target_size, target_size);
    let pad_x = (target_size - new_w) / 2;
    let pad_y = (target_size - new_h) / 2;

    imageops::overlay(&mut padded, &resized, pad_x as i64, pad_y as i64);

    (padded, pad_x as f32, pad_y as f32, scale)
}

/// Non-maximum suppression: keep only the best detection per spatial region.
fn nms(detections: &[Detection]) -> Vec<Detection> {
    let mut sorted: Vec<_> = detections.to_vec();
    sorted.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    let mut kept = Vec::with_capacity(sorted.len());
    let mut suppressed = vec![false; sorted.len()];

    for i in 0..sorted.len() {
        if suppressed[i] {
            continue;
        }
        kept.push(sorted[i].clone());
        for j in (i + 1)..sorted.len() {
            if suppressed[j] {
                continue;
            }
            if iou(&sorted[i].bbox, &sorted[j].bbox) > NMS_THRESHOLD {
                suppressed[j] = true;
            }
        }
    }

    kept
}

fn iou(a: &[f32; 4], b: &[f32; 4]) -> f32 {
    let x1 = a[0].max(b[0]);
    let y1 = a[1].max(b[1]);
    let x2 = a[2].min(b[2]);
    let y2 = a[3].min(b[3]);

    let inter_w = (x2 - x1).max(0.0);
    let inter_h = (y2 - y1).max(0.0);
    let inter = inter_w * inter_h;

    let area_a = (a[2] - a[0]) * (a[3] - a[1]);
    let area_b = (b[2] - b[0]) * (b[3] - b[1]);
    let union = area_a + area_b - inter;

    if union <= 0.0 {
        0.0
    } else {
        inter / union
    }
}

/// COCO class names (80 classes, 0-indexed).
fn coco_class_name(id: usize) -> &'static str {
    const NAMES: &[&str] = &[
        "person",
        "bicycle",
        "car",
        "motorcycle",
        "airplane",
        "bus",
        "train",
        "truck",
        "boat",
        "traffic light",
        "fire hydrant",
        "stop sign",
        "parking meter",
        "bench",
        "bird",
        "cat",
        "dog",
        "horse",
        "sheep",
        "cow",
        "elephant",
        "bear",
        "zebra",
        "giraffe",
        "backpack",
        "umbrella",
        "handbag",
        "tie",
        "suitcase",
        "frisbee",
        "skis",
        "snowboard",
        "sports ball",
        "kite",
        "baseball bat",
        "baseball glove",
        "skateboard",
        "surfboard",
        "tennis racket",
        "bottle",
        "wine glass",
        "cup",
        "fork",
        "knife",
        "spoon",
        "bowl",
        "banana",
        "apple",
        "sandwich",
        "orange",
        "broccoli",
        "carrot",
        "hot dog",
        "pizza",
        "donut",
        "cake",
        "chair",
        "couch",
        "potted plant",
        "bed",
        "dining table",
        "toilet",
        "tv",
        "laptop",
        "mouse",
        "remote",
        "keyboard",
        "cell phone",
        "microwave",
        "oven",
        "toaster",
        "sink",
        "refrigerator",
        "book",
        "clock",
        "vase",
        "scissors",
        "teddy bear",
        "hair drier",
        "toothbrush",
    ];
    NAMES.get(id).copied().unwrap_or("unknown")
}

/// Map reCAPTCHA / hCaptcha task text to COCO class names.
///
/// Returns `None` when the task mentions an object not in the COCO
/// dataset (the caller should fall back to VLM in that case).
pub fn task_to_coco_classes(task: &str) -> Option<Vec<&'static str>> {
    let lower = task.to_lowercase();

    // reCAPTCHA / hCaptcha task patterns → COCO classes.
    if lower.contains("traffic light") || lower.contains("traffic lights") {
        return Some(vec!["traffic light"]);
    }
    if lower.contains("bus") || lower.contains("buses") {
        return Some(vec!["bus"]);
    }
    if lower.contains("bicycle") || lower.contains("bicycles") || lower.contains("bike") {
        return Some(vec!["bicycle"]);
    }
    if lower.contains("car")
        || lower.contains("cars")
        || lower.contains("vehicle")
        || lower.contains("vehicles")
    {
        return Some(vec!["car", "truck", "bus", "motorcycle"]);
    }
    if lower.contains("motorcycle") || lower.contains("motorcycles") {
        return Some(vec!["motorcycle"]);
    }
    if lower.contains("train") || lower.contains("trains") {
        return Some(vec!["train"]);
    }
    if lower.contains("boat") || lower.contains("boats") {
        return Some(vec!["boat"]);
    }
    if lower.contains("fire hydrant") || lower.contains("fire hydrants") {
        return Some(vec!["fire hydrant"]);
    }
    if lower.contains("cat") || lower.contains("cats") {
        return Some(vec!["cat"]);
    }
    if lower.contains("dog") || lower.contains("dogs") {
        return Some(vec!["dog"]);
    }
    if lower.contains("person") || lower.contains("people") || lower.contains("pedestrian") {
        return Some(vec!["person"]);
    }
    if lower.contains("bird") || lower.contains("birds") {
        return Some(vec!["bird"]);
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn coco_names_complete() {
        assert_eq!(coco_class_name(0), "person");
        assert_eq!(coco_class_name(9), "traffic light");
        assert_eq!(coco_class_name(79), "toothbrush");
    }

    #[test]
    fn task_mapping_known() {
        assert_eq!(
            task_to_coco_classes("Select all images with traffic lights"),
            Some(vec!["traffic light"])
        );
        assert_eq!(task_to_coco_classes("Select all buses"), Some(vec!["bus"]));
    }

    #[test]
    fn task_mapping_unknown() {
        assert!(task_to_coco_classes("Select all crosswalks").is_none());
        assert!(task_to_coco_classes("Select all chimneys").is_none());
    }

    #[test]
    fn iou_calculation() {
        let a = [0.0, 0.0, 1.0, 1.0];
        let b = [0.5, 0.5, 1.5, 1.5];
        assert!((iou(&a, &b) - 0.1428).abs() < 0.01);
    }

    /// Integration test: load real YOLOv8n ONNX model and run inference.
    /// Skips gracefully if model is not cached and network is unavailable.
    #[test]
    #[cfg(feature = "vision")]
    fn yolov8n_loads_and_runs() {
        use crate::vision::ModelHub;
        let hub = ModelHub::new();
        let rt = tokio::runtime::Runtime::new().unwrap();
        let path = match rt.block_on(async { hub.resolve(super::super::ModelId::YoloV8n).await }) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("Skipping integration test, model unavailable: {e}");
                return;
            }
        };

        let mut detector = YoloDetector::load(&path).expect("load YOLOv8n from disk");

        // Create a synthetic test image (red square on gray background).
        let mut img = image::RgbImage::new(640, 480);
        for pixel in img.pixels_mut() {
            *pixel = image::Rgb([128, 128, 128]);
        }
        for y in 100..300 {
            for x in 100..300 {
                img.put_pixel(x, y, image::Rgb([255, 0, 0]));
            }
        }
        let dyn_img = image::DynamicImage::ImageRgb8(img);

        let detections = detector
            .detect(&dyn_img)
            .expect("run detection without panic");
        // The model runs, we don't assert specific detections on synthetic
        // data because YOLOv8n is trained on real-world COCO images.
        println!(
            "YOLOv8n detections on synthetic image: {}",
            detections.len()
        );
    }
}