# 81newark/anticap architecture

> Architecture diagram of the GitHub repository 81newark/anticap, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/81newark/anticap
- Repository: https://github.com/81newark/anticap

## Overview

##### Project type and purpose
- AntiCAP is a Python library/tool (not a full-stack app) for recognizing various CAPTCHA types:
- OCR text CAPTCHAs (via DdddOCR)
- Arithmetic CAPTCHAs (via a PyTorch model)
- Sliding‐puzzle CAPTCHAs (gap‐match and shadow‐comparison)
- It exposes a simple Python API (with future plans for a WebApi) and relies on pre‐trained ML/ONNX models.

##### File structure
Top level
- setup.py, requirements.txt, main.py – installation, deps, CLI entrypoint
- README.md, LICENSE
- AntiCAP/ (package)
– __init__.py: defines class AntiCAP() and dispatch methods
– Arithmetic.pt: PyTorch model for arithmetic CAPTCHA
– Ddddocr.onnx: ONNX model for text OCR
– README.md, requirements.txt: module‐specific docs & deps
No “frontend” or “backend” directories: this is a library/service component.

##### Key insights from README
- Describes supported CAPTCHA types and usage examples
- Mentions future Web API layer
- Points to a separate “AntiCAP_trainer” repo for training new models → extensibility

##### Components & interactions
a. Main components
1. Client (Python script or future HTTP client)
2. AntiCAP Controller (AntiCAP.AntiCAP class)
3. Strategy Modules:
- DdddOCR Module (loads Ddddocr.onnx via onnxruntime)
- Arithmetic Module (loads Arithmetic.pt via PyTorch)
- Slide-Match Module (image processing logic)
- Slide-Comparison Module (image comparison logic)
4. Model Artifacts
- Ddddocr.onnx
- Arithmetic.pt
5. Dependencies & Runtimes
- Python 3.8
- PyTorch
- onnxruntime
- OpenCV / PIL for image decoding
6. (Optional future) WebApi Layer
7. (External) AntiCAP_trainer service

b. Relationships & data flow
1. Client → AntiCAP Controller (calls specific method, e.g., `.Arithmetic(img_base64)`)
2. Controller decodes Base64 → dispatches to correct Module
3. Module loads corresponding model or uses comparison algorithm
4. Module returns result to Controller → back to Client
5. (Future) Controller may also expose HTTP endpoints

c. Architectural patterns & design principles
- Strategy pattern: each CAPTCHA type is a pluggable “strategy module” under a unified API
- Dependency inversion: high-level Controller depends on abstract module interfaces; modules depend on ML runtimes
- Separation of concerns: image I/O, model inference, result formatting are in separate layers

d. Significant technologies & libraries
- Python 3.8, pip package
- PyTorch for .pt models
- ONNX Runtime for .onnx models
- OpenCV / PIL for Base64 image decoding
- Future: FastAPI or Flask for WebApi

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User"
        Client["Client"]:::client
    end

    subgraph "Package Metadata"
        Setup["setup.py"]:::metadata
        ReqRoot["requirements.txt"]:::metadata
        License["LICENSE"]:::metadata
        ReadmeRoot["README.md"]:::metadata
    end

    subgraph "AntiCAP Library"
        Controller["AntiCAP Controller"]:::controller

        subgraph "Inference Engine"
            OCRModule["DdddOCR Module"]:::module
            ArithmeticModule["Arithmetic Module"]:::module
            SlideMatch["Slide-Match Module"]:::module
            SlideComp["Slide-Comparison Module"]:::module

            OCRModel[(Ddddocr.onnx)]:::artifact
            ArithModel[(Arithmetic.pt)]:::artifact
        end

        Dependencies["Dependencies\n(Python 3.8, PyTorch,\nonnxruntime, OpenCV/PIL)"]:::dependency
        FutureAPI["Future WebAPI Layer"]:::future
    end

    ExternalTrainer["AntiCAP_trainer"]:::external

    %% Connections
    Client -->|"calls API"| Controller
    Controller -->|"dispatch to strategy"| OCRModule
    Controller -->|"dispatch to strategy"| ArithmeticModule
    Controller -->|"dispatch to strategy"| SlideMatch
    Controller -->|"dispatch to strategy"| SlideComp

    OCRModule -->|"loads model"| OCRModel
    ArithmeticModule -->|"loads model"| ArithModel

    OCRModule -->|"uses ONNX Runtime"| Dependencies
    ArithmeticModule -->|"uses PyTorch"| Dependencies
    SlideMatch -->|"uses OpenCV/PIL"| Dependencies
    SlideComp -->|"uses OpenCV/PIL"| Dependencies

    OCRModule -->|"return result"| Controller
    ArithmeticModule -->|"return result"| Controller
    SlideMatch -->|"return result"| Controller
    SlideComp -->|"return result"| Controller

    ExternalTrainer -->|"provides artifacts"| OCRModel
    ExternalTrainer -->|"provides artifacts"| ArithModel

    Controller -->|"may expose HTTP endpoints"| FutureAPI

    %% Click Events
    click Client "https://github.com/81newark/anticap/blob/main/main.py"
    click Controller "https://github.com/81newark/anticap/blob/main/AntiCAP/__init__.py"
    click OCRModule "https://github.com/81newark/anticap/blob/main/AntiCAP/__init__.py"
    click ArithmeticModule "https://github.com/81newark/anticap/blob/main/AntiCAP/__init__.py"
    click OCRModel "https://github.com/81newark/anticap/blob/main/AntiCAP/Ddddocr.onnx"
    click ArithModel "https://github.com/81newark/anticap/blob/main/AntiCAP/Arithmetic.pt"
    click Setup "https://github.com/81newark/anticap/blob/main/setup.py"
    click ReqRoot "https://github.com/81newark/anticap/blob/main/requirements.txt"
    click License "https://github.com/81newark/anticap/tree/main/LICENSE"
    click ReadmeRoot "https://github.com/81newark/anticap/blob/main/README.md"
    click SubReadme "https://github.com/81newark/anticap/blob/main/AntiCAP/README.md"
    click SubReq "https://github.com/81newark/anticap/blob/main/AntiCAP/requirements.txt"

    %% Styles
    classDef client fill:#FFD700,stroke:#333,stroke-width:1px
    classDef controller fill:#87CEEB,stroke:#333,stroke-width:1px
    classDef module fill:#98FB98,stroke:#333,stroke-width:1px
    classDef artifact fill:#FFDEAD,stroke:#333,stroke-width:1px
    classDef dependency fill:#FFE4E1,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px,stroke-dasharray:5 5
    classDef future fill:#FFF0F5,stroke:#333,stroke-width:1px,stroke-dasharray:5 5
    classDef metadata fill:#E0FFFF,stroke:#333,stroke-width:1px
```
