Rock Paper Scissors With Open CV

by just_another_person in Circuits > Computers

642 Views, 8 Favorites, 0 Comments

Rock Paper Scissors With Open CV

Screenshot 2024-02-20 111529.png

This project uses OpenCV to leverage the power of computer vision, allowing users to play the classic game of Rock, Paper, Scissors against a computer opponent. Using the OpenCV library, this project employs image processing techniques to capture and analyze hand gestures in real-time. Users can interact with the system's webcam by making gestures with their hands, and the computer will respond with its own randomly generated choice, and then processes the data to decide a winner.

In this tutorial I'll go in over how each part of the code works so it's pretty beginner friendly, but the full code is also linked at the bottom for reference.

Supplies

This program is run using IDLE for Python, as well as Open CV for gesture detection. You also need to have a camera on your computer to collect the video data.

Imports & Defining Variables

Screenshot 2024-02-19 235624.png
Screenshot 2024-02-19 233704.png

Imports--

cv2-- what we use for computer vision in this program.

mediapipe-- we use Mediapipe's pre-trained Hand Landmarker model to detect different landmarks on hand.

counter-- will be used later in the code to count the number of times a specific loop should repeat. On line 42, we initially set the value of the counter as 0.

random-- used by the computer to randomly generate a result for rock, paper, or scissors

time & sleep-- sleep is used to delay the execution of part of the program for a given amount of seconds


my_list is used to give a list of possible outputs for the computer, one of which will later be generated randomly.

h & w are the dimensions of the frame

the tip & mid values are based on the values assigned to each joint of the hand; in this case we are using the tips of the fingers and comparing them to the middle joint (image attached above)

def findnameoflandmark-- finds the name of each landmark on the hand using Hand Landmarker 

Live Feedback Loop

Screenshot 2024-02-20 000229.png

By creating a 'while true' loop, we are able to continuously rely live feedback to the computer, and use that to track hand gestures. Each game, the computer will generate a new randomized gesture based on the values previously given in my_list.

After that, using the data collected from the video, the computer is able to get each landmark's position as a (x,y) coordinate.


Detecting Gestures

Screenshot 2024-02-20 001112.png

Using Mediapipe's Hand Landmarker, we are able to divide the hand into 21 different landmarks (image attached on step 1), which we can then reference to determine certain symbols and gestures. To detect the specific gestures we are using in this code, we compare the position of the middle joint of the finger relative to the tip. Since the point (0,0) is at the top left, lower values are oriented towards the top of the screen, whereas higher values correspond to the bottom. Using this, we can figure out if a finger is up (indicated by '1') or not ('0') if the tip has a lower y value than the middle joint.

Applying Gestures to Game

Screenshot 2024-02-20 001914.png
Screenshot 2024-02-20 002021.png
Screenshot 2024-02-20 002631.png
Screenshot 2024-02-20 002700.png
Screenshot 2024-02-20 002719.png

Now that we've determined when fingers are raised, all we need to do is apply it to the game symbols!

in the 'if' conditionals, basically all we're doing is determining what fingers need to be raised to meet the criteria for each gesture. For paper, it's all four fingers; for scissors, it's the index and middle; and for rock it's none of them. In addition to that, you need to take into account the computer's result, and for each possible combination create print statements to display the results of the round (feel free to modify these).

At the end of each 'if' statement, note that we add the statement 'counter=counter+1'. Initially, we set the counter value as 0, but if it detects a hand gesture then it changes to one, deferring it to the last 'if' statement where we implement a sleep statement to delay the code. By doing this, we create a break of 10 seconds before the next round of the game starts and the camera begins detection again.

Modifications!!! (+ Link to Code)

Ok now that the basic code is done feel free to add modifications to change up the game using some of the modifications I've listed below, or add your own unique twist to it!

  • Add a sound to determine if you won or lost the game (using pygame)
  • Add another counter to keep track of the wins/losses
  • Use different hand gestures for each symbol

Code for this project is linked below:

https://github.com/just-another-person1/Projects-/blob/3df7e79dec5b34184bc654737672703dcc49315d/Rock%20Paper%20Scissors%20code%20using%20Open%20CV%20FINAL