Hand detection issue with Mediapipe: NameError: name 'results' is not defined

120 views Asked by At

On windows 11 I'm working with PyCharm and I'm a beginner. I loaded the OpenCV vers.4.8.1.78 and Mediapipe vers.0.10.7 packages. I also installed the Python 3.10 interpreter. What I would like to do is a program for detecting hand movement by capturing a webcam. I have this code that displays a window where I capture images from a webcam, and that works perfectly.

import cv2
import mediapipe as mp

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False,
                       max_num_hands=2,
                       min_detection_confidence=0.5,
                       min_tracking_confidence=0.5,
                       )

mp_drawing = mp.solutions.drawing_utils

# if you have second camera you can set first parameter as 1
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
# video dimension
cap.set(3,715)
cap.set(4,715)

if not (cap.isOpened()):
    print("Could not open video device")
while (True):

    # Capture the video frame by frame
    ret, frame = cap.read()

    # flip the image
    frame = cv2.cvtColor(cv2.flip(frame, 1), cv2.COLOR_BGR2RGB)
    # recoloring image
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    cv2.imshow('frame', frame)

    # the 'q' button is set as the quitting button, you may use any desired button of your choice
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# After the loop release the cap object
cap.release()
# Destroy all the windows
cv2.destroyAllWindows()

The problem is that if I insert the lines of code to detect the movement of the hands, it gives me an error saying that it does not recognize "results.multi_hand_landmarks:"

frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks(frame, hand_landmarks, connections=mp_hands.HAND_CONNECTIONS)

cv2.imshow('frame', frame)

The error message is this: File "C:\Users\plipl\PycharmProjects\pythonProject\main.py", line 35, in if results.multi_hand_landmarks: NameError: name 'results' is not defined

Can someone help me? Thank you

0

There are 0 answers