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.
- 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)
/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.
-
Create a telegram bot with BotFather
-
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
-
Add the
TELEGRAM_TOKEN
provided by BotFather to the Python environment or replaceTOKEN
insrc/bot.py
with your token -
Run
python src/bot.py
-
Create a telegram bot with BotFather
-
Clone the repo:
git clone https://github.com/OneMagicKey/Yolov8-ONNX-telegram-bot.git cd Yolov8-ONNX-telegram-bot
-
Replace a dummy
TELEGRAM_TOKEN
in the Dockerfile with your telegram token -
Build the image and run it:
docker build -t yolo-bot . docker run yolo-bot
Deploy on the Render Cloud Platform (Webhooks Version)
- Create a telegram bot with BotFather
- Create a Render account
- Click on the button below to deploy
- Create a telegram bot with BotFather
- Create a Render account
- Go to
New
->Web Service
->Build and deploy from a Git repository
- 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
- Public Git repository -
- Add Environment Variables:
- Name -
TELEGRAM_TOKEN
, value{YOUR_TELEGRAM_TOKEN}
- Name -
PYTHON_VERSION
, value3.10.13
- Name -
- 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.
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:
-
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))
-
Place the resulting
.onnx
file in either thesrc/checkpoints/detection
orsrc/checkpoints/segmentation
folder, depending on the model type -
Add the model to the
model_list
insrc/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