基于mediapipe和cvzone的虚拟键盘

目录

         PS:一个来自印度老哥的cvzone网站的小项目,这里做源码的讲解

效果演示:

原理及流程:

代码如下: 

源码剖析:


PS:一个来自印度老哥的cvzone网站的小项目,这里做源码的讲解

效果演示:

原理及流程:

'''
1.链接摄像头
2.识别手势
3.绘制键盘
 3.1创建键盘字母List
 3.2通过循环绘制键盘
4.根据坐标,取得返回字母
 4.1 利用lmList[8]食指之间坐标,判断选中的字母
 4.2 利用食指与中指之间的距离,确认输入的字母
 5.扩展,修改键盘背景
 6.利用pynput模拟真实键盘输入
'''

代码如下: 

import cv2
from cvzone.HandTrackingModule import HandDetector
from time import sleep
import numpy as np
import cvzone
from pynput.keyboard import Key,Controller
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
#识别手势
detector = HandDetector(detectionCon=0.8)
keyboard = Controller()
#键盘关键字
keys = [['Q','W','E','R','T','Y','U','I','O','P'],
        ['A','S','D','F','G','H','J','K','L',';'],
        ['Z','X','C','V','B','N','M',',','.','/']]
class Button():
    def __init__(self,pos,text,size = [50,50]):
        self.pos = pos
        self.text = text
        self.size = size

buttonList = []
finalText = ''
for j in range(len(keys)):
    for x,key in enumerate(keys[j]):
        #循环创建buttonList对象列表
        buttonList.append(Button([60*x+20,100+j*60],key))

def drawAll(img,buttonList):
    for button in buttonList:
        x, y = button.pos
        w, h = button.size
        cvzone.cornerRect(img,(x,y,w,h),20,rt = 0)
        cv2.rectangle(img, button.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED)
        cv2.putText(img, button.text, (x + 10, y + 40),
                    cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 2)
    return img

while True:
    success,img = cap.read()
    #识别手势
    img = detector.findHands(img)
    lmList,bboxInfo = detector.findPosition(img)

    img = drawAll(img,buttonList )
    if lmList:
        for button in buttonList:
            x,y = button.pos
            w,h = button.size
            if x<lmList[8][0]<x+w and y<lmList[8][1]<y+h:
                cv2.rectangle(img, (x-5,y-5), (x + w + 5, y + h + 5), (175, 0, 175), cv2.FILLED)
                cv2.putText(img, button.text, (x + 10, y + 40),
                            cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 2)

                l,_,_ = detector.findDistance(8,12,img,draw=False)
                print('中指(12)和食指(8)之间的距离:',l)
                if l < 30:
                    keyboard.press(button.text)
                    cv2.rectangle(img, button.pos, (x + w, y + h), (0, 255, 0), cv2.FILLED)
                    cv2.putText(img, button.text, (x + 10, y + 40),
                                cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 2)
                    finalText += button.text
                    print('当前选中的是:', button.text)
                    sleep(0.2)
    cv2.rectangle(img, (20,350), (600, 400), (175, 0, 175), cv2.FILLED)
    cv2.putText(img, finalText, (20, 390),
                cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 4)
    cv2.imshow("Image",img)
    if cv2.waitKey(1)==ord('q'):
        break

源码剖析:

cap.set(3,1280) #设置摄像头窗口的宽度为1280
cap.set(4,720)  #设置摄像头窗口的高度为720
from pynput.keyboard import Key,Controller
keyboard = Controller() #获取键盘的操控对象
class Button():  #创建虚拟键盘的按钮对象,分别是位置,内容,和默认为50*50的大小
    def __init__(self,pos,text,size = [50,50]):
        self.pos = pos
        self.text = text
        self.size = size
buttonList = []
for j in range(len(keys)):
    for x,key in enumerate(keys[j]):
        buttonList.append(Button([60*x+20,100+j*60],key)) #循环创建buttonList对象列表
def drawAll(img,buttonList): #在图像上绘制所有的自定义键盘按钮
    for button in buttonList:
        x, y = button.pos
        w, h = button.size
        cvzone.cornerRect(img,(x,y,w,h),20,rt = 0) #在按钮图像的轮廓边缘进行以四个角延伸20px的绿色线条
        cv2.rectangle(img, button.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED) #以红色填充键盘按钮区域
        cv2.putText(img, button.text, (x + 10, y + 40), 
                    cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 2)#将自定义的键盘的内容显示到图像上的指定位置,以FONT_HERSHEY_PLAIN字体,3的字体大小,白色颜色,2px的粗细
    return img
输出选中的信息框
cv2.rectangle(img, (20,350), (600, 400), (175, 0, 175), cv2.FILLED)  #绘制输出框
cv2.putText(img, finalText, (20, 390),
            cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 4)  #将已经录入的字符全部显示到图像帧上对应的输出框中
lmList,bboxInfo = detector.findPosition(img)  #会在识别出手部信息后在周围框上绿色的框

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

獜洛橙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值