A Telegram bot and Flask API for detecting and recognizing text in images using PaddleOCR and TrOCR.
- Detects text regions in images using PaddleOCR.
- Recognizes text in detected regions using TrOCR (transformer-based OCR).
- REST API for programmatic access.
- Telegram bot for easy image-to-text interaction.
- Python 3.7+
- pip
Install all required packages:
pip install paddleocr opencv-python-headless numpy pillow matplotlib flask python-telegram-bot transformers torchNote:
paddleocrmay require additional system dependencies (see PaddleOCR installation guide).torchversion should match your CUDA version if using GPU.
git clone <repo-url>
cd text-detector-botCreate a file named private_data.json in the project root with the following content:
{
"telegram_token": "YOUR_TELEGRAM_BOT_TOKEN"
}Get your bot token from BotFather on Telegram.
python flask_server.py- The server will run on
http://127.0.0.1:5000. - Main endpoint:
POST /api/process_image/- Request JSON:
{ "image": "<base64-encoded-image>" } - Response JSON:
{ "bboxes": [...], "binary_image": "<base64>", "text": ["..."] }
- Request JSON:
python telegram_bot.py- The bot will listen for image messages.
- When you send an image, it will reply with:
- The image with detected text regions highlighted.
- The binarized (preprocessed) image.
- The recognized text.
- Detection: PaddleOCR detects text regions in the image.
- Recognition: Each detected region is cropped and passed to TrOCR for text recognition.
- API: The Flask server exposes an endpoint for image processing.
- Telegram Bot: Forwards images to the API and returns results to the user.
text_detector.py— PaddleOCR-based text detection.trocr.py— TrOCR-based text recognition.flask_server.py— REST API server.telegram_bot.py— Telegram bot logic.utils.py— Image conversion utilities.
- The Telegram bot expects the Flask server to be running locally at
http://127.0.0.1:5000. - For production, consider deploying the Flask server and bot separately, and update the API URL in
telegram_bot.pyif needed. - For GPU acceleration, ensure CUDA is installed and
torchis installed with CUDA support.