Play rock-paper-scissors against the computer using real hand gestures.
Built and tested by Shahan Fathima K · Industrial Robotics Institute
Python 3.11, OpenCV, MediaPipe
The classic game played with your hand in front of a webcam. MediaPipe reads your fingers to recognise Rock (fist), Paper (open hand) or Scissors (two fingers). After a 3-2-1 countdown the computer picks at random, the winner is shown, and scores are kept over 10 rounds.
| Component | Qty |
|---|---|
| Laptop / PC | 1 |
| Webcam | 1 |
Complete, tested Python code for this project. Copy it into Python 3.11 and upload it and run it.
import cv2
import mediapipe as mp
import random
import time
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands(max_num_hands=1, min_detection_confidence=0.7)
mpDraw = mp.solutions.drawing_utils
choices = ["Rock", "Paper", "Scissors"]
score_user = 0
score_computer = 0
rounds = 10
game_state = "COUNTDOWN"
start_time = time.time()
computer_choice = ""
user_choice = ""
result_text = ""
gesture_locked = False
display_text = ""
def get_gesture(lm):
fingers = [1 if lm[4].x < lm[3].x else 0] # thumb
for tip, pip in [(8, 6), (12, 10), (16, 14), (20, 18)]:
fingers.append(1 if lm[tip].y < lm[pip].y else 0)
if sum(fingers) == 0:
return "Rock"
elif sum(fingers) == 5:
return "Paper"
elif fingers[1] == 1 and fingers[2] == 1 and fingers[3] == 0:
return "Scissors"
return None
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
result = hands.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
detected_choice = None
if result.multi_hand_landmarks:
for handLms in result.multi_hand_landmarks:
mpDraw.draw_landmarks(frame, handLms, mpHands.HAND_CONNECTIONS)
detected_choice = get_gesture(handLms.landmark)
now = time.time()
if rounds > 0:
if game_state == "COUNTDOWN":
elapsed = now - start_time
if elapsed < 1:
display_text = "3"
elif elapsed < 2:
display_text = "2"
elif elapsed < 3:
display_text = "1"
else:
display_text = "SHOW!"
if detected_choice and not gesture_locked:
user_choice = detected_choice
computer_choice = random.choice(choices)
if user_choice == computer_choice:
result_text = "Draw"
elif (user_choice, computer_choice) in [("Rock", "Scissors"), ("Paper", "Rock"), ("Scissors", "Paper")]:
result_text = "You Win"
score_user += 1
else:
result_text = "Computer Wins"
score_computer += 1
rounds -= 1
game_state = "RESULT"
start_time = now
gesture_locked = True
elif game_state == "RESULT":
display_text = f"Result: {result_text}"
if now - start_time > 2:
game_state = "COUNTDOWN"
start_time = now
gesture_locked = False
else:
display_text = "GAME OVER"
cv2.putText(frame, f"Rounds Left: {rounds}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
cv2.putText(frame, f"You: {score_user} Computer: {score_computer}", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 255), 2)
cv2.putText(frame, display_text, (20, 150), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3)
if game_state == "RESULT":
cv2.putText(frame, f"You: {user_choice}", (20, 220), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.putText(frame, f"Computer: {computer_choice}", (20, 270), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow("Rock Paper Scissors AI", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()The classic game played with your hand in front of a webcam. MediaPipe reads your fingers to recognise Rock (fist), Paper (open hand) or Scissors (two fingers).
You need: Laptop / PC (1), Webcam (1). Software: Python 3.11, OpenCV, MediaPipe.
Yes. It is rated beginner and takes about 1–2 hours to build, using the Laptop / PC with webcam. The guide on this page explains every connection and step.
The complete, tested Python code is free on this page. Copy it into Python 3.11 and upload it and run it.
No kit is needed. This project runs on a laptop or PC with webcam. For help with the setup, message IRI on WhatsApp at +91 86060 82634.