# YOLOv5-RT-TFJS
**Repository Path**: CV_Lab/yolov5_rt_tfjs
## Basic Information
- **Project Name**: YOLOv5-RT-TFJS
- **Description**: ๐ ๅบไบ TensorFlow.js ็ YOLOv5 ๅฎๆถ็ฎๆ ๆฃๆต้กน็ฎใๆฏๆ่ชๅฎไนๆจกๅ๏ผ่ฝป้ๅฎ่ฃ
ใไฝฟ็จๆนไพฟใๅฏ็งปๆคๆงๅผบใ
- **Primary Language**: JavaScript
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: https://gitee.com/PyCVer
- **GVP Project**: No
## Statistics
- **Stars**: 111
- **Forks**: 34
- **Created**: 2022-02-23
- **Last Updated**: 2025-05-30
## Categories & Tags
**Categories**: cv
**Tags**: YOLOv5, JavaScript, ๅฎๆถ็ฎๆ ๆฃๆต, ่ฎก็ฎๆบ่ง่ง, TensorFlow
## README
[ไธญๆ็ฎไฝ](./README.md)|English
YOLOv5 real-time target detection project based on TensorFlow.js, supports custom models
Lightweight installation, easy to use, and strong portability
๐Update Trend
- `2023-01-09` **โก [yolov5-rt-tfjs v0.4.2 release version](https://gitee.com/CV_Lab/yolov5_rt_tfjs/releases/v0.4.2)officially launched**
- `2022-08-05` **โก [yolov5-rt-tfjs v0.4 release version](https://gitee.com/CV_Lab/yolov5_rt_tfjs/releases/v0.4)officially launched**
- `2022-03-02` **โก [yolov5-rt-tfjs v0.3 release version](https://gitee.com/CV_Lab/yolov5_rt_tfjs/releases/v0.3)officially launched**
- **โก [yolov5-rt-tfjs historical version](./v_change)**
๐Project Purpose
### ๐ฅ Custom Object Detection Task
### โค๏ธ Custom Model
๐ Modify [yolov5_rt_tfjs.js](./yolov5_rt_tfjs_src/static/js/yolov5_rt_tfjs.js) file, customize **weight file**, **category name** and **model input size**
```javascript
// weight file
const weights = '../yolov5s_web_model/model.json';
// classification name (English version)
const cls_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush'
]
```
### โค๏ธ Custom TensorFlow.js version
๐ Modify the [index.html](./yolov5_rt_tfjs_src/index.html) file to customize the version of TensorFlow.js
```html
```
โ Note: **TensorFlow.js version>=3.9.0**, the current version used in this project is **4.2.0**
๐ก Project Structure
```shell
.
โโโyolov5_rt_tfjs # project name
โ โโโ yolov5 # YOLOv5 project
โ โ โโโ yolov5s.pt # yolov5s model file
โ โ โโโ yolov5s_web_model # pt2tfjs model 01 (used)
โ โ โโโ yolov5s_saved_model # pt2tfjs model 02
โ โ โโโ yolov5s.pb # pt2tfjs model 03
โ โ โโโ ... # other
โ โโโ yolov5_rt_tfjs_src # Project main directory
โ โ โโโ yolov5s_web_model # pt2tfjs model 01
โ โ โโโ static # static file directory
โ โ โ โโโ js # JavaScript directory
โ โ โ โโโ yolov5_rt_tfjs.js # JS main run file
โ โ โ โโโ css # JavaScript directory
โ โ โ โโโ main.css # css style file
โ โ โโโ index.html # Project main run file
โ โโโ yolov5_rt_tfjs_hls # Project main directory๏ผhls.js version๏ผ
โ โ โโโ static # static file directory
โ โ โโโ โโโ yolov5s_web_model # pt2tfjs model 01
โ โ โ โโโ js # JavaScript directory
โ โ โ โ โโโ yolov5_rt_tfjs.js # JS main run file๏ผhls.js version๏ผ
โ โ โ โโโ css # JavaScript directory
โ โ โ โโโ main.css # css style file
โ โ โโโ index.html # Project main run file
โ โโโ yolov5_rt_tfjs_flask # Flask version main directory
โ โ โโโ templates # flask template directory
โ โ โ โโโ index.html # flask html file
โ โ โโโ server.py # flask server file
โ โ โโโ README.md # Flask version project description
โ โโโ yolov5_rt_tfjs_fastapi # FastAPI version main directory
โ โ โโโ templates # fastapi template directory
โ โ โ โโโ index.html # fastapi html file
โ โ โโโ server.py # fastapi server file
โ โ โโโ README.md # Fastapi version project description
โ โโโ v_change # Version history
โ โ โโโ v01_change.md # v0.1 version changes
โ โ โโโ v02_change.md # v0.2 version changes
โ โ โโโ README.md # Historical release notes
โ โโโ setup.sh # install script
โ โโโ setup_flask.sh # install script (flask version)
โ โโโ setup_fastapi.sh # install script๏ผfastapi version๏ผ
โ โโโ LICENSE # Project License
โ โโโ CodeCheck.md # code inspection
โ โโโ .gitignore # git ignore files
โ โโโ .gitmodules # git submodule file
โ โโโ README.md # project instruction
โ โโโ README.en.md # Project Description (English)
```
โ Note: Some model files need to be generated after installation
โค๏ธ Version Creativity
๐ฅ yolov5-rt-tfjs v0.2 adopts **bounding box and label decoupling management mode**
๐ฅInstallation Tutorial
### โ
Method 1: Linux Shell Quick Installation
๐ Create a conda environment
```shell
conda create -n yolo python==3.8
```
๐ Execute [bash script](./setup.sh)
```shell
bash setup.sh
```
### โ
Method 2: Manual installation
๐ Create a conda environment
```shell
conda create -n yolo python==3.8
conda activate yolo # ่ฟๅ
ฅ็ฏๅข
```
๐ Install dependencies
```shell
pip install -r ./requirements.txt -U
pip install tensorflowjs # >=3.9.0
```
โ Notice: [tensorflowjs>=3.9.0](https://pypi.org/project/tensorflowjs/)
๐ Clone the project (recursive clone)
```shell
git clone https://gitee.com/CV_Lab/yolov5_rt_tfjs.git --recursive
```
๐ Add the weight file of yolov5 or **custom weight file** (.pt) to the root directory of yolov5
๐ Convert, take [yolov5s.pt](https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt) as an example
```shell
python export.py --weights yolov5s.pt --include tfjs
```
๐ Add `yolov5s_web_model` directory to `yolov5_rt_tfjs_src`
โค๏ธ This project provides 2 extension demos: Flask version and FastAPI version, which aims to show that the project has good portability. Click on the above logo to learn about the detailed installation and usage tutorials.
โ Note: Before using the following methods, make sure that the `yolov5s_web_model` directory is added to `yolov5_rt_tfjs_src/static`.
### โ
Method 3: [Flask version](./yolov5_rt_tfjs_flask)
๐ Create a conda environment
```shell
conda create -n yolo python==3.8
conda activate yolo # into the environment
```
๐ Execute the [setup_flask.sh](./setup_flask.sh) script
```shell
bash setup_flask.sh
```
### โ
Method 4: [FastAPI version](./yolov5_rt_tfjs_fastapi)
๐ Create a conda environment
```shell
conda create -n yolo python==3.8
conda activate yolo # into the environment
```
๐ Execute the [setup_fastapi.sh](./setup_fastapi.sh) script
```shell
bash setup_fastapi.sh
```
โกUsage
### ๐ก Method 1: [VSCode](https://code.visualstudio.com/) run
๐ Install [Live Server](https://github.com/ritwickdey/vscode-live-server-plus-plus) plugin
๐ Run the program with `yolov5_rt_tfjs_src` as the working directory
โค๏ธ Universal version
```shell
cd yolov5_rt_tfjs_src # enter the working directory
code . # open vscode
# Right click, select Open with Live Server, run the program
```
โค๏ธ hls.js version
```shell
cd yolov5_rt_tfjs_hls # enter the working directory
code . # open vscode
# Right click, select Open with Live Server, run the program
```
### ๐ก Method 2: [Flask](https://github.com/pallets/flask) version
๐ Run the file [server.py](./yolov5_rt_tfjs_flask/server.py)
```shell
python server.py
```
๐ Browser input `http://127.0.0.1:5000/`
### ๐ก Method 3: [FastAPI](https://github.com/tiangolo/fastapi) version
๐ Run the file [server.py](./yolov5_rt_tfjs_fastapi/server.py)
```shell
python server.py
```
๐ Browser type `http://localhost:8000/`
## ๐ทThanks
- [YOLOv5](https://github.com/ultralytics/yolov5)Founder: [Glenn Jocher](https://github.com/glenn-jocher)
- [Add TensorFlow and TFLite export](https://github.com/ultralytics/yolov5/pull/1127) PR contributor: [Jiacong Fang](https://github.com/zldrobit)
- [TensorFlow.jsโReal-Time Object Detection Demo](https://codesandbox.io/s/z364noozrm) author: [Nick Bourdakos](https://github.com/bourdakos1)