Skip to content

firasayari10/OCR-task

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ₯ Medical Prescription OCR System with Multi-Agent Architecture

An advanced OCR system for medical prescriptions using a multi-agent architecture with privacy-first PHI filtering and automated drug information extraction.


🎯 System Overview

This system processes medical prescription images through a sophisticated multi-agent pipeline that:

  1. Extracts text from prescription images using Azure Vision OCR
  2. Detects and redacts PHI (Protected Health Information) automatically
  3. Identifies medications and queries multiple drug databases for alternatives
  4. Provides drug information from FDA, RxNorm, and AI-powered sources

Key Features

  • πŸ€– 6-Agent Architecture - Orchestrated multi-agent system for specialized tasks
  • πŸ”’ HIPAA-Compliant PHI Filtering - Automatic detection and redaction of sensitive information
  • πŸ’Š Drug Information Extraction - Automated medication detection with database queries
  • πŸ”„ Multi-Database Drug Lookup - RxNorm (NIH), FDA openFDA, and LLaMA AI fallback
  • πŸ–ΌοΈ Image Segmentation - SAM2-powered region detection (with fallback)
  • πŸ“± Modern React UI - Beautiful, responsive interface for image upload and results display

πŸ—οΈ Architecture

Multi-Agent System

The system uses 6 specialized agents coordinated by an orchestrator:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    OrchestratorAgent                         β”‚
β”‚              (Routes tasks to specialized agents)            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        OCRAgent                              β”‚
β”‚           (Coordinates full pipeline workflow)               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           ↓              ↓              ↓              ↓
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚Segmentationβ”‚  β”‚    Text    β”‚  β”‚    PHI     β”‚  β”‚    Drug    β”‚
  β”‚   Agent    β”‚  β”‚Recognition β”‚  β”‚   Filter   β”‚  β”‚Information β”‚
  β”‚            β”‚  β”‚   Agent    β”‚  β”‚   Agent    β”‚  β”‚   Agent    β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Processing Pipeline

Image Upload
    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  STEP 1: Image Segmentation         β”‚
β”‚  Agent: SegmentationAgent           β”‚
β”‚  Tools: SAM2 / Fallback Contours    β”‚
β”‚  Output: Detected regions           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  STEP 2: Text Recognition           β”‚
β”‚  Agent: TextRecognitionAgent        β”‚
β”‚  Tools: Azure Vision OCR / TrOCR    β”‚
β”‚  Output: Extracted text             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  STEP 3: PHI Filtering              β”‚
β”‚  Agent: PHIFilterAgent              β”‚
β”‚  Tools: HuggingFace NER + Regex     β”‚
β”‚  Output: Redacted text, PHI list    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  STEP 4: Drug Information           β”‚
β”‚  Agent: DrugInformationAgent        β”‚
β”‚  Tools: RxNorm, FDA, LLaMA AI       β”‚
β”‚  Output: Medications + Alternatives β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    ↓
Final Response to UI

πŸ€– Agent Details

1. OrchestratorAgent

  • Purpose: Routes incoming tasks to the appropriate specialized agent
  • Routing Rules:
    • "prescription" β†’ OCRAgent
    • "segment" β†’ SegmentationAgent
    • "phi" / "hipaa" β†’ PHIFilterAgent
    • "drug" / "medication" β†’ DrugInformationAgent
  • Capabilities: Task delegation, agent discovery, priority-based routing

2. OCRAgent

  • Purpose: Coordinates the entire OCR pipeline
  • Workflow:
    1. Delegates segmentation to SegmentationAgent
    2. Delegates text extraction to TextRecognitionAgent
    3. Delegates PHI filtering to PHIFilterAgent
    4. Delegates drug extraction to DrugInformationAgent
  • Processing Modes:
    • full - Complete pipeline (default)
    • ocr_only - Text extraction only
    • segment_only - Image segmentation only

3. SegmentationAgent

  • Purpose: Detects and extracts regions from prescription images
  • Tools:
    • SAM2 (Segment Anything Model 2) - Advanced AI segmentation
    • Fallback Contour Detection - OpenCV-based region detection
    • Region Extraction - Crops and preprocesses detected regions
  • Output: List of regions with bounding boxes and cropped images

4. TextRecognitionAgent

  • Purpose: Extracts text from images using OCR
  • Tools:
    • Azure Vision OCR - Microsoft's cloud OCR API (primary)
    • TrOCR - Transformer-based handwriting recognition (optional)
  • Methods:
    • auto - Tries all available methods
    • azure - Azure Vision only
    • trocr - TrOCR only
  • Output: Extracted text with confidence scores

