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.
- 🖼️ 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
├── main.py               # Main FastAPI application
├── gambar/               # Folder where uploaded images are saved
├── hasil/                # Folder for OCR output and processed images
└── requirements.txt      # Python dependencies
This project uses the following main libraries:
- fastapi
- uvicorn
- opencv-python
- pytesseract
- pillow
- numpy
- rapidfuzz
- 
Clone this repository git clone https://github.com/yourusername/ocr-api.git cd ocr-api
- 
Create a virtual environment python -m venv venv source venv/bin/activate # On Linux/Mac venv\Scripts\activate # On Windows 
- 
Install dependencies pip install -r requirements.txt 
- 
Install Tesseract OCR - Ubuntu/Debian
sudo apt update sudo apt install tesseract-ocr 
- Windows
- Download and install from: https://github.com/UB-Mannheim/tesseract/wiki
 
 
- Ubuntu/Debian
Start the FastAPI app using Uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8124or simply run:
python main.pyThe server will start on:
👉 http://localhost:8124
Method: POST
Content-Type: multipart/form-data
| Field | Type | Description | 
|---|---|---|
| file | File | The image file to scan | 
| keyword | String | The text keyword to search for | 
curl -X POST "http://localhost:8124/ocr" \
  -F "[email protected]" \
  -F "keyword=DIGITAL"{
  "found": true,
  "match": "DIGITAL",
  "score": 91
}- Fix image orientation using EXIF metadata
- Run raw OCR directly
- Apply preprocessing techniques (grayscale, contrast adjustment)
- If no match, rotate the image 180° and retry
- Compare OCR results against the target keyword using fuzzy matching
All results are saved in the hasil/ directory:
- step0_raw.jpg→ The processed image
- hasil_ocr.txt→ The OCR extracted text
- Upload an image via /ocrendpoint
- The server processes it, runs OCR, and checks similarity
- Returns whether the keyword was found and the similarity score
You can adjust:
- OCR configuration in ocr_raw()andocr_preprocessed()
- Similarity threshold in check_similarity()(default: 85%)
- Output directory names for results and uploads
This project is open-source under the MIT License.