![]() |
VOOZH | about |
Let us discuss gesture-controlled game using TensorFlow, I hope this article will be much interesting as it provides a holistic gaming experience at the end. We begin by describing the modules necessary to develop this game.
We start our coding by importing the necessary libraries, including cv2 for capturing video from the webcam, then mediapipe for hand tracking, also numpy for numerical operations, then Tensorflow for loading the pre-trained model and pygame for creating the game window and handling game-related functionalities.
pip install cv2 mediapipe numpy tensorflow pygameFor this gesture-controlled game five libraries are required.
Output:
pygame 2.5.0 (SDL 2.28.0, Python 3.10.6)
Hello from the pygame community. https://www.pygame.org/wiki/Contribute
The pre-trained TensorFlow model is loaded using tf.keras.models.load_model(). This model is trained to recognize hand gestures based on hand landmarks. Add "gesture_model.h5" inside the same folder. You can download the pre-trained gesture model using this GitHub link: gesture_model.h5
Output:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 100, 100, 8) 136
max_pooling2d_1 (MaxPooling (None, 24, 24, 8) 0
2D)
conv2d_2 (Conv2D) (None, 24, 24, 16) 528
max_pooling2d_2 (MaxPooling (None, 12, 12, 16) 0
2D)
flatten_1 (Flatten) (None, 2304) 0
dense_1 (Dense) (None, 512) 1180160
dropout_1 (Dropout) (None, 512) 0
dense_2 (Dense) (None, 128) 65664
dropout_2 (Dropout) (None, 128) 0
dense_3 (Dense) (None, 10) 1290
=================================================================
Total params: 1,247,778
Trainable params: 1,247,778
Non-trainable params: 0
_________________________________________________________________
STEP 3: Initialize MediaPipe Hands model:
The below code initializes the MediaPipe's Hands Model by creating an instance of mp_hands.Hands. This model is used for detecting hand landmarks in each frame captured from the webcam. From the gesture_labels dictionary, it takes 0 as 'Left' and 1 as 'Right'
Next Pygame is initialized by calling pygame.init(). The game window dimensions are set, and the window caption is set to "Gesture-Based Game". The player's properties, such as width, height, initial position, and speed, are defined clearly. The player is represented as a white rectangle. The main game loop is set up using a while loop. This loop continues until the game is quit which means the player hits the obstacle.
Here the entire code, combining all the above steps to start playing our gesture controlled game using TensorFlow is ready. The below code combines video processing, gesture recognition, and game mechanics in order to create an interactive gesture-based gaming experience. Further, improve this code with additional features, and more gestures to make this very interesting.Let's start the game now.
Output:
pygame 2.5.0 (SDL 2.28.0, Python 3.10.6)
Hello from the pygame community. https://www.pygame.org/wiki/Contribute
Pygame starts the game with the player and obstacle, and the gesture recognition shows Neutral to perform the player action.
The player can restart the game with restart button, to continue playing endlessly.
Feel free to support me with your comments below. Happy Playing!