Skip to content

Commit

Permalink
adding new comments
Browse files Browse the repository at this point in the history
  • Loading branch information
namannarula committed May 23, 2021
1 parent fe7ba60 commit 039fcf4
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import numpy as np
from random import choice

# Path where the trained model is stored
MODEL_PATH = "rock-paper-scissors-trained.h5"

# Defining class map for each label
CLASS_MAP = {
0: "rock",
1: "paper",
2: "scissors",
}


def mapper(val):
return CLASS_MAP[val]


# Function to calculate winner
def calculate_winner(move1, move2):
if move1 == move2:
return "Tie"
Expand All @@ -36,16 +38,16 @@ def calculate_winner(move1, move2):
if move2 == "rock":
return "Computer"

# Loading the previously trained model
model = load_model(MODEL_PATH)

model = load_model("rock-paper-scissors-trained.h5")

# Starting the video capture
cap = cv2.VideoCapture(0)

prev_move = None

#Drawing 2 Rectangle boxes in the camera frame
while True:
ret, frame = cap.read()
cv2.resize(frame, (1280, 720))
if not ret:
continue
# rectangle for computer to play
Expand All @@ -54,25 +56,25 @@ def calculate_winner(move1, move2):
cv2.rectangle(frame, (50, 50), (350, 350), (255, 255, 255), 2)


# extract the region of image within the user rectangle
# specifing to only detect the hand inside the rectangle
roi = frame[50:350, 50:350]
img = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (155, 155))

# predict the move made
# predicting the move
pred = model.predict(np.array([img]))
print(pred[0])
move_code = np.argmax(pred[0])
user_move_name = mapper(move_code)

# predict the winner (human vs computer)
# deciding the winner
if prev_move != user_move_name:
if user_move_name != "none":
computer_move_name = choice(['rock', 'paper', 'scissors'])
winner = calculate_winner(user_move_name, computer_move_name)
prev_move = user_move_name

# display the information
# Text & Formatting
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "Naman's Move: " + user_move_name,
(25, 25), font, 0.6, (255, 255, 255), 2, cv2.LINE_AA)
Expand All @@ -81,6 +83,7 @@ def calculate_winner(move1, move2):
cv2.putText(frame, "Winner: " + winner,
(150, 410), font, 1.5, (0, 0, 255), 4, cv2.LINE_AA)

#displaying the computer view
if computer_move_name != "none":
icon = cv2.imread(
"images/{}.png".format(computer_move_name))
Expand All @@ -89,8 +92,9 @@ def calculate_winner(move1, move2):

cv2.imshow("Rock Paper Scissors", frame)

k = cv2.waitKey(10)
if k == ord('q'):
#Killing the game if the "Q" key is pressed
killKey = cv2.waitKey(10)
if killKey == ord('q'):
break

cap.release()
Expand Down

0 comments on commit 039fcf4

Please sign in to comment.