-
Importing Libraries:
cv2
: OpenCV library for computer vision tasks.mediapipe as mp
: MediaPipe library for hand tracking and gesture recognition.
-
Constants for Digit-Color Mapping:
COLORS
: A list of colors, where each color corresponds to a digit from 0 to 10. The colors are represented as tuples of RGB values.DIGIT_THRESHOLD
: A threshold value for digit recognition based on the Y-coordinate of the index finger tip.
-
Initializing MediaPipe Hand Tracking:
mp_hands
: An instance of the MediaPipe Hand Tracking solution.mp_draw
: Utility functions for drawing landmarks on the image.
-
Starting Video Capture:
cap
: A VideoCapture object to capture frames from the default camera (index 0).
-
Hand Tracking Loop:
- The code enters a loop to continuously capture frames from the camera and perform hand tracking.
-
Processing Each Frame:
- Inside the loop, each frame is read from the camera (
cap.read()
), and the success flag indicates whether the frame was read successfully. - The frame is then converted from BGR to RGB format (
cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
) as MediaPipe processes RGB images.
- Inside the loop, each frame is read from the camera (
-
Hand Landmark Detection:
- The
hands.process()
method detects hand landmarks in the frame using MediaPipe's Hand Tracking solution. - If hand landmarks are detected (
results.multi_hand_landmarks
), the code proceeds to analyze the landmarks.
- The
-
Digit Recognition:
- For each detected hand (
for hand_landmarks in results.multi_hand_landmarks
), the Y-coordinate of the index finger tip (index_finger_tip.y
) is checked against theDIGIT_THRESHOLD
. - If the Y-coordinate is below the threshold, it's mapped to a digit between 0 and 10 (
detected_digit = round(index_finger_tip.y * 10)
).
- For each detected hand (
-
Drawing Hand Landmarks:
- Hand landmarks are drawn on the image using
mp_draw.draw_landmarks()
.
- Hand landmarks are drawn on the image using
-
Displaying Digit on Screen:
- If a digit is detected (
detected_digit
is notNone
), its corresponding color is retrieved from theCOLORS
list. - The digit is then drawn on the image using
cv2.putText()
with the corresponding color.
- If a digit is detected (
-
Displaying the Image:
- The processed image with hand landmarks and detected digit is displayed using
cv2.imshow()
.
- The processed image with hand landmarks and detected digit is displayed using
-
Exiting the Loop:
- The loop continues until the user presses the 'q' key (
if cv2.waitKey(1) == ord('q')
), at which point the loop is exited.
- The loop continues until the user presses the 'q' key (
-
Releasing Resources:
- After the loop ends, the video capture object is released (
cap.release()
) and all OpenCV windows are destroyed (cv2.destroyAllWindows()
).
- After the loop ends, the video capture object is released (
This code provides a real-time visualization of hand landmarks and recognizes digits based on the movement of the index finger tip.
Fingertips movement corresponds to the recognition of different digits based on the position of the index finger tip relative to the camera's view.
Here's how the code works:
-
Hand Tracking: The code uses MediaPipe's Hand Tracking module to detect and track the landmarks of the hand, including the index finger tip.
-
Digit Recognition: The Y-coordinate of the index finger tip is used to determine which digit to recognize. The Y-coordinate is normalized between 0 and 1, where 0 corresponds to the top of the screen and 1 corresponds to the bottom. The code then maps this normalized Y-coordinate to a range of digits (0 to 4 in the provided code).
-
Color Mapping: Each recognized digit is associated with a color from the
COLORS
list. For example, digit 0 corresponds to the color(0, 255, 0)
, digit 1 to(0, 255, 255)
, and so on. -
Displaying Digits: When a digit is recognized, its corresponding color is retrieved from the
COLORS
list, and the digit is displayed on the screen using OpenCV'scv2.putText()
function.
Therefore, the movement of fingertips triggers the recognition of different digits, which in turn leads to the display of different colors on the screen based on the recognized digit.
The digit recognition mechanism has been adjusted to round the Y-coordinate of the index finger tip multiplied by 10 to get an integer value between 0 and 10. When a digit is recognized, its corresponding color is retrieved from the COLORS list and used to display the digit on the screen. The code can now recognize and display digits from 0 to 10 based on the movement of the fingertips.