5. PHIFilterAgent

  • Purpose: Detects and redacts Protected Health Information
  • Tools:
    • HuggingFace NER - Named Entity Recognition for person names
    • Regex Patterns - Pattern matching for structured PHI
  • Detects:
    • πŸ‘€ Names - Patient and doctor names
    • πŸ“… Dates - Birth dates, appointment dates
    • πŸ“ž Phone Numbers - All formats
    • πŸ“§ Email Addresses
    • 🏠 Addresses - Street addresses
    • πŸ”’ SSN - Social Security Numbers
    • πŸ†” Medical IDs - MRNs, Patient IDs
    • πŸ’³ Insurance Numbers
  • Output: Redacted text (PHI replaced with [TYPE_REDACTED]), PHI entity list

6. DrugInformationAgent

  • Purpose: Extracts medications and queries drug databases
  • Tools:
    • FAISS Vector Database - Local database of 106 essential medications with semantic search
    • Medication Extractor - Regex-based pattern matching
    • RxNorm API - NIH drug database (free, international)
    • FDA openFDA API - US FDA drug labels database
    • LLaMA AI - AI-powered fallback for unknown drugs
  • Query Priority:
    1. Vector Database (local, 10ms response time)
    2. RxNorm API (NIH, international coverage)
    3. FDA openFDA (US drug labels)
    4. LLaMA AI (AI-generated fallback)
  • Extraction Patterns:
    • Tab. Augmentin 625mg
    • Cap. Amoxicillin 500mg
    • Drug names with common suffixes (cillin, mycin, pril, etc.)
    • 100+ common medication names
  • Output:
    • Medication list (name + dosage)
    • Drug alternatives from multiple sources
    • Manufacturer information
    • Indications and usage
    • Category and usage classification
    • Multi-source information combining all databases

πŸ› οΈ Technical Stack

Backend

  • Framework: FastAPI (Python)
  • Agent System: Custom multi-agent framework with async/await
  • OCR: Azure Vision API
  • Segmentation: SAM2 (Meta AI) + OpenCV fallback
  • NLP: HuggingFace Transformers (NER models)
  • Vector Database: FAISS + SentenceTransformers (all-MiniLM-L6-v2)
  • Drug APIs: RxNorm (NIH), openFDA, HuggingFace LLaMA
  • Image Processing: OpenCV, NumPy, PIL

Frontend

  • Framework: React 18
  • Build Tool: Vite
  • Styling: CSS3 with modern features
  • State Management: React Hooks

APIs & Services

  • Azure Vision API - Text extraction
  • RxNorm REST API - Drug information
  • FDA openFDA API - US drug labels
  • HuggingFace Inference API - NER and LLaMA
  • Meta SAM2 - Image segmentation
  • FAISS Vector Database - Local medication database (106 drugs)

πŸ“ Project Structure

ocr/
β”œβ”€β”€ backend/                          # Backend API server
β”‚   β”œβ”€β”€ agent_system.py              # Agent system initialization
β”‚   β”œβ”€β”€ main_agent_api.py            # FastAPI server with agent routes
β”‚   β”œβ”€β”€ main.py                      # Legacy API (reference)
β”‚   β”œβ”€β”€ requirements.txt             # Python dependencies
β”‚   β”œβ”€β”€ .env                         # Configuration (keys, tokens)
β”‚   β”œβ”€β”€ drugs.json                   # Essential medications dataset (106 drugs)
β”‚   β”œβ”€β”€ load_drugs_json.py           # Load medications into vector DB
β”‚   β”œβ”€β”€ medication_vector_db.py      # Vector database implementation
β”‚   β”œβ”€β”€ agents/                      # Agent implementations
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ base_agent.py           # Base agent class
β”‚   β”‚   β”œβ”€β”€ orchestrator.py         # Task routing agent
β”‚   β”‚   β”œβ”€β”€ ocr_agent.py            # Pipeline coordinator
β”‚   β”‚   β”œβ”€β”€ segmentation_agent.py   # Image segmentation
β”‚   β”‚   β”œβ”€β”€ text_recognition_agent.py # Text extraction
β”‚   β”‚   β”œβ”€β”€ phi_filter_agent.py     # PHI detection/redaction
β”‚   β”‚   β”œβ”€β”€ drug_information_agent.py # Medication extraction
β”‚   β”‚   └── tools.py                # Tool implementations
β”‚   β”œβ”€β”€ medication_db/               # Vector database storage
β”‚   β”‚   β”œβ”€β”€ faiss_index.bin         # FAISS vector index
β”‚   β”‚   └── metadata.json           # Medication metadata
β”‚   β”œβ”€β”€ checkpoints/                 # Model checkpoints
β”‚   β”‚   └── sam2_hiera_large.pt     # SAM2 model (900MB)
β”‚   └── segment-anything-2/          # SAM2 library
β”‚
β”œβ”€β”€ src/                             # React frontend
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ LandingPage.jsx         # Main UI component
β”‚   β”‚   └── LandingPage.css         # Styling
β”‚   β”œβ”€β”€ App.jsx
β”‚   └── main.jsx
β”‚
β”œβ”€β”€ index.html
β”œβ”€β”€ package.json
β”œβ”€β”€ vite.config.js
└── README.md

