-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b5caea
commit 5ed0d5b
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from keras.models import load_model | ||
import cv2 | ||
import numpy as np | ||
from random import choice | ||
|
||
REV_CLASS_MAP = { | ||
0: "rock", | ||
1: "paper", | ||
2: "scissors", | ||
3: "none" | ||
} | ||
|
||
|
||
def mapper(val): | ||
return REV_CLASS_MAP[val] | ||
|
||
|
||
def calculate_winner(move1, move2): | ||
if move1 == move2: | ||
return "Tie" | ||
|
||
if move1 == "rock": | ||
if move2 == "scissors": | ||
return "User" | ||
if move2 == "paper": | ||
return "Computer" | ||
|
||
if move1 == "paper": | ||
if move2 == "rock": | ||
return "User" | ||
if move2 == "scissors": | ||
return "Computer" | ||
|
||
if move1 == "scissors": | ||
if move2 == "paper": | ||
return "User" | ||
if move2 == "rock": | ||
return "Computer" | ||
|
||
|
||
model = load_model("rock-paper-scissors-trained.h5") | ||
|
||
cap = cv2.VideoCapture(0) | ||
|
||
prev_move = None | ||
|
||
while True: | ||
ret, frame = cap.read() | ||
if not ret: | ||
continue | ||
|
||
# rectangle for user to play | ||
cv2.rectangle(frame, (100, 100), (500, 500), (255, 255, 255), 2) | ||
# rectangle for computer to play | ||
cv2.rectangle(frame, (800, 100), (1200, 500), (255, 255, 255), 2) | ||
|
||
# extract the region of image within the user rectangle | ||
roi = frame[100:500, 100:500] | ||
img = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) | ||
img = cv2.resize(img, (155, 155)) | ||
|
||
# predict the move made | ||
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) | ||
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) | ||
else: | ||
computer_move_name = "none" | ||
winner = "Waiting..." | ||
prev_move = user_move_name | ||
|
||
# display the information | ||
font = cv2.FONT_HERSHEY_SIMPLEX | ||
cv2.putText(frame, "Your Move: " + user_move_name, | ||
(50, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA) | ||
cv2.putText(frame, "Computer's Move: " + computer_move_name, | ||
(750, 50), font, 1.2, (255, 255, 255), 2, cv2.LINE_AA) | ||
cv2.putText(frame, "Winner: " + winner, | ||
(400, 600), font, 2, (0, 0, 255), 4, cv2.LINE_AA) | ||
|
||
if computer_move_name != "none": | ||
icon = cv2.imread( | ||
"images/{}.png".format(computer_move_name)) | ||
icon = cv2.resize(icon, (380, 480)) | ||
#frame[100:500, 800:1200] = icon | ||
|
||
cv2.imshow("Rock Paper Scissors", frame) | ||
|
||
k = cv2.waitKey(10) | ||
if k == ord('q'): | ||
break | ||
|
||
cap.release() | ||
cv2.destroyAllWindows() |