{"id":20766641,"url":"https://github.com/elijah-1994/computer-vision-rock-paper-scissors","last_synced_at":"2026-04-10T11:01:26.911Z","repository":{"id":61752690,"uuid":"543075767","full_name":"Elijah-1994/computer-vision-rock-paper-scissors","owner":"Elijah-1994","description":"Training a computer vision model using TensorFlow to detect whether Rock, Paper or Scissors is shown to the camera in real- time and with a high accuracy.","archived":false,"fork":false,"pushed_at":"2022-12-03T13:00:45.000Z","size":4265,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-25T23:37:38.957Z","etag":null,"topics":["machine-learning","opencv","python3","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Elijah-1994.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-29T11:07:42.000Z","updated_at":"2023-02-06T14:41:52.000Z","dependencies_parsed_at":"2023-01-24T00:45:11.078Z","dependency_job_id":null,"html_url":"https://github.com/Elijah-1994/computer-vision-rock-paper-scissors","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Elijah-1994/computer-vision-rock-paper-scissors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elijah-1994%2Fcomputer-vision-rock-paper-scissors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elijah-1994%2Fcomputer-vision-rock-paper-scissors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elijah-1994%2Fcomputer-vision-rock-paper-scissors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elijah-1994%2Fcomputer-vision-rock-paper-scissors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Elijah-1994","download_url":"https://codeload.github.com/Elijah-1994/computer-vision-rock-paper-scissors/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elijah-1994%2Fcomputer-vision-rock-paper-scissors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31639524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-10T07:40:12.752Z","status":"ssl_error","status_checked_at":"2026-04-10T07:40:11.664Z","response_time":98,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["machine-learning","opencv","python3","tensorflow"],"created_at":"2024-11-17T11:25:13.531Z","updated_at":"2026-04-10T11:01:26.893Z","avatar_url":"https://github.com/Elijah-1994.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Computer Vision Rock-Paper-Scissors\n\u0026nbsp;\n\nThe aim of this project is to use keras modelling and python methods\nto create the classic rock paper scissors game. Keras modelling is used to define the user input.\n\n\u0026nbsp;\n\n## Milestone 1 - Create the computer vision system \n\u0026nbsp;\n\nThe first step is to go onto [Teachable-Machine](https://teachablemachine.withgoogle.com/) to create the model. This consisted of creating four classes which represent Rock,Paper,Scissors and Nothing. I placed my hand to the camera to represent the first three classes and for nothing the camera was placed to the wall. For each class roughly 1000 images were taken to build the model.\n\nOnce all the images were taken the model was trained and downloaded from the \"Tensorflow\" tab. the model is labeled as \"keras_model.h5\" and contains a \"labels.txt\" file which contains the classes with the corresponding index. The model is used to take in the user input.\n\n![](Images/Milestone_1.PNG)\n*Figure 1 - Training model*\n\n\u0026nbsp;\n## Milestone 2 - Setting up the virtual environment\n\u0026nbsp;\n\nIn order to run the model a virtual environment with the correct installed libaries needs to be set up. The required libraries installed are opencv-python, tensorflow and ipykernel.\n\nThe conda create -n command is inputted into the bash terminal on VSCode to create the virtual environment and , conda pip install(pip install) commands are inputted to install the required libraries and its dependencies in the virtual environment.\n\npip list \u003e requirements.txt and conda env export \u003e env.yaml commands are inputted into the bash terminal to copy over the required libraries and dependencies. These file enables any other user to run the script on there computer but installing the dependencies.\n\nIn order to check if the virtual environment is correctly installed the script which contains the  main code for the model was run and worked fine.\n\n\u0026nbsp;\n\n## Milestone 3 - Create a Rock-Paper-Scissors game\n\n\u0026nbsp;\n\nThe object of this Milestone is to create a python script that will simulate a Rock-Paper-Scissors game that will ask for a user input and then compare the user input against the computers choice to show the winner.\n\n__ask_user_input function__\n\u0026nbsp;\n\nThe first step is to create a list which contains the options between rock,paper and scissors and the use the import random module to pick a random option in the list.\n\nA get_user choice functions is written which will take in the users choice. The user choice input is placed in a while loop which asks the user to enter either Rock,Paper or Scissors. If the user enters one of these options the users choice will be returned and stored as a string variable. The while loop will continue to run until the user input is correct.\n\n![](Images/Milestone_3%20-%20User_Input.PNG)\n\n*Figure 2 - get_user_choice function*\n\n\u0026nbsp;\n\n__ask_computer_input function__\n\n\u0026nbsp;\nA get_computer_choice function was written which will pick a random option from a list which contains strings labelled Rock,Paper,Scissors and return a choice and store as a string variable.\n\n![](Images/Milestone_3%20-%20computer_choice.PNG)\n\n*Figure 3 - get_computer_choice function*\n\n\u0026nbsp;\n\n__get winner function__\n\nA get_winner function was written which will contain if-elif-else statements to decide the winner between the user and winner based on the classic rules of rock,paper,scissors. The arguments passed through the parameters of the get_winner function are the variables stored from the get_user_choice function and get_computer_choice function.\n\n\u0026nbsp;\n![](Images/Milestone_3%20-%20Get_winner.PNG)\n\n*Figure 4 - get_winner function*\n\n\u0026nbsp;\n\n__play function__\n\nIn order to make the script more streamlined and efficient the \u0026ensp; __play function__\u0026ensp;  is coded and encapsulates the \u0026ensp; __get_user_choice function__\u0026ensp;  ,\u0026ensp; __get_computer_choice function__\u0026ensp;  and \u0026ensp;__get_winner function__\u0026ensp;.\n\n\n## Milestone 4 - Use the camera to play  Rock - Paper Scissors\n\n\u0026nbsp;\n\nIn order for the camera to work and take in the user inputs, the first step is to import  the \u0026ensp;__cV2 module__\u0026ensp; from the keras_model. This will allow the capture frame which will take in the user input to pop up during each run. The other required imports for the script are  \u0026ensp;__numpy as np__\u0026ensp; and \u0026ensp;__random__\u0026ensp;.\n\n__get_prediction method__\n\u0026nbsp;\nThe output of the keras model is a list of probabilities for each class (Rock,Paper,Scissors,Nothing). A list labelled choices which contains the four classes was created. in order for the highest probability to be chosen, the prediction is stored in a highest index variable which contained the np.argmax function. The highest probability is then picked and was stored in a user_choice variable. As each class corresponds to the corresponding value (e.g 0 - Rock) and he user_choice variable contains the choices list which is indexed based on the highest_index. This will convert high probability into the corresponding class.\n\u0026nbsp;\n\n![](Images/Milestone_4%20-%20get_prediction.PNG)\n\n*Figure 5 - get_prediction method*\n\n\u0026nbsp;\n\n__get_computer_choice method__\n\nthe \u0026ensp;__get_computer_choice method__\u0026ensp; returns the random computer choice using the \u0026ensp;__random.choice function__\u0026ensp; and is indexed between [0:3]. This is to ensure the computer choice does not choose \"Nothing\" in the choices list.\n\n\u0026nbsp;\n\n![](Images/Milestone_4%20-%20get_computer_choice.PNG)\n\n*Figure 6 - get_computer_choice method*\n\n__get_winner method__\n\nthe output from the \u0026ensp;__get_prediction method__\u0026ensp; and \u0026ensp;__get_computer_choice method__\u0026ensp; are returned and stored as user_choice and computer_choice variables respectively and are passed as arguments within the parameter of the get_winner method. The get_winner function contains if,elif,else statements which decide the winner based on the classic Rock,Paper,Scissors rules \n\n__play_game method__\n\nIn order to run the main game loop A define play_game function was created amd the main code encapsulated into this methods and the corresponding functions mentioned above are called in the play_game function. Since the main code from the model is within a while loop the script reads the input from the camera and then compares it with the computer's choice without stopping. Therefore the \u0026ensp;__time.time() function__\u0026ensp; was stored within a  start_time variable and placed outside of the __play_game__ while loop. The  __time.time() function__ is to used to calculate the current function and placed in a variable  placed within the while loop. An elapsed time variable was created which is the difference between the start_time and current time. This allows for a countdown before the get_prediction function is called. A series of if,elif statements were created to ensure the the \u0026ensp;__get_prediction method__\u0026ensp; and the \u0026ensp;__get_computer_choice method__\u0026ensp; is called after the countdown of 5 seconds. then between 5 and 7 seconds the \u0026ensp;__get_winner method__\u0026ensp; is called. After  7 seconds the start_variable is called  which resets the countdown. A series of true/false flags are placed within the code of the \u0026ensp;__play_game method__\u0026ensp; to ensure only one user prediction is store per iteration.\n\n\u0026nbsp;\n\n![](Images/Milestone_4%20-%20play_game_1.PNG)\n![](Images/Milestone_4%20-%20play_game_2.PNG)\n![](Images/Milestone_4%20-%20play_game_3.PNG)\n\n*Figure 7 - play_game method*\n\n\u0026nbsp;\n\n __Class computer vision__\n\n\u0026nbsp;\n\nIn order to repeat the game until the user or computer wins 3 games, each win would need to be stored and iterated within a variable. In order to do this the methods for the game were wrapped in a class called Computer_Vision. The def \u0026ensp;__init__\u0026ensp; method was created in order to initialize the two parameters which are computer_wins and user_wins. The parameters initialise and  iterate the user and computer score. A series of if,elif statements were created in the play_game function which control the user and computer points. If the return variable from the \u0026ensp;__get_winner method__\u0026ensp; is 'Computer wins' then the computer_wins variable is increased by 1 and vise versa for the user wins. An additional set of if,elif statments were created in the \u0026ensp;__play_game method__\u0026ensp; which return a  print statement on who wins the game if the computer_wins or user_wins reaches 3 points. Once 3 points is reach the while loop breaks amd the game ends.\n\n\u0026nbsp;\n\n__Code improvements__\n\n\u0026nbsp;\n\nin order to make the script more readable the methods in the class are encapsulated within the  __game_loop method__ .\n\nTo make the script more user friendly a additional lines are written at the start of the code which explains to the user how to play the game. The user score and user score are also displayed throughout the game the so the user is aware. \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felijah-1994%2Fcomputer-vision-rock-paper-scissors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felijah-1994%2Fcomputer-vision-rock-paper-scissors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felijah-1994%2Fcomputer-vision-rock-paper-scissors/lists"}