AI Deploy - Tutorial - Create an application to play rock paper scissors with YoloV8
Objective
The purpose of this tutorial is to explain how to deploy an application to play the game "rock paper scissors" using the YOLOv8 model.
In order to do this, you will use Streamlit, 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.
Requirements
- You have created an AI Deploy project inside a Public Cloud project.
- You have created a user for AI Deploy.
- You have Docker installed on your machine.
- You have knowledge about building images with Dockerfile.
- You have weights obtained from training the YOLOv8 model on the Rock Paper Scissors Dataset. You can use the tutorial Train YOLOv8 to play rock paper scissors to generate these weights.
Instructions
You are going to follow different steps to build your Streamlit application.
- More information about Streamlit capabilities can be found here.
- All source files can be found on GitHub.
Write the Streamlit application
Create a Python file named app.py and paste the following code:
from ultralytics import YOLO
import streamlit as st
#######################################################################################################################
## 🎯 The aim of this script is to create an Rock/Paper/Scissors application based on a trained model (from YOLOv8). ##
## 🏞 The uploaded snapshots are stored in /workspace/ ##
## 🧠 The train model is stored in /workspace/model/rock-paper-scissors/ ##
#######################################################################################################################
# Save uploaded photo
def save_photo(photo):
photoAbsolutePath = '/workspace/' + photo.name
with open(photoAbsolutePath,'wb') as f:
f.write(photo.getbuffer())
return photoAbsolutePath
# main
if __name__ == '__main__':
st.write("## Welcome on the 🪨 📄 ✂️ game!")
# 🧠 Load the model
model = YOLO('/workspace/model/rock-paper-scissors/best.torchscript')
# 📸 Camera input
img_file_buffer = st.camera_input("Take your picture in real time:")
if img_file_buffer is not None:
photoPath = save_photo(img_file_buffer)
# 🔎 Prediction
results = model.predict(photoPath, verbose=True, save=True, conf=0.5)
# 📈 Display results
for r in results:
for c in r.boxes.cls:
st.write(r.names[int(c)])
Write the requirements.txt file for the application
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.
ultralytics==8.0.194
opencv-python-headless==4.8.1.78
streamlit==1.27.2
Write the Dockerfile for the application
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:
# 🐳 Base image to execute Python application
FROM python:3.8
# 👱 Userspace for AI Deploy
WORKDIR /workspace
# 📚 Libraries for the application
ADD requirements.txt /workspace
RUN pip install -r requirements.txt
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
# 🐍 Python application
ADD app.py /workspace
# 👮 Rootless execution
RUN chown -R 42420:42420 /workspace
# 🚀 Run the Python application
CMD [ "streamlit" , "run" , "/workspace/app.py", "--server.address=0.0.0.0" ]
ENV HOME=/workspace
Build the Docker image from the Dockerfile
Launch one of the following commands from the Dockerfile directory to build your application image:
# Build the image using your machine's default architecture
docker build . -f Dockerfile -t <shared-regristry-name>/rock-paper-scissors-app:1.0.0
# Build image targeting the linux/amd64 architecture
docker buildx build --platform linux/amd64 -f Dockerfile -t <shared-regristry-name>/rock-paper-scissors-app:1.0.0 .
-
The first command builds the image using your system’s default architecture. This may work if your machine already uses the
linux/amd64architecture, which is required to run containers with our AI products. However, on systems with a different architecture (e.g.ARM64onApple Silicon), the resulting image will not be compatible and cannot be deployed. -
The second command explicitly targets the
linux/AMD64architecture to ensure compatibility with our AI services. This requiresbuildx, which is not installed by default. If you haven’t usedbuildxbefore, you can install it by running:docker buildx install
Push the image into the shared registry
Warning 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. The images pushed to this registry are for AI Tools workloads only, and will not be accessible for external uses.
Find the address of your shared registry by launching this command:
ovhai registry list
Log in to the shared registry with your usual AI Platform user credentials:
docker login -u <user> -p <password> <shared-registry-address>
Push the compiled image into the shared registry:
docker push <shared-registry-address>/rock-paper-scissors-app:1.0.0
Launch the AI Deploy app
The following command starts a new app running your Streamlit application:
ovhai app run \
--name rock-paper-scissors-app \
--cpu 1 \
--default-http-port 8501 \
--volume rock-paper-scissors-model@GRA:/workspace/model:RO:cache \
--unsecure-http \
<shared-registry-address>/rock-paper-scissors-app:1.0.0
Notes
-
--default-http-port 8501indicates that the port to reach on the app URL is8501. -
--cpu 1indicates that we request only one CPU for that app. -
Consider adding the
--unsecure-httpattribute if you want your application to be reachable without any authentication.
Go further
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.
Feedback
Please send us your questions, feedback and suggestions to improve the service:
- On the OVHcloud Discord server