Skip to content

YOLO telegram bot for object detection and instance segmentation, implemented in Python + OpenCV + ONNXRuntime

License

Notifications You must be signed in to change notification settings

OneMagicKey/Yolov8-ONNX-telegram-bot

Repository files navigation

YOLOv8-ONNX-telegram-bot

Code style: black CI

A Telegram bot for object detection and instance segmentation using YOLOv5/YOLOv8/YOLOv10/YOLOv11, implemented in Python + OpenCV + ONNXRuntime.

Additionally, the bot can be deployed on the Render cloud platform for free.

The webhook version of the bot requires around 380 MB of RAM and includes 2 quantized models: yolov11s and yolov11s-seg. The polling version includes 7 quantized models: yolov5s, yolov8s, yolov10s, yolov11s, yolov5s-seg, yolov8s-seg, and yolov11s-seg, and requires approximately 700 MB of RAM.

Features

  • English / Russian language support
  • Object Detection / Instance Segmentation
  • Quantized YOLOv5, YOLOv8, YOLOv10, and YOLOv11 models
  • Support webhooks to deploy as a webservice on the Render platform (webhooks-render branch)
  • Support polling to run bot locally (master branch)

List of Bot Commands

  • /start - start the bot
  • /help - list of available commands
  • /language - set language preferences
  • /model - select a model
  • /color_scheme - select a color scheme ('equal' or 'random' colors for detected objects of the same class)
  • /retina_masks - enable high-quality segmentation masks

These commands are automatically added to the bot at startup.

Run the Bot on Your Local Machine (Polling Version)

Run via Python Environment (Requires Python >= 3.10)

  1. Create a telegram bot with BotFather

  2. Clone the repo and install all the dependencies:

    git clone https://github.com/OneMagicKey/Yolov8-ONNX-telegram-bot.git
    cd Yolov8-ONNX-telegram-bot
    pip install -r requirements.txt
  3. Add the TELEGRAM_TOKEN provided by BotFather to the Python environment or replace TOKEN in src/bot.py with your token

  4. Run python src/bot.py

Run via Docker

  1. Create a telegram bot with BotFather

  2. Clone the repo:

    git clone https://github.com/OneMagicKey/Yolov8-ONNX-telegram-bot.git
    cd Yolov8-ONNX-telegram-bot
  3. Replace a dummy TELEGRAM_TOKEN in the Dockerfile with your telegram token

  4. Build the image and run it:

    docker build -t yolo-bot .
    docker run yolo-bot

Deploy on the Render Cloud Platform (Webhooks Version)

Automatic Deployment

  1. Create a telegram bot with BotFather
  2. Create a Render account
  3. Click on the button below to deploy

Deploy to Render

Manual Deployment

  1. Create a telegram bot with BotFather
  2. Create a Render account
  3. Go to New -> Web Service -> Build and deploy from a Git repository
  4. Build a webservice with the following options:
    • Public Git repository - https://github.com/OneMagicKey/Yolov8-ONNX-telegram-bot
    • Branch - webhooks-render
    • Runtime - Python 3
    • Build Command - pip install -r requirements.txt
    • Start Command - python src/bot.py
    • Instance Type = Free
  5. Add Environment Variables:
    • Name - TELEGRAM_TOKEN, value {YOUR_TELEGRAM_TOKEN}
    • Name - PYTHON_VERSION, value 3.10.13
  6. Deploy the app

Note: Instances under the Render free plan will spin down after about 15 minutes of inactivity. To keep the bot running, configure a cron-job to send a POST request to the bot every 10 minutes.

Adding New Models

To add n/s/m/l/x versions of Yolo to the bot or to adjust the model's image input size, follow these steps:

  1. Export a model to ONNX format:

    pip install ultralytics
    from ultralytics import YOLO
    
    # Load a model
    model_name = 'yolov8s'  # [yolov8s, yolov8m, yolov8s-seg, yolov8m-seg, ...]
    model = YOLO(f"{model_name}.pt")
    
    # Image size
    height, width = (640, 640)  # [(640, 480), (480, 640), ...]
    
    # Export the model
    model.export(format='onnx', opset=14, simplify=True, optimize=True, imgsz=(height, width))
  2. Place the resulting .onnx file in either the src/checkpoints/detection or src/checkpoints/segmentation folder, depending on the model type

  3. Add the model to the model_list in src/bot.py using the format:

    ModelInfo(type, name, input_size, conf, iou, version)

    # Example:
    
    ModelInfo("detection", "yolov8s", (640, 640), version=8),  # small detection model
    ModelInfo("segmentation", "yolov8m-seg", (640, 480), version=8),  # medium segmentation model with rectangular input size

References

  1. Ultralytics
  2. yolov5_onnx_cv

Releases

No releases published

Packages

No packages published