Open flame smoke target detection project deployment (YoloV5+Flask)

Open flame smoke target detection project deployment

Article directory

  • Open flame smoke target detection project deployment
  • 1. Pull the Docker PyToch image
  • 2. Configure the system environment
    • 2.1 Change software source
    • 2.2 Download vim
    • 2.3 Solve the problem of Chinese garbled characters in vim
  • 3. Run the project
    • 3.1 Copy the project to the container
    • 3.2 Install the toolkits required for the project
    • 3.3 Start the project
  • 4. Build project mirror
    • 4.1 Docker commit setup
    • 4.2 Dockerfile construction
  • 5. Publish project image

This project was the project I was responsible for last year. There were still some minor problems that had not been completely resolved at that time, and it was also limited by the knowledge base at the time, so it has been shelved until now. After studying relevant knowledge, I spent a lot of time improving this project in the past two days, and finally modified it to the ideal state.

The current effect is to open it through a web page, and the user uploads a picture, and the backend will detect whether there is open flame or smoke in the picture. After debugging, the project can be opened on different devices, and the front end will adapt to different devices. The framework used is PyTorch, the algorithm used is the YoloV5 algorithm (version 4.0), and the model used is the trained YoloV5-S model.

At the same time, I also packaged the project into a mirror through Docker to facilitate deployment on other systems without the need to repeatedly install the environment.

Below is the effect of the project.

1. Pull the Docker PyToch image

Pull the PyTorch image from Docker Hub and perform the following operations on the image.

# Pull the image
$ docker pull pytorch/pytorch:latest
# Generate container
$ docker run -dit --name fire -p 5001:5001 pytorch/pytorch:latest
# Enter the container
$ docker exec -it fire bash

Check the version of the container torch, enter the python terminal and enter the following command. You can see that the torch version used is 1.10.0.

>>> import torch
>>>torch.__version__
'1.10.0'
>>>

2. Configure system environment

2.1 Change software source

First back up the original software source, the command is as follows.

cp /etc/apt/sources.list /etc/apt/sources.list.bak

Because the image does not have vim installed, the content of the /etc/apt/sources.list file can only be changed through the echo command.
Tsinghua Source

# Clear file contents
echo "">/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe">>/etc/apt/sources.list
echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse">>/etc/apt/sources.list

Alibaba

echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse">>/etc/apt/sources.list
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse">>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse">>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse">>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse">>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse">>/etc/apt/sources.list
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse">>/etc/apt/sources.list

Update software sources.

apt-get update
apt-get upgrade

2.2 Download vim

You need to use vim to modify the file content, so you need to download it.

apt-get install vim -y

After the download is complete, you can check the vim version through the following command.

vim --version

2.3 Solve vim Chinese garbled problem

Modify the content of /etc/vim/vimrc and add the following content at the end:

set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8

After setting, the Chinese in the file can be displayed normally.

3. Run the project

3.1 Copy the project to the container

Copy the project code files on your local machine to the working directory of the container.

$ docker cp "Path of the project on this machine" fire:/workspace/

The function implemented by the above command is to copy the project to the /workspace/ directory in the fire container.

3.2 Install the tool packages required for the project

Use the pip command to download the toolkit, followed by the Tsinghua source -i, and finally the name of the toolkit.

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple packageName

In this container, the packages I need to install are as follows:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python-headless
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyyaml

Among them, after I installed the opencv-python package, I reported an error when executing the project:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

The solution found on the Internet is to install opencv-python-headless.

3.3 Start the project

In flask_app.py, you need to check the host number and port number. The host number must be written as 0.0.0.0, otherwise the project cannot be opened locally. If the port number is the same as when creating the container The mapped ports are consistent (here I set 5001). And turn off debugging mode.

app.run(debug=False, host='0.0.0.0', port=5001)

After completing the above operations, enter the following command in the terminal to start the project:

python flask_app.py

At this time, enter localhost:5001 in the local browser to successfully open the project page!

4. Build project image

Now encapsulate the container where the project is located into an image to facilitate deployment on different systems. Here I use two methods to build it, namely Docker commit and Dockerfile to build the project image.

4.1 Docker commit construction

In Docker, the image is a multi-layer storage, and each layer is modified based on the previous layer; the container is also a multi-layer storage, which uses the image as the base layer and adds one layer on top of it as the container. Runtime storage layer.

In this project, we created the fire container based on the pytorch image and made modifications in the container. Specific changes can be seen through the docker diff command.

$ docker diff CONTAINER

The docker commit command can save the storage layer of the container into an image. In other words, on the basis of the original image, the storage layer of the container is superimposed to form a new image. The syntax format of docker commit is:

$ docker commit [options] <container ID or container name> [<warehouse name>[:<label>]]

In this project, I use the following instructions to build the project image:

$ docker commit --author "xxxx" --message "Fire Detection Project" fire username/image:tag

Among them, --author is the designated author, and --message records the content of this modification. This is similar to git version control, but this information can be omitted here. It should be noted that the warehouse name must be in lowercase.

Use the docker image ls command to view our newly created image.

Use the docker run command to generate a project container based on the project image. The container has already configured the environment, and you can start the service directly in the container.

4.2 Dockerfile construction

The content of the Dockerfile file is as follows:

# FROM specifies the base image
FROM pytorch/pytorch:1.8.1-cuda10.2-cudnn7-runtime
#Specify the working path of the Docker command. If the path does not exist, Docker will automatically create it.
WORKDIR/app
# Copy all programs to the Docker image
# COPY <local path> <destination path>
# The first '.' represents all files in the program root directory; the second '.' represents the path in the Docker image, which is the current working path '/app/'
COPY . .
# Create a mirror, install the environment, the RUN command is used when creating a mirror
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak \
     & amp; & amp; echo "">/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe">>/etc/apt/sources.list \
     & amp; & amp; echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse">>/etc/apt/sources.list \
     & amp; & amp; apt-get update \
     & amp; & amp; apt-get install vim -y \
     & amp; & amp; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# The CMD instruction is used to specify the command to be executed when the Docker container is running.
CMD ["python","flask_app.py"]

The content of requirements.txt is as follows.

opencv-python-headless
matplotlib
scipy
flask
pyyaml

Enter the following instructions in the Dockerfile file directory:

$ docker build -t firedemo .

The -t parameter specifies the image name. . tells docker to look for the Dockerfile file in this directory.

Start a container through the docker run command. The instructions are as follows:

$ docker run -p 5001:5001 -d --name firetest firedemo

Then enter localhost:5001 on your local browser to use the service!

5. Publish project image

Create a repository on Docker Hub. Push the built project image to the remote warehouse with the following instructions:

$ docker push username/image:tag

After the upload is successful, you can now pull it from the remote warehouse for deployment.

$ docker run -dit -p 5001:5001 --name firetest username/image:tag