A game of Rock Paper Scissors in which the user plays against the computer. The program accesses the webcam and uses a Keras model to detect whether the user is showing rock, paper or scissors to the camera.
The model is created using Teachable-Machine. Here, I created four classes, Rock, Paper, Scissors and Nothing. To train each class, I provided images of myself showing each option to the camera.
While the webcam is active, the model calculates a prediction of what the user is showing to the camera. This prediction will be the user input for the game.
The model was tested for accuracy and retrained with more images as required.
Tensorflow, Opencv-python and ipykernel are required for this game. I set up a new conda virtual environment, using python 3.8 to avoid known issues between Tensorflow and the latest version of python.
The play_game function is simple.
It initiates an instance of the RockPaperScissors class and enters the while loop. The webcam captures and shows the video on the screen and the game plays until the user presses 'q' at the end of three rounds.
The flowchart below shows the logic inside the while loop.
The model provides its prediction in a nested list, e.g. [[1.0838162e-07 2.2813882e-13 1.3565130e-09 9.9999988e-01]], with the highest value being the class (rock, paper, scissors or nothing) it thinks the user is showing. The get_prediction method extracts the highest value from the list using the Numpy method, argmax.
After coding the basic logic of the game, I added a few extra features to improve the user experience:
- using the cv2.putText() method so the messages were printed onto the video frame, not in the terminal.
- adding a countdown to make it clearer when the user has to show their hand.
Further improvements that could be made are:
- further retraining of the model with different users and backgrounds to improve accuracy of model's predictions.
- adding images to visually represent the computer choice on the screen.
- instead of just giving the option to close the window at the end of 3 rounds, the user could press a different key to play again.