|
| 1 | +--- |
| 2 | +title: AI Deploy - Tutorial - Create a web service to recognize sign language with YOLOv7 |
| 3 | +slug: deploy/tuto-streamlit-yolov7-sign-language |
| 4 | +excerpt: How to build a sign language recognition app with Streamlit |
| 5 | +section: AI Deploy - Tutorials |
| 6 | +order: 13 |
| 7 | +updated: 2023-04-03 |
| 8 | +routes: |
| 9 | + canonical: 'https://docs.ovh.com/gb/en/publiccloud/ai/deploy/tuto-streamlit-yolov7-sign-language/' |
| 10 | +--- |
| 11 | + |
| 12 | +**Last updated 3rd April, 2023.** |
| 13 | + |
| 14 | +## Objective |
| 15 | + |
| 16 | +The purpose of this tutorial is to show how to deploy a web service to recognize **American Sign Language letters** using YOLOv7 model. |
| 17 | + |
| 18 | +In order to do this, you will use [Streamlit](https://streamlit.io/), a Python framework that turns scripts into a shareable web application. You will also learn how to build and use a custom Docker image for a Streamlit application. |
| 19 | + |
| 20 | +For more information on how to train YOLOv7 on a custom dataset, refer to the following [documentation](https://docs.ovh.com/de/publiccloud/ai/notebooks/yolov7-sign-language/). |
| 21 | + |
| 22 | +Here is an overview of the Sign Language recognition app: |
| 23 | + |
| 24 | +{.thumbnail} |
| 25 | + |
| 26 | +## Requirements |
| 27 | + |
| 28 | +- Access to the [OVHcloud Control Panel](https://www.ovh.com/auth/?action=gotomanager&from=https://www.ovh.de/&ovhSubsidiary=de) |
| 29 | +- An AI Deploy project created inside a Public Cloud project |
| 30 | +- A [user for AI Deploy](https://docs.ovh.com/de/publiccloud/ai/users) |
| 31 | +- [Docker](https://www.docker.com/get-started) installed on your local computer |
| 32 | +- Some knowledge about building image and [Dockerfile](https://docs.docker.com/engine/reference/builder/) |
| 33 | +- Your weights obtained from training YOLOv7 model on the [ASL letters dataset](https://public.roboflow.com/object-detection/american-sign-language-letters/1) (refer to the *"Export trained weights for future inference"* part of the [notebook for YOLOv7](https://github.com/ovh/ai-training-examples/blob/main/notebooks/computer-vision/object-detection/miniconda/yolov7/notebook_object_detection_yolov7_asl.ipynb) |
| 34 | + |
| 35 | +## Instructions |
| 36 | + |
| 37 | +You are going to follow different steps to build your Streamlit application. |
| 38 | + |
| 39 | +- More information about Streamlit capabilities can be found [here](https://docs.streamlit.io/en/stable/). |
| 40 | +- Direct link to the full Python script can be found [here](https://github.com/ovh/ai-training-examples/blob/main/apps/streamlit/sign-language-recognition-yolov7-app/main.py). |
| 41 | + |
| 42 | +> [!warning] |
| 43 | +> **Warning** |
| 44 | +> You must have previously created an `asl-volov7-model` Object Storage container when training your model via [AI Notebooks](https://docs.ovh.com/de/publiccloud/ai/notebooks/yolov7-sign-language/). |
| 45 | +> |
| 46 | +> Check that this container contains your **YOLOv7 custom weights**. They will be necessary for the deployment of the app! |
| 47 | +
|
| 48 | +Here we will mainly discuss how to write the `main.py` code, the `requirements.txt` file and the `Dockerfile`. If you want to see the whole code, please refer to the [GitHub](https://github.com/ovh/ai-training-examples/tree/main/apps/streamlit/sign-language-recognition-yolov7-app) repository. |
| 49 | + |
| 50 | +### Write the Streamlit application |
| 51 | + |
| 52 | +Create a Python file named `main.py`. |
| 53 | + |
| 54 | +Inside that file, import your required modules: |
| 55 | + |
| 56 | +```python |
| 57 | +import streamlit as st |
| 58 | +from PIL import Image |
| 59 | +import numpy as np |
| 60 | +import torch |
| 61 | +import cv2 |
| 62 | +import io |
| 63 | +import os |
| 64 | +``` |
| 65 | + |
| 66 | +Load the **YOLOv7** model and your own weights. Put this function in **cache**: |
| 67 | + |
| 68 | +```python |
| 69 | +@st.cache |
| 70 | +def load_model(): |
| 71 | + |
| 72 | + custom_yolov7_model = torch.hub.load("WongKinYiu/yolov7", 'custom', '/workspace/asl-volov7-model/yolov7.pt') |
| 73 | + |
| 74 | + return custom_yolov7_model |
| 75 | +``` |
| 76 | + |
| 77 | +Create the inference function to get prediction: |
| 78 | + |
| 79 | +```python |
| 80 | +def get_prediction(img_bytes, model): |
| 81 | + |
| 82 | + img = Image.open(io.BytesIO(img_bytes)) |
| 83 | + results = model(img, size=640) |
| 84 | + |
| 85 | + return results |
| 86 | +``` |
| 87 | + |
| 88 | +Write the image analysis function: |
| 89 | + |
| 90 | +```python |
| 91 | +def analyse_image(image, model): |
| 92 | + |
| 93 | + if image is not None: |
| 94 | + |
| 95 | + img = Image.open(image) |
| 96 | + bytes_data = image.getvalue() |
| 97 | + img_bytes = np.asarray(bytearray(bytes_data), dtype=np.uint8) |
| 98 | + result = get_prediction(img_bytes, model) |
| 99 | + result.render() |
| 100 | + |
| 101 | + for img in result.imgs: |
| 102 | + RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
| 103 | + im_arr = cv2.imencode('.jpg', RGB_img)[1] |
| 104 | + st.image(im_arr.tobytes()) |
| 105 | + |
| 106 | + result_list = list((result.pandas().xyxy[0])["name"]) |
| 107 | + |
| 108 | + else: |
| 109 | + st.write("no asl letters were detected!") |
| 110 | + result_list = [] |
| 111 | + |
| 112 | + return result_list |
| 113 | +``` |
| 114 | + |
| 115 | +Define the Python function that displays the letters and forms a word: |
| 116 | + |
| 117 | +```python |
| 118 | +def display_letters(letters_list): |
| 119 | + |
| 120 | + word = ''.join(letters_list) |
| 121 | + path_file = "/workspace/word_file.txt" |
| 122 | + with open(path_file, "a") as f: |
| 123 | + f.write(word) |
| 124 | + |
| 125 | + return path_file |
| 126 | +``` |
| 127 | + |
| 128 | +Define the main and start your app: |
| 129 | + |
| 130 | +```python |
| 131 | +if __name__ == '__main__': |
| 132 | + |
| 133 | + st.image("/workspace/head-asl-yolov7-app.png") |
| 134 | + st.write("## Welcome on your ASL letters recognition app!") |
| 135 | + |
| 136 | + model = load_model() |
| 137 | + |
| 138 | + img_file_buffer = st.camera_input("Take your picture in real time:") |
| 139 | + |
| 140 | + result_list = analyse_image(img_file_buffer, model) |
| 141 | + path_file = display_letters(result_list) |
| 142 | + |
| 143 | + if st.button("Clear result"): |
| 144 | + if os.path.isfile(path_file): |
| 145 | + os.remove(path_file) |
| 146 | + print("File has been deleted") |
| 147 | + else: |
| 148 | + print("File does not exist") |
| 149 | + |
| 150 | + if (os.path.exists(path_file)==True): |
| 151 | + with open(path_file, "r") as f: |
| 152 | + content = f.read() |
| 153 | + st.write(content) |
| 154 | + f.close() |
| 155 | + else: |
| 156 | + pass |
| 157 | +``` |
| 158 | + |
| 159 | +### Write the requirements.txt file for the application |
| 160 | + |
| 161 | +The `requirements.txt` file will allow us to write all the modules needed to make our application work. This file will be useful when writing the `Dockerfile`. |
| 162 | + |
| 163 | +```console |
| 164 | +torchvision==0.14.0 |
| 165 | +numpy==1.23.4 |
| 166 | +pandas==1.5.1 |
| 167 | +matplotlib==3.6.2 |
| 168 | +pillow==9.3.0 |
| 169 | +opencv-python-headless==4.6.0.66 |
| 170 | +streamlit==1.14.0 |
| 171 | +tqdm==4.64.1 |
| 172 | +seaborn==0.12.1 |
| 173 | +scipy==1.9.3 |
| 174 | +ipython==8.6.0 |
| 175 | +psutil==5.9.4 |
| 176 | +pyyaml==6.0 |
| 177 | +``` |
| 178 | + |
| 179 | +### Write the Dockerfile for the application |
| 180 | + |
| 181 | +Your Dockerfile should start with the `FROM` instruction indicating the parent image to use. In our case we choose to start from a `python:3.8` image: |
| 182 | + |
| 183 | +```console |
| 184 | +FROM python:3.8 |
| 185 | +``` |
| 186 | + |
| 187 | +Create the home directory and add your files to it: |
| 188 | + |
| 189 | +```console |
| 190 | +WORKDIR /workspace |
| 191 | +ADD . /workspace |
| 192 | +``` |
| 193 | + |
| 194 | +Install the `requirements.txt` file which contains your needed Python modules using a `pip install ...` command: |
| 195 | + |
| 196 | +```console |
| 197 | +RUN pip install -r requirements.txt |
| 198 | +``` |
| 199 | + |
| 200 | +Define your default launching command to start the application: |
| 201 | + |
| 202 | +```console |
| 203 | +CMD [ "streamlit" , "run" , "/workspace/main.py", "--server.address=0.0.0.0" ] |
| 204 | +``` |
| 205 | + |
| 206 | +Give correct access rights to **OVHcloud user** (`42420:42420`): |
| 207 | + |
| 208 | +```console |
| 209 | +RUN chown -R 42420:42420 /workspace |
| 210 | +ENV HOME=/workspace |
| 211 | +``` |
| 212 | + |
| 213 | +### Build the Docker image from the Dockerfile |
| 214 | + |
| 215 | +Launch the following command from the **Dockerfile** directory to build your application image: |
| 216 | + |
| 217 | +```console |
| 218 | +docker build . -t yolov7-streamlit-asl-recognition:latest |
| 219 | +``` |
| 220 | + |
| 221 | +> [!primary] |
| 222 | +> **Notes** |
| 223 | +> |
| 224 | +> - The dot `.` argument indicates that your build context (place of the **Dockerfile** and other needed files) is the current directory. |
| 225 | +> |
| 226 | +> - The `-t` argument allows you to choose the identifier to give to your image. Usually image identifiers are composed of a **name** and a **version tag** `<name>:<version>`. For this example we chose **yolov7-streamlit-asl-recognition:latest**. |
| 227 | +
|
| 228 | +### Test it locally (optional) |
| 229 | + |
| 230 | +Launch the following **Docker command** to launch your application locally on your computer: |
| 231 | + |
| 232 | +```console |
| 233 | +docker run --rm -it -p 8501:8051 --user=42420:42420 yolov7-streamlit-asl-recognition:latest |
| 234 | +``` |
| 235 | + |
| 236 | +> [!primary] |
| 237 | +> **Notes** |
| 238 | +> |
| 239 | +> - The `-p 8501:8501` argument indicates that you want to execute a port redirection from the port **8501** of your local machine into the port **8501** of the Docker container. The port **8501** is the default port used by **Streamlit** applications. |
| 240 | +> |
| 241 | +> - Don't forget the `--user=42420:42420` argument if you want to simulate the exact same behaviour that will occur on **AI Deploy apps**. It executes the Docker container as the specific OVHcloud user (user **42420:42420**). |
| 242 | +
|
| 243 | +Once started, your application should be available on `http://localhost:8501`. |
| 244 | + |
| 245 | +### Push the image into the shared registry |
| 246 | + |
| 247 | +> [!warning] |
| 248 | +> **Warning** |
| 249 | +> The shared registry of AI Deploy should only be used for testing purpose. Please consider attaching your own Docker registry. More information about this can be found [here](https://docs.ovh.com/de/publiccloud/ai/training/add-private-registry). |
| 250 | +
|
| 251 | +Find the address of your shared registry by launching this command: |
| 252 | + |
| 253 | +```console |
| 254 | +ovhai registry list |
| 255 | +``` |
| 256 | + |
| 257 | +Log in to the shared registry with your usual OpenStack credentials: |
| 258 | + |
| 259 | +```console |
| 260 | +docker login -u <user> -p <password> <shared-registry-address> |
| 261 | +``` |
| 262 | + |
| 263 | +Push the compiled image into the shared registry: |
| 264 | + |
| 265 | +```console |
| 266 | +docker tag yolov7-streamlit-asl-recognition:latest <shared-registry-address>/yolov7-streamlit-asl-recognition:latest |
| 267 | +docker push <shared-registry-address>/yolov7-streamlit-asl-recognition:latest |
| 268 | +``` |
| 269 | + |
| 270 | +### Launch the AI Deploy app |
| 271 | + |
| 272 | +The following command starts a new app running your Streamlit application: |
| 273 | + |
| 274 | +```console |
| 275 | +ovhai app run <shared-registry-address>/yolov7-streamlit-asl-recognition:latest \ |
| 276 | + --gpu 1 \ |
| 277 | + --default-http-port 8501 \ |
| 278 | + --volume asl-volov7-model@GRA/:/workspace/asl-volov7-model:RO |
| 279 | +``` |
| 280 | + |
| 281 | +> [!primary] |
| 282 | +> **Notes** |
| 283 | +> |
| 284 | +> - `--default-http-port 8501` indicates that the port to reach on the app URL is `8501`. |
| 285 | +> |
| 286 | +> - `--gpu 1` indicates that we request 4 CPUs for that app. |
| 287 | +> |
| 288 | +> - Consider adding the `--unsecure-http` attribute if you want your application to be reachable without any authentication. |
| 289 | +
|
| 290 | +## Go further |
| 291 | + |
| 292 | +- You can imagine deploying an app using YOLO models with another Python framework: **Flask**. Refer to this [tutorial](https://docs.ovh.com/de/publiccloud/ai/deploy/web-service-yolov5/). |
| 293 | +- Feel free to use **Streamlit** for other AI tasks! Deploy a Speech-to-Text app [here](https://docs.ovh.com/de/publiccloud/ai/deploy/tuto-streamlit-speech-to-text-app/). |
0 commit comments