Convert Mediapipe Landmarks to recognizable Gestures
In this code provided is a class called “HandRecog” which is used for gesture recognition using hand tracking. It initializes various variables such as finger count, gesture type, frame count, and hand result. The class has methods to update the hand result, calculate the signed distance, distance, and change in z-coordinate of hand landmarks. It also has a method to set the finger state based on the hand result and handle fluctuations due to noise.
Python3
class HandRecog: def __init__( self , hand_label): self .finger = 0 self .ori_gesture = Gest.PALM self .prev_gesture = Gest.PALM self .frame_count = 0 self .hand_result = None self .hand_label = hand_label def update_hand_result( self , hand_result): self .hand_result = hand_result def get_signed_dist( self , point): sign = - 1 if self .hand_result.landmark[point[ 0 ]].y < self .hand_result.landmark[point[ 1 ]].y: sign = 1 dist = ( self .hand_result.landmark[point[ 0 ]].x - self .hand_result.landmark[point[ 1 ]].x) * * 2 dist + = ( self .hand_result.landmark[point[ 0 ]].y - self .hand_result.landmark[point[ 1 ]].y) * * 2 dist = math.sqrt(dist) return dist * sign def get_dist( self , point): dist = ( self .hand_result.landmark[point[ 0 ]].x - self .hand_result.landmark[point[ 1 ]].x) * * 2 dist + = ( self .hand_result.landmark[point[ 0 ]].y - self .hand_result.landmark[point[ 1 ]].y) * * 2 dist = math.sqrt(dist) return dist def get_dz( self ,point): return abs ( self .hand_result.landmark[point[ 0 ]].z - self .hand_result.landmark[point[ 1 ]].z) # Function to find Gesture Encoding using current finger_state. # Finger_state: 1 if finger is open, else 0 def set_finger_state( self ): if self .hand_result = = None : return points = [[ 8 , 5 , 0 ],[ 12 , 9 , 0 ],[ 16 , 13 , 0 ],[ 20 , 17 , 0 ]] self .finger = 0 self .finger = self .finger | 0 #thumb for idx,point in enumerate (points): dist = self .get_signed_dist(point[: 2 ]) dist2 = self .get_signed_dist(point[ 1 :]) try : ratio = round (dist / dist2, 1 ) except : ratio = round (dist / 0.01 , 1 ) self .finger = self .finger << 1 if ratio > 0.5 : self .finger = self .finger | 1 # Handling Fluctations due to noise def get_gesture( self ): if self .hand_result = = None : return Gest.PALM current_gesture = Gest.PALM if self .finger in [Gest.LAST3,Gest.LAST4] and self .get_dist([ 8 , 4 ]) < 0.05 : if self .hand_label = = HLabel.MINOR : current_gesture = Gest.PINCH_MINOR else : current_gesture = Gest.PINCH_MAJOR elif Gest.FIRST2 = = self .finger : point = [[ 8 , 12 ],[ 5 , 9 ]] dist1 = self .get_dist(point[ 0 ]) dist2 = self .get_dist(point[ 1 ]) ratio = dist1 / dist2 if ratio > 1.7 : current_gesture = Gest.V_GEST else : if self .get_dz([ 8 , 12 ]) < 0.1 : current_gesture = Gest.TWO_FINGER_CLOSED else : current_gesture = Gest.MID else : current_gesture = self .finger if current_gesture = = self .prev_gesture: self .frame_count + = 1 else : self .frame_count = 0 self .prev_gesture = current_gesture if self .frame_count > 4 : self .ori_gesture = current_gesture return self .ori_gesture |
Real-Time AI Virtual Mouse System Using Computer Vision
AI Virtual Mouse is a software that allows users to give inputs of a mouse to the system without using the actual mouse. To the extreme, it can also be called hardware as it uses an ordinary camera. A virtual muse can usually be operated with multiple input devices, which may include an actual mouse or computer keyboard. The virtual mouse uses a web camera with the help of different image processing techniques. Using figures detection methods for instant Camera access and a user-friendly interface makes it more easily accessible. The system is used to implement a motion-tracking mouse, a physical mouse that saves time and also reduces effort. The hand movements of a user are mapped into mouse inputs. A web camera is set to take images continuously. Most laptops today are equipped with webcams, which have recently been used in security applications utilizing face recognition. To harness the full potential of a webcam, it can be used for vision-based CC which would effectively eliminate the need for a computer mouse or mouse pad. The usefulness of a webcam can also be greatly extended to other HCI applications such as a sign language database or motion controller.
Software Specification:
- Python Libraries: Various Python libraries like OpenCV, NumPy, PyAutoGUI, and TensorFlow can be used for building the Al virtual mouse system.
- Open CV: This library is used for image and video processing, which can be used for hand detection and tracking.
- NumPy: NumPy is used for numerical computations, and it is used to process the captured images.
- PyAutoGUI: PyAuto GUI is used to control the mouse movements and clicks.
- Mediapipe: A cross-platform framework for building multi-modal applied machine learning pipelines.
- Comtypes: A Python module that provides access to Windows COM and .NET components.
- Screen-Brightness-Control: A Python module for controlling the brightness of the screen on Windows, Linux, and macOS.
Detecting which Fingure is Up and Performing the particular Mouse Function
Over here we are detecting which finger is Up using the tip ID of the respective finger that we found using the MideaPipe and the respective figure that we found using the Mediapipe and the respective coordinates of the fingers that are up, according to that we found using the MediaPipe and the respective co-coordinates of the figures that are up and according to that the particular mouse function is performed.
Mouse Functions depending on the Hand Gestures and Hand Tip Detection. Using Computer Vision for the mouse cursor moving around the system window. If the index fingure is up with tip Id = 1 or both the index finger with tip Id = 1 and the middle finger with tip Id =2 are up, the mouse cursor is made to move around the window of the computer using the AutoPy package of Python.