You are on page 1of 3

import the necessary files

controller inside keyboard


mp.solutions.hands inside mp_hands and mp.solutions.drawing.utils inside
mp_drawing
mp_hands.Hands inside hands with minimum detection confidence as 0.8 and
tracking as 0.5
tip ids
state = None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
def countFingers(image, hand_landmarks, handNO = 0):
global state
get all landmarks of the First Hand VISIBLE
if hand_landmarks:
landmarks = hand_landmarks[handNO].landmark
make an empy list for variable fingers
for lm_index in tipIds:
#Get finger tip and bottom Y position value
finger_tip_y = landmarks[lm_index].y
finger_bottom_y = landmarks[lm_index-2].y
#check if any finger is open or closed
if lm_index !=4:
fingers.append(1)
if finger_tip_y > fniger_bottom_y:
fingers.append(0)
increase total finger count by 1
totalFingers = fingers.count(1)
#Play or pause a video
if totalFingers ==4:
state = "play"
if totalFingers ==0 and state =="play":
state = "pause"
keyboard.press(Key.space)
#Move video forward and backwards
finger_tip_x = (landmarks[8].x)*width
if finger_tip_x < width -400:
print("play backward")
keyboard.press(Key.left)
if finger_tip_x > width -60:
print("Play forward")
keyboard.press(Key.right)
#define a function to
def drawHandLandmarks(image, hand_landmarks):
draw connection between landmarks points
if hand_landmarks:
for landmarks in hand_landmarks:
mp_drawing.draw_landmarks(image, landmarks, mp_hands.HAND_CONNECTIONS)
while True:
success, image = cap.read()
image = cv2.flip(image,1)
#Detect the Hand Landmarks
results = hands.process(image)
#Get the landmark position from the processed result
hand_landmarks = results.multi_hand_landmarks
#Draw landmarks
drawHandLandmarks(image, hand_landmarks)
#Get hand fingers position
countFingers(image,hand_landmarks)
cv2.imshow("Media Controller",image)
#Quit the window on pressing Spacebar key
key = cv2.waitkey(1)
if key ==27:
break
cv2.destroyAllWindows()

first part of the code practice


import cv2
import mediapipe as mp
from pynput.keyboard import Key, Controller

keyboard = Controller()
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(min_detection_confidence = 0.8, min_tracking_confidence =
0.5)
tipIds = [4,8,12,16,20]
state = None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

to remember -
import the necessary libraries that are cv2, mediapide and key, controller
from pynput.keyboard
store controller in keyboard vraiable ()
open camera - cap = cv2.VideoCapture(0)
store mp.solutions.hands in mp_hands variable and mp.solutions.drawing_utils
in mp_drawing
hands = mp_hands.Hands(min_detection_confidence = 0.8 and min_tracking_confidence
as 0.5.
tipIds = [4,8,12,16,20]
state = None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

import cv2
import mediapipe as mp
from pynput.keyboard import Key, Controller
keyboard = Controller()
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(min_detection_confidence = 0.8, min_tracking_confidence =
0.5)
tipIds = [4,8,12,16,20]
state = None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

import cv2
import mediapipe as mp
from pynput.keyboard import Keyboard, Controller
keyboard = Controller()
cap = cv2.VideoCapture(0)
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(min_detection_confidence = 0.8, min_tracking_confidence =
0.5)
tipIds[4,8,12,16,20]
state = None
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

icgccpfw
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

import cv2
import mediapipe as mp
from pynput.keyboard import Keyboard, Controller

You might also like