Skip to content

fckveza/ocr-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 OCR Keyword Detection API

This project is a FastAPI-based OCR (Optical Character Recognition) service that detects whether a specific keyword appears in an uploaded image.
It uses Tesseract OCR, OpenCV, and RapidFuzz to read and analyze text content from images with automatic rotation and preprocessing for better accuracy.


🚀 Features

  • 🖼️ Image upload API with keyword checking
  • 🔄 Automatic image rotation (fixes portrait/landscape orientation)
  • 🎨 Multiple OCR preprocessing strategies (contrast enhancement, grayscale, etc.)
  • 📊 String similarity matching using RapidFuzz
  • 🧾 Outputs OCR results and confidence scores
  • ⚡ Built with FastAPI + Uvicorn for high performance

📁 Project Structure

├── main.py               # Main FastAPI application
├── gambar/               # Folder where uploaded images are saved
├── hasil/                # Folder for OCR output and processed images
└── requirements.txt      # Python dependencies

🧩 Dependencies

This project uses the following main libraries:

  • fastapi
  • uvicorn
  • opencv-python
  • pytesseract
  • pillow
  • numpy
  • rapidfuzz

⚙️ Installation

  1. Clone this repository

    git clone https://github.com/yourusername/ocr-api.git
    cd ocr-api
  2. Create a virtual environment

    python -m venv venv
    source venv/bin/activate  # On Linux/Mac
    venv\Scripts\activate     # On Windows
  3. Install dependencies

    pip install -r requirements.txt
  4. Install Tesseract OCR


▶️ Running the Server

Start the FastAPI app using Uvicorn:

uvicorn main:app --host 0.0.0.0 --port 8124

or simply run:

python main.py

The server will start on:
👉 http://localhost:8124


🧠 API Usage

Endpoint: /ocr

Method: POST
Content-Type: multipart/form-data

Form Data:

Field Type Description
file File The image file to scan
keyword String The text keyword to search for

Example using curl

curl -X POST "http://localhost:8124/ocr" \
  -F "[email protected]" \
  -F "keyword=DIGITAL"

Example Response

{
  "found": true,
  "match": "DIGITAL",
  "score": 91
}

🧮 Processing Steps

  1. Fix image orientation using EXIF metadata
  2. Run raw OCR directly
  3. Apply preprocessing techniques (grayscale, contrast adjustment)
  4. If no match, rotate the image 180° and retry
  5. Compare OCR results against the target keyword using fuzzy matching

📂 Output Files

All results are saved in the hasil/ directory:

  • step0_raw.jpg → The processed image
  • hasil_ocr.txt → The OCR extracted text

🧑‍💻 Example Workflow

  1. Upload an image via /ocr endpoint
  2. The server processes it, runs OCR, and checks similarity
  3. Returns whether the keyword was found and the similarity score

🛠️ Customization

You can adjust:

  • OCR configuration in ocr_raw() and ocr_preprocessed()
  • Similarity threshold in check_similarity() (default: 85%)
  • Output directory names for results and uploads

📜 License

This project is open-source under the MIT License.

About

Keyword detection from images using FastAPI, Tesseract, OpenCV, and RapidFuzz.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages