OpenCV 练手(一)--实现视频虚拟键盘

前言

       小破站瞄到的视频,主要适合初学者或刚入门的人看。需要的可以直接搜标题应该就有。当然本文不是简单的Code copy,此文作为一个思路整理和学习笔记吧,希望对你有参考价值。

如有问题您可以留言评论指正,很期待探讨。

一、OpenCV是什么?

百度百科有,emmm,我的理解里就是一个强大的图像处理工具。

二、虚拟键盘实现效果

在这里插入图片描述

最终结果示意图

三、OpenCV实现流程

1.链接摄像头,获取视频流

cap = cv2.VideoCapture(0)#参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频,
						 #如cap = cv2.VideoCapture(../test.mp4”)
cap.set(3,1280)#设置视频宽度
cap.set(4,720)#设置视频长度
while True:
    success,img = cap.read()#cap.read()按帧读取视频,ret,frame是获cap.read()方法的两个返回值。
    						#其中ret是布尔值,如果读取帧是正确的则返回True,如果文件读取到结尾,
    						#它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。
    cv2.imshow("Image",img)#链接窗口名为Image
    cv2.waitKey(1)#延时1ms输出下一张图像

cap.set()详细参数说明《传送门

2.识别手势

detector = HandDetector(detectionCon=0.8)#设置置信度
img = detector.findHands(img)#手部定位
lmList,bboxInfo = detector.findPosition(img)#手部节点标记

       虽然视频中调用的是cvzone.HandTrackingModule去进行手部图像的识别,但是查看其GitHub源码可知,其调用的也是Google的mediapipe,Google开源的一个强大的图像标记处理库。

3.绘制界面键盘

#键盘关键字
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',',','.','/']]
#Button的主要目的就是为了将位置,大小,内容link在一起,方便后面使用
class Button():
    def __init__(self,pos,text,size = [50,50]):
        self.pos = pos
        self.text = text
        self.size = size
#创建由Button对象组成的List  
for j in range(len(keys)):
    for x,key in enumerate(keys[j]):
        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
        #绘制矩形底框,填充色为RGB(255, 0, 255)
        cv2.rectangle(img, but,ton.pos, (x + w, y + h), (255, 0, 255), cv2.FILLED)
        #按键盘布局排列keys
        cv2.putText(img, button.text, (x + 10, y + 40),
                    cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 2)
    return img

4.获取选择的字母

食指指尖选择,中指指尖确认
在这里插入图片描述

lmList的Index所对应的相应节点
#lmList为手部节点坐标
if lmList:
   for button in buttonList:
       x,y = button.pos
       w,h = button.size
       #lmList[8]为食指指尖坐标,lmList[12]为中指指尖坐标
       if x<lmList[8][0]<x+w and y<lmList[8][1]<y+h:
           l,_,_ = detector.findDistance(8,12,img,draw=False)
           if l < 30:
           	   #用户UI反馈,选中后修改字体和底部矩形颜色
               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)
               #设定间隔时间,避免多次选择同一字母
               sleep(0.2)

5.模拟真实键盘输入

#输入结果显示于图像
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)

6.扩展:修改键盘UI

def drawAll(img,buttonList):
    imgNew = np.zeros_like(img,np.uint8)
    for button in buttonList:
        x,y = button.pos
        w, h = button.size
        cvzone.cornerRect(imgNew,(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)
    out = img.copy()
    alpha = 0.5
    mask = imgNew.astype(bool)
    print(mask.shape)
    out[mask] = cv2.addWeighted(img,alpha,imgNew,1-alpha,0)[mask]
    return out

7.完整代码

'''
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
    # def draw(self,img):
    #     x,y = self.pos
    #     w,h = self.size
    #     cv2.rectangle(img, self.pos, (x+w,y+h), (255, 0, 255), cv2.FILLED)
    #     cv2.putText(img, self.text, (x+10,y+40),
    #                 cv2.FONT_HERSHEY_PLAIN, 3,(255, 255, 255), 2)
    #     return img
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))
#mybutton = Button([100,100],"Q")
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
# def drawAll(img,buttonList):
#     imgNew = np.zeros_like(img,np.uint8)
#     for button in buttonList:
#         x,y = button.pos
#         w, h = button.size
#         # cvzone.cornerRect(imgNew,(button.pos[0],button.pos[1],button.size[0],button.size[1]),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)
#         out = img.copy()
#         alpha = 0.1
#         mask = imgNew.astype(bool)
#         print(mask.shape)
#         out[mask] = cv2.addWeighted(img,alpha,imgNew,1-alpha,0)[mask]
#         return out
while True:
    success,img = cap.read()
    #识别手势
    img = detector.findHands(img)
    lmList,bboxInfo = detector.findPosition(img)

    # img = mybutton.draw(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)
    cv2.waitKey(1)

总结

Ending:
       一个学习记录,其实还有可以改善的地方,例如可以换用mediapipe实现,页面UI原视频也是提供一个引导,还有可以改善的空间,以及人物背景切换,不要视频直接采集的背景,等等,后面继续完善技能树,争取一周两篇学习文章,学海无涯,输出就是最高效的输入,共勉。

  • 4
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值