Finger control servo motor with Arduino

Code Snippet with Copy Button

import cv2
import mediapipe as mp
import serial
import time

# Initialize MediaPipe Hands module
mp_hands = mp.solutions.hands
hands = mp_hands.Hands()
mp_drawing = mp.solutions.drawing_utils

# Initialize serial communication with Arduino
ser = serial.Serial('COM4', 9600)  # Replace 'COM4' with your Arduino port
time.sleep(2)  # Allow some time for the connection to establish

# Open webcam
cap = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    success, image = cap.read()
    if not success:
        break

    # Flip the image horizontally for a later selfie-view display
    image = cv2.flip(image, 1)

    # Convert the BGR image to RGB
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Process the image to detect hands and landmarks
    results = hands.process(rgb_image)

    # Draw hand landmarks and control servo motor
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            # Track the index finger tip (landmark 8)
            index_finger_tip = hand_landmarks.landmark[8]
            x = int(index_finger_tip.x * image.shape[1])
            y = int(index_finger_tip.y * image.shape[0])

            # Optional: Draw a circle at the index finger tip for visualization
            cv2.circle(image, (x, y), 10, (255, 0, 0), -1)

            # Map the x-coordinate of the index finger tip to an angle (0-180 degrees)
            angle = int(index_finger_tip.x * 180)

            # Send the angle to Arduino
            ser.write(f"{angle}\n".encode())

    # Display the resulting frame
    cv2.imshow('Finger Control for Servo', image)

    # Break the loop when 'q' key is pressed
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the capture and close windows
cap.release()
cv2.destroyAllWindows()
ser.close()
    

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.