- First import the library that we need
import cv2
import numpy as np
import mediapipe as mp
- Make the program to connect to the webcam
import cv2
import numpy as numpy
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
cv2.imshow('Hand Tracking', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
- Load the module of hands and drawing_utils
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands
- Determine the minimum percentage
with mp_hands.Hands(
min_detection_confidence=0.8,
min_tracking_confidence=0.8) as hands:
- Chanfe BGR to RGB
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
- To optimize the program change writeable to False
image.flags.writeable = False
- processing
results = hands.process(image)
- Change RGB to BGR
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
- Make a loop and draw the landmark
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
mp_drawing.draw_landmarks(
image, hand_landmarks, mp_hands.HAND_CONNECTIONS,mp_drawing.DrawingSpec(color=(0, 0, 255 )),
mp_drawing.DrawingSpec(color=(255, 255, 255 )))
Clik the picture to see the Video
![Watch the video](https://camo.githubusercontent.com/25104b8b94f0a6d411e937cf839dc143ed7cc43b2cf992086c84cf2cd75f5e36/68747470733a2f2f696d672e796f75747562652e636f6d2f76692f303643744a68787847716b2f6d617872657364656661756c742e6a7067)