0

Apologies if i format this wrong its my first question! but I'm new to Mediapipe and OpenCV and I am trying to figure out a snippet of code. It seems to run the webcam and hand detection fine however it does not output any shape or text in the console or on the canvas when I attempt to draw a shape with my hand, is there a specific way this is detected or am I missing something very obvious?

I am using Pycharm with the Mediapipe and Opencv packages installed directly through it.

Any help would be appreciated thanks!

import cv2
import mediapipe as mp
import numpy as np


# Initialize Mediapipe hands
mp_drawing = mp.solutions.drawing_utils
mp_hands = mp.solutions.hands

# Initialize OpenCV
cap = cv2.VideoCapture(0)

# Initialize drawing parameters
color = (0, 255, 0)
thickness = 2

# Initialize drawing canvas
canvas = np.zeros((480, 640, 3), dtype=np.uint8)

# Initialize shape drawing flag
draw_shape = False
shape_type = ""

# Define shape drawing function
def draw_shape_on_canvas(canvas, shape_type):
    if shape_type == "rectangle":
        cv2.rectangle(canvas, (50, 50), (200, 200), color, -1)
    elif shape_type == "circle":
        cv2.circle(canvas, (320, 240), 100, color, -1)
    elif shape_type == "triangle":
        pts = np.array([[320, 50], [50, 430], [590, 430]], np.int32)
        cv2.fillPoly(canvas, [pts], color)


# Initialize Mediapipe hands
with mp_hands.Hands(min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands:
    while True:

        ret, frame = cap.read()

        # Flip frame horizontally for natural movement
        frame = cv2.flip(frame, 1)

        # Convert frame to RGB for hand detection
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        # Process frame with Mediapipe hands
        results = hands.process(frame_rgb)

        # Check if hand landmarks detected
        if results.multi_hand_landmarks:
            # Get first hand landmark
            hand_landmarks = results.multi_hand_landmarks[0]

            # Get index finger tip position
            index_finger_tip = hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP]
            x = int(index_finger_tip.x * frame.shape[1])
            y = int(index_finger_tip.y * frame.shape[0])

            # Check if index finger is extended
            is_index_finger_extended =   hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].visibility > 0.5

            # Check if thumb is extended
            is_thumb_extended = hand_landmarks.landmark[mp_hands.HandLandmark.THUMB_TIP].visibility > 0.8

            # Check if shape drawing mode is on
            if draw_shape:
                # Draw shape on canvas
                draw_shape_on_canvas(canvas, shape_type)

            # Check if index finger is extended and thumb is not extended
            if is_index_finger_extended and not is_thumb_extended:
                # Turn on shape drawing mode
                draw_shape = True

                # Check finger position to determine shape type
                if y < 240:
                    shape_type = "triangle"
                elif x < 320:
                    shape_type = "rectangle"
                else:
                    shape_type = "circle"
            else:
                # Turn off shape drawing mode
                draw_shape = False

            # Draw hand landmarks and shape type on frame
            mp_drawing.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
            cv2.putText(frame, shape_type, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
                        3, (255, 0, 255), 2)


        cv2.imshow("image", frame)
        cv2.waitKey(1)

0 Answers0