OpenCVを用いてマウス操作を実行したい

前提

OpenCVにて、PC内蔵のカメラでハンドトラッキングをして、その位置情報を活用することでマウスを操作できるプログラムを色々と参考にしながら作っています。プログラムを実行したらカメラは起動し手を認識するのですが、手を動かしてもマウスが全く動いてくれません。実行コードのどこを修正すれば良いのでしょうか?ご教授いただければ幸いです。

実現したいこと

マウスを動かす機能を実装したいのですがうまく動きません。

該当のソースコード

Python

import cv2 import mediapipe as mp import pyautogui import time mp_drawing = mp.solutions.drawing_utils mp_hands = mp.solutions.hands cap = cv2.VideoCapture(0)with mp_hands.Hands( min_detection_confidence=0.5, min_tracking_confidence=0.5) as hands: while cap.isOpened(): success, image = cap.read() if not success: print("Ignoring empty camera frame.") continue image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB) image.flags.writeable = False results = hands.process(image) image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) if results.multi_hand_landmarks: for hand_landmarks in results.multi_hand_landmarks: mp_drawing.draw_landmarks( image, hand_landmarks, mp_hands.HAND_CONNECTIONS) cv2.imshow('MediaPipe Hands', image) if cv2.waitKey(1) & 0xFF == ord('q'): break image = cv2.flip(image, 1) image_width, image_height = image.shape[1], image.shape[0] index_finger_tip_x = int(hand_landmarks.landmark[8].x * image_width)index_finger_tip_y = int(hand_landmarks.landmark[8].y * image_height) index_finger_pip_x = int(hand_landmarks.landmark[6].x * image_width)index_finger_pip_y = int(hand_landmarks.landmark[6].y * image_height) if index_finger_tip_y > index_finger_pip_y: pyautogui.doubleClick(index_finger_pip_x,index_finger_pip_y) time.sleep(1) else: pyautogui.moveTo(index_finger_pip_x,index_finger_pip_y) cap.release()cv2.destroyAllWindows()

コメントを投稿

0 コメント