πŸš€ Getting Started

Prerequisites

  1. Python 3.8+
  2. Node.js 16+
  3. Azure Vision API credentials
  4. HuggingFace API token (optional, for LLaMA)

Backend Setup

  1. Install Python dependencies:
cd backend
pip install -r requirements.txt
  1. Build Vector Database (essential medications):
# Load 106 essential medications from drugs.json into vector database
python load_drugs_json.py drugs.json

This creates a FAISS vector database in medication_db/ with semantic search capabilities.

  1. Install SAM2 (optional, for advanced segmentation):
cd segment-anything-2
pip install -e .
  1. Download SAM2 checkpoint (optional):
cd ..
python download_sam2_checkpoint.py
  1. Configure environment variables (.env file):
# Azure Vision API (Required)
AZURE_VISION_ENDPOINT=https://your-resource.cognitiveservices.azure.com/
AZURE_VISION_KEY=your_azure_key

# HuggingFace API (Optional - for LLaMA AI fallback)
HF_TOKEN=your_huggingface_token
  1. Start the agent API server:
python main_agent_api.py

Server will be available at http://localhost:8000

Frontend Setup

  1. Install Node dependencies:
npm install
  1. Start development server:
npm run dev

App will be available at http://localhost:5173


πŸ“‘ API Endpoints

Main Endpoints

POST /api/process-image

Process an image through the full agent pipeline.

Request:

curl -X POST http://localhost:8000/api/process-image \
  -F "file=@prescription.jpg" \
  -F "mode=full" \
  -F "filter_phi=true"

Parameters:

  • file - Image file (JPEG, PNG)
  • mode - Processing mode: full, ocr_only, segment_only
  • filter_phi - Enable PHI filtering (default: true)
  • include_regions - Include region details (default: false)

Response:

{
  "success": true,
  "mode": "full",
  "agent_used": "OCRAgent",
  "tools_used": ["azure_vision_ocr", "filter_phi", "extract_medications"],
  "regions_detected": 3,
  "extracted_text": "Tab. Augmentin 625mg...",
  "redacted_text": "[NAME_REDACTED]...",
  "phi_summary": [
    {"type": "NAME", "original": "John Doe"},
    {"type": "DATE", "original": "12-09-2025"}
  ],
  "medications": [
    {"name": "augmentin", "dosage": "625mg"}
  ],
  "drug_alternatives": [
    {
      "original_drug": {"name": "augmentin", "dosage": "625mg"},
      "drug_info": {
        "found": true,
        "alternatives": [...],
        "source": "RxNorm (NIH)"
      }
    }
  ]
}

POST /api/phi/filter

Filter PHI from text directly (without image).

Request:

curl -X POST http://localhost:8000/api/phi/filter \
  -H "Content-Type: application/json" \
  -d '{"text": "Patient: John Doe, Age: 45, Phone: 555-1234"}'

GET /health

Check system health and status.

Response:

{
  "status": "healthy",
  "agent_system_initialized": true,
  "sam2_loaded": false,
  "trocr_loaded": false
}

GET /api/agent-status

Get detailed agent system status.


πŸ”§ Configuration

Processing Modes

  • full - Complete pipeline (recommended)

    • Segmentation β†’ OCR β†’ PHI Filtering β†’ Drug Extraction
  • ocr_only - Text extraction only

    • Skips segmentation, directly extracts text
  • segment_only - Image segmentation only

    • Returns detected regions without text extraction

Agent Configuration

Edit agent_system.py to:

  • Enable/disable SAM2 segmentation
  • Enable/disable TrOCR handwriting recognition
  • Configure model checkpoints
  • Adjust agent routing rules

PHI Filtering Rules

Customize PHI detection in agents/phi_filter_agent.py:

  • Add custom regex patterns
  • Configure NER model
  • Adjust redaction format

Drug Extraction Patterns

Customize medication extraction in agents/drug_information_agent.py:

  • Add medication name patterns
  • Configure database priorities
  • Adjust extraction rules

πŸ§ͺ Testing

Test with Sample Image

# Backend
cd backend
python main_agent_api.py

# In another terminal
curl -X POST http://localhost:8000/api/process-image \
  -F "file=@test_prescription.jpg" \
  -F "mode=full"

Test Individual Agents

from agent_system import get_agent_system
import asyncio

async def test():
    # Initialize system
    agent_system = await get_agent_system()
    
    # Test PHI filtering
    response = await agent_system.phi_filter_agent.process(
        "Filter PHI",
        {"text": "Patient: John Doe, DOB: 01/15/1980"}
    )
    print(response.data)

asyncio.run(test())

πŸ”’ Privacy & Security

PHI Protection

  • All PHI is detected and redacted before drug information queries
  • No PHI is sent to external APIs (RxNorm, FDA, LLaMA)
  • Redacted text uses placeholder format: [TYPE_REDACTED]

HIPAA Compliance Features

  • βœ… Automatic PHI detection and redaction
  • βœ… No PHI logging in agent responses
  • βœ… Privacy-first design (PHI filtered before external API calls)
  • ⚠️ Note: This is a demonstration system. For production HIPAA compliance, additional measures required:
    • Encrypted storage
    • Access logging
    • Audit trails
    • BAA with cloud providers

🎨 UI Features

Image Upload

  • Drag & drop support
  • Format support: JPEG, PNG
  • Real-time processing feedback

Results Display

  • Agent Badge - Shows which agent processed the image
  • Tools Badge - Lists tools used in pipeline
  • Text Display - Original extracted text
  • Redacted Text - PHI-filtered version
  • PHI Summary - Detailed list of detected PHI entities
  • Medications - Extracted drugs with dosages
  • Drug Alternatives - Database results for each medication

Interactive Features

  • Toggle between original/segmented image view
  • Copy text to clipboard
  • Download full report with metadata
  • Show/hide detailed metadata
  • Region statistics display

πŸ› Troubleshooting

"Agent system not initialized"

Solution: Wait a few seconds after server start for initialization.

"No text detected"

Solutions:

  • Verify Azure Vision API credentials in .env
  • Check image quality and resolution
  • Ensure prescription is clearly visible

"SAM2 not loaded"

Solution: This is optional. System uses fallback segmentation. To enable:

cd backend/segment-anything-2
pip install -e .
cd ..
python download_sam2_checkpoint.py

"TrOCR not loaded"

Solution: Optional for handwriting. To enable:

pip install "huggingface-hub>=0.24.0,<1.0"
pip install transformers --upgrade

πŸ“Š Performance

Processing Times (Approximate)

  • Segmentation: 1-3 seconds (with fallback)
  • SAM2 Segmentation: 5-10 seconds (if enabled)
  • Azure Vision OCR: 2-5 seconds
  • PHI Filtering: <1 second
  • Drug Information: 1-3 seconds per medication

Total Pipeline: 5-15 seconds per image

Optimization Tips

  1. Use ocr_only mode if segmentation not needed
  2. Disable SAM2 for faster processing (fallback is sufficient)
  3. Cache drug information results
  4. Process multiple images in batch mode

πŸ”„ Future Enhancements

  • Multi-language support
  • Batch processing UI
  • Drug interaction checker
  • Dosage validation
  • Prescription history tracking
  • Export to PDF/JSON
  • Advanced handwriting recognition
  • Custom agent creation API

πŸ“ License

This project is for educational and demonstration purposes.


🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ“§ Support

For issues or questions, please open an issue on GitHub.


πŸ™ Acknowledgments

  • Azure Vision API - Microsoft Cognitive Services
  • SAM2 - Meta AI Research
  • RxNorm - National Library of Medicine (NLM)
  • openFDA - U.S. Food & Drug Administration
  • HuggingFace - Transformers and Inference API
  • FastAPI - Modern Python web framework
  • React - Facebook Open Source

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors