KyrieRui / Hand_Recognition

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hand Detector

Author: Ray

Date: 09/20/2022

Description: This is a hand detector using OpenCV and Mediapipe pakages.

This program basically detects the hand and the landmarks through user's camera.

Hand Detector

Installation

Install OpenCV

pip install opencv-python

Install Mediapipe

pip install mediapipe

If you are use M1 Mac, you can use this command to install Mediapipe

pip install mediapipe-silicon

Usage

  1. import two packages
import cv2
import mediapipe as mp
  1. allow the camera and hand solution in mediapipe
camera = cv2.VideoCapture(0) ## '0' is your device default camera, use '1' if you have another camera
hand_detetor = mp.solutions.hands.Hands()
  1. We can def an loop, as long as the camera is open, we can get the image from the camera
while True:
    success, img = camera.read()
    if success:
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ## because the mediapipe only support RGB image we need to convert it to RGB
        results = hand_detetor.process(img_rgb) ## get the hand result
        
        if results.multi_hand_landmarks: ## if we can detect the hand
            for hand_landmarks in results.multi_hand_landmarks: ## we can get the hand landmarks
                mp.solutions.drawing_utils.draw_landmarks(img, hand_landmarks, mp.solutions.hands.HAND_CONNECTIONS) ## draw the hand landmarks
        
        cv2.imshow("Hand Detector", img)
    quit = cv2.waitKey(1)

    if quit == ord('q'):
        break
  1. Last step, we need to release the camera and destroy the window to save the device memory use
camera.release()
cv2.destroyAllWindows()

At present, the program can store the joint points of the left and right hands separately in a specific dictionary to facilitate searching and calling. Next, I will try to determine whether the user gesture is a specific gesture, and then implement operations such as painting in the air or controlling the computer mouse.

Reference

https://www.bilibili.com/video/BV1mb4y1B78p?vd_source=9fa62e123b591fe30734493ac73e11b1

About


Languages

Language:Python 100.0%