实现步骤:

  1. 使用OpenCV读取视频输入。

  2. 使用MediaPipe进行手部检测和跟踪,获取手部的坐标。

  3. 将手部坐标映射到屏幕上,作为虚拟鼠标的坐标。

  4. 使用PyAutoGUI将虚拟鼠标移动到指定位置。

  5. 在每个视频帧上重复上述步骤,实现实时的虚拟鼠标控制。

代码实现:

  1. 导入所需库和模块。
import cv2
import mediapipe as mp
import pyautogui
  1. 初始化MediaPipe的手部检测模型。
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)
  1. 打开视频输入。
cap = cv2.VideoCapture(0)
  1. 循环处理每个视频帧。
while True:
    # 读取视频帧
    ret, frame = cap.read()
    
    # 将视频帧转换为RGB格式
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
    # 使用MediaPipe检测手部,并获取手部的坐标
    results = hands.process(frame)
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            for id, lm in enumerate(hand_landmarks.landmark):
                # 将手部坐标映射到屏幕上
                h, w, c = frame.shape
                x, y = int(lm.x * w), int(lm.y * h)
                pyautogui.moveTo(x, y)
    
    # 将视频帧从RGB格式转换为BGR格式
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    
    # 显示视频帧
    cv2.imshow('Virtual Mouse', frame)
    
    # 按下'q'键退出循环
    if cv2.waitKey(1) == ord('q'):
        break
  1. 释放视频输入和MediaPipe模型。
cap.release()
hands.close()

完整代码:

import cv2
import mediapipe as mp
import pyautogui

mp_hands = mp.solutions.hands
hands = mp_hands.Hands(max_num_hands=1)

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    
    results = hands.process(frame)
    if results.multi_hand_landmarks:
        for hand_landmarks in results.multi_hand_landmarks:
            for id, lm in enumerate(hand_landmarks.landmark):
                h, w, c = frame.shape
                x, y = int(lm.x * w), int(lm.y * h)
                pyautogui.moveTo(x, y)
    
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    
    cv2.imshow('Virtual Mouse', frame)
    
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
hands.close()
使用OpenCV和MediaPipe的虚拟鼠标设计的视频输入模块

原文地址: https://www.cveoy.top/t/topic/bK7q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录