# Build an ML Rock Paper Scissors Game with Lobe

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/101/120/medium800thumb/camera_Rock_Paper_Scissors_game_on_Pi.jpg?1617134453)

Classify hand gestures to play Rock, Paper, Scissors on your Pi using computer vision and machine learning! Lobe is a free, easy to use app that lets you train custom machine learning (ML) models without any code. [Download Lobe here](https://lobe.ai/).

In this tutorial, we'll build a Rock, Paper, Scissors ML model and play against our Pi. The game will classify your hand gesture using the Pi camera, let the computer randomly pick one, and determine who the winner is.

This is a fun introduction to building interactive ML models with Lobe on the Pi. Use this as a starting point to explore your own ideas!

This tutorial is part of a series which includes:

- [Tutorial 1: Machine Learning 101 with Lobe and Raspberry Pi](https://learn.adafruit.com/lobe-braincraft)
- [Tutorial 3: Build an ML Package Detector with Lobe](https://learn.adafruit.com/build-an-ml-package-detector)

## Background Knowledge

New to using Lobe? Check out our introductory tutorial:

- [Tutorial 1: Machine Learning 101 with Lobe and Raspberry Pi](https://learn.adafruit.com/lobe-braincraft)

We also recommend that you have familiarity with the following topics:

1. Setting up and using the Raspberry Pi
2. Some experience terminal window
3. Some experience with Python

# Build an ML Rock Paper Scissors Game with Lobe

## Required Parts

# Build an ML Rock Paper Scissors Game with Lobe

## Set up your Pi

## Hardware Setup

Before starting, set up your Pi and BrainCraft HAT. Follow [this primary guide for the BrainCraft](https://learn.adafruit.com/lobe-braincraft/raspberry-pi-setup) to configure these services:

- Blinka
- Fan Service
- Display Module

## Installing Lobe SDK

Connect to your Pi using SSH and run the following script to install the Lobe SDK:

```none
cd ~
wget https://raw.githubusercontent.com/lobe/lobe-python/master/scripts/lobe-rpi-install.sh
sudo bash lobe-rpi-install.sh
```

_Note: To setup SSH, follow [this guide](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh)._

# Build an ML Rock Paper Scissors Game with Lobe

## Train a Gesture Detector

The first part of our Rock, Paper, Scissors game is to train an ML model that can classify the hand gesture you make.

We'll use Lobe to collect images to create a dataset and train the model.

## Collect your images in Lobe

First, download and install Lobe from the link below:

[Download Lobe](https://lobe.ai/)
![](https://cdn-learn.adafruit.com/assets/assets/000/101/101/medium800/camera_1._Lobe_New_Project.png?1617128423)

Open Lobe and create a new project.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/102/medium800/camera_1._empty_project.png?1617128435)

From the top right, select _ **Import** _ and choose _ **C**** amera**_ in the drop-down menu.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/104/medium800/camera_3._Rock.png?1617128830)

In the bottom left corner, set your first label to " **Rock**" and take **at least 10 pictures** of your fist at different angles.

_Hint: If you press and hold the capture button you can take a burst of images in Lobe._

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/101/105/medium800/camera_4._Paper.png?1617128882)

![](https://cdn-learn.adafruit.com/assets/assets/000/101/106/medium800/camera_6._All_images.png?1617128909)

Repeat this step for the " **Paper**" and " **Scissors**" hand gestures.

## Test your model
In Lobe, switch to the _ **Use** _ tab&nbsp;in the left menu and select _ **Camera** _ on&nbsp;the top.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/107/medium800/camera_8._Test_Scissors.png?1617128963)

Test all three hand gestures and check that the model is recognizing them accurately. If the model is not working as expected, you can use the green and red buttons to improve the model.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/108/medium800thumb/camera_Use_Correct_Incorrect_Example.jpg?1617129074)

Click the **green button** when your model predicts the correct label. This will add the image with the correct label to your dataset.

Click the **red button** when your model predicts the wrong label. You can then provide the correct label and the image will get added to your dataset.&nbsp;

If you find that one of the gestures is consistently confusing the model, try collecting more images of that gesture.

# Build an ML Rock Paper Scissors Game with Lobe

## Export your Model

Next, export your Lobe model to use on the Raspberry Pi. We'll use **TensorFlow Lite** which is a format that is optimized for mobile and edge devices, like the Pi.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/101/098/medium800/adafruit_products_6._Export.png?1617128073)

In Lobe, navigate to the Use tab and click Export.

Select TensorFlow Lite and select a location to save the model. We'll transfer the model to our Raspberry Pi later in the tutorial.

# Build an ML Rock Paper Scissors Game with Lobe

## Set up an FTP Connection

Since we're using the Pi in a headless configuration, we'll use an FTP connection to transfer files between our computer and the Pi.

## Windows Instructions

Download and install [WinSCP](https://winscp.net/eng/index.php)

&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/099/376/medium800/adafruit_products_winscp-new-session.png?1613066405)

Open WinSCP and start a New Session

![](https://cdn-learn.adafruit.com/assets/assets/000/099/377/medium800/adafruit_products_winscp-new-session-details.png?1613066416)

Select an SFTP connection, fill in the IP address of your Pi, set the username to Pi, and put in your password.

Info: 

## Mac Instructions
Download and install [FileZilla](https://filezilla-project.org/download.php?type=client). When it's done installing, open the program.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/090/medium800/adafruit_products_FileZilla_config.png?1617126693)

Type `sftp://` followed by the IP address of your Pi. Set the username to `pi` and put in your password.

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/101/091/medium800/adafruit_products_FileZilla_connected.png?1617126721)

# Build an ML Rock Paper Scissors Game with Lobe

## Download the Code

Download the GitHub repo into the Pi's home folder with the following commands:

```none
cd ~
git clone https://github.com/lobe/lobe-adafruit-kit.git
```

The GitHub repo can also be found [here](https://github.com/lobe/lobe-adafruit-kit).

# Build an ML Rock Paper Scissors Game with Lobe

## Test your Model on the Pi

## Transfer the model to the Pi

Connect your Pi to a power source.

Open command prompt on a PC or terminal on Mac/Linux. Connect to your Pi using SSH.

The command will look like the following, replacing the numbers after the @ sign with the IP address of your Pi:

```none
ssh pi@192.168.0.22
```

Info: 

Create a new folder in the home directory and name it `model` by using the following commands:

```none
cd ~
mkdir model
```

![](https://cdn-learn.adafruit.com/assets/assets/000/100/955/medium800/camera_move_model.png?1616616925)

Open the FTP connection and use it to copy `saved_model.tflite` and&nbsp;`signature.json` from your exported Lobe model into the `model` directory on the Pi.

_Follow the same procedure for FileZilla._

## Test your model
Use the commands below to download and run the basic Lobe classifier on your Pi:

```none
cd ~
cd lobe-adafruit-kit
python3 lobe-basic-prediction.py
```

![](https://cdn-learn.adafruit.com/assets/assets/000/101/110/medium800thumb/camera_Rock_Paper_Scissors_on_Pi.jpg?1617129438)

Make sure that all the Rock, Paper, Scissors hand gestures are working as expected. It's best to keep the camera background consistent with the images you used for training.

# Build an ML Rock Paper Scissors Game with Lobe

## Play Rock, Paper, Scissors

Now that your model is trained and tested we can integrate it with a game of Rock, Paper, Scissors! The game demonstrates how to use the predicted label from the model to trigger events.

First, navigate to the Lobe Adafruit Kit folder:

```python
cd ~
cd lobe-adafruit-kit
```

Run the Python program with the following command:

```none
python3 lobe-rock-paper-scissors.py
```

## Play the Game!
![](https://cdn-learn.adafruit.com/assets/assets/000/101/114/medium800thumb/camera_Rock_Paper_Scissors_game_on_Pi.jpg?1617130891)

Click the BrainCraft button. This starts a countdown -- make your hand gesture and the Pi will take a photo at the end of the countdown.

The screen displays the Pi's choice and then displays a winner!

# Build an ML Rock Paper Scissors Game with Lobe

## Game Code Overview

This page dives deeper into how the Rock, Paper, Scissors game code works.

```python
signs = ['Rock', 'Paper', 'Scissors']

game_logic = {'Rock' : 'Paper',
	      'Paper' : 'Scissors',
	      'Scissors' : 'Rock'}
```

First, we define the three hand gestures that the player and computer can play.

Next, we define the game logic using a dictionary. The dictionary value on the right beats the dictionary key on the left. For example, the dictionary key 'Rock' is beaten by the dictionary value 'Paper'.&nbsp;

```python
def load_image(path, camera) -> Image:
	img = Image.open(path)

	pad = Image.new('RGB', (
		((img.size[0] + 31) // 32) * 32,
		((img.size[1] + 15) // 16) * 16,
		))

	pad.paste(img, (0, 0))
	
	layer = camera.add_overlay(pad.tobytes(), size=img.size)

	return layer
```

Next, we include a function to load images as layers. We use these images for the countdown and to show the hand signs that the computer chooses. The function scales the images to proper size to fit on the screen.

```python
def random_sign():
	return random.choice(signs)

def compare_signs(player_sign, computer_sign):
	if (game_logic[player_sign] == computer_sign):
		print('computer wins')
		return 
	elif (game_logic[computer_sign] == player_sign):
		print('you win')
		return
	else:
		print('tie')
		return
```

The first function above uses `random.choice` to return a random element from the list of `signs`.

The second function compares the sign detected by the Lobe model with the sign the Raspberry Pi chose randomly. It checks whether the sign the player chose loses to the sign the computer chose.

For example, if the player chooses `Rock`, then `game_logic[player_sign]` will equal `Paper` since we're getting the element that is at `game_logic` index `Rock`.

## Main program code
```python
model = ImageModel.load('~/model')
```

`ImageModel` is a class from the Lobe library. We load the Lobe model and create an instance of the class.

```python
with picamera.PiCamera(resolution=(224, 224), framerate=30) as camera:
```

Next, we instantiate the Pi Camera. The rest of the code runs inside this indent block.

```python
stream = io.BytesIO()
camera.start_preview()

time.sleep(2)
```

We create a stream to continuously show the camera footage, then start the preview on the camera to populate the stream, and wait 2 seconds to let the camera warm up.

```python
rock = load_image('assets/rock.png', camera)
paper = load_image('assets/paper.png', camera)
scissor = load_image('assets/scissor.png', camera)
counter_one = load_image('assets/one.png', camera)
counter_two = load_image('assets/two.png', camera)
counter_three = load_image('assets/three.png', camera)
```

The above code loads all the images we use in the game. Each image is loaded into a separate layer.

### Main loop

The section below covers the main program loop.

```python
stream.seek(0)
```

The above code returns the stream to the first byte.

```python
inputs = get_inputs()
while Input.BUTTON not in inputs:
	inputs = get_inputs()
	time.sleep(0.1)
```

Next, we wait for the button to be pressed before starting the game.

```python
camera.preview.alpha = 0
counter_one.layer = 3
time.sleep(1)

counter_one.layer = 0
counter_two.layer = 3
time.sleep(1)

counter_two.layer = 0
counter_three.layer = 3
time.sleep(1)

counter_three.layer = 0
camera.preview.alpha = 255
time.sleep(1)
```

Once the button is pressed, we turn the opacity of the camera preview to 0 so it's transparent. Then we move each number of the countdown to the front and wait 1 second between each one. Finally we turn the camera opacity back to 255 (opaque).

```python
camera.capture(stream, format='jpeg')
img = Image.open(stream)

result = model.predict(img)

label = result.prediction

camera.annotate_text = label
time.sleep(0.5)
```

The next step is to capture an image from the camera stream and open it as a Pillow image. The Lobe model uses inferencing to make a prediction on the image, and returns the predicted label. This is added to the camera preview.

```python
computer_sign = random_sign()

if (computer_sign == 'Rock'):
	Rock.layer = 3
elif (computer_sign == 'Paper'):
	Paper.layer = 3
elif (computer_sign == 'Scissors'):
	Scissors.layer = 3

time.sleep(2)

Rock.layer = 0
Paper.layer = 0
Scissors.layer = 0
```

The above code is how the Pi plays the game! The Pi generates a random sign, checks which sign was generated, and shows the corresponding image for 2 seconds.

```python
winner = compare_signs(label, computer_sign)

if (winner == 'player'):
	camera.annotate_text = 'You Win!'
elif (winner == 'computer'):
	camera.annotate_text = 'You Lose...'
elif (winner == 'tie'):
	camera.annotate_text = 'Tie'
```

Finally, we compare the player gesture (`label` from the prediction) and the computer gesture, check who the winner was and add the text to the camera preview.

# Build an ML Rock Paper Scissors Game with Lobe

## Going Forward

![](https://cdn-learn.adafruit.com/assets/assets/000/101/146/medium800thumb/camera_Rock_Paper_Scissors_game_on_Pi.jpg?1617160077)

Now that you've built a Rock, Paper, Scissors game for yourself, try making it more accessible for other folks!

The model is only as good as the data you give it. To make this model work for more people, collect images of many different hands. If you can train with hands that are feminine and masculine, old and young, small and large, and with different skin tones, you'll end up with a better model that will work for more people.

You can also use this project as a template to do other types of gesture applications and trigger different actions with the BrainCraft.

To learn how to update your Lobe model with images taken from the Pi Camera, check out this tutorial:

- [Tutorial 3: Build an ML Package Detector with Lobe](https://learn.adafruit.com/build-an-ml-package-detector)

Happy making!


## Related Guides

- [Raspberry Pi Care and Troubleshooting](https://learn.adafruit.com/raspberry-pi-care-and-troubleshooting.md)
- [Adafruit BrainCraft HAT - Easy Machine Learning for Raspberry Pi](https://learn.adafruit.com/adafruit-braincraft-hat-easy-machine-learning-for-raspberry-pi.md)
- [Machine Learning 101 with Lobe and BrainCraft](https://learn.adafruit.com/machine-learning-101-lobe-braincraft.md)
- [Set up Home Assistant with a Raspberry Pi](https://learn.adafruit.com/set-up-home-assistant-with-a-raspberry-pi.md)
- [Raspberry Pi Video Synth with Blinka and Processing](https://learn.adafruit.com/raspberry-pi-video-synth-with-blinka-and-processing.md)
- [Raspberry Pi E-Ink Weather Station using Python](https://learn.adafruit.com/raspberry-pi-e-ink-weather-station-using-python.md)
- [iNTERCEPT + SDR for Signal Monitoring on Raspberry Pi](https://learn.adafruit.com/intercept-and-sdr-for-monitoring-signals-on-raspberry-pi.md)
- [Python Edge Speech Recognition with Voice2JSON](https://learn.adafruit.com/edge-speech-recognition-with-voice2json.md)
- [Running TensorFlow Lite Object Recognition on the Raspberry Pi 4 or Pi 5](https://learn.adafruit.com/running-tensorflow-lite-on-the-raspberry-pi-4.md)
- [Visual Studio Code for the Raspberry Pi](https://learn.adafruit.com/visual-studio-code-for-the-raspberry-pi.md)
- [diy lofi hip hop raspberry pi radio](https://learn.adafruit.com/lofi-hip-hop-raspberry-pi-radio-braincraft.md)
- [Raspberry Pi Low-Light Long-Exposure Photography](https://learn.adafruit.com/raspberry-pi-hq-camera-low-light-long-exposure-photography.md)
- [Raspberry Pi E-Ink Event Calendar using Python](https://learn.adafruit.com/raspberry-pi-e-ink-desk-calendar-using-python.md)
- [Video Feedback with Raspberry Pi](https://learn.adafruit.com/video-feedback-with-raspberry-pi.md)
- [Programming with Scratch 2 or 3 on Raspberry Pi](https://learn.adafruit.com/programming-with-scratch-on-raspberry-pi.md)
- [Using Google Assistant on the BrainCraft HAT or Voice Bonnet](https://learn.adafruit.com/using-google-assistant-on-the-braincraft-hat.md)
- [BrainCraft Camera Case](https://learn.adafruit.com/braincraft-camera-case.md)
