欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用?OpenCV?開發(fā)虛擬鍵盤的方法

 更新時(shí)間:2021年11月27日 09:49:39   作者:woshicver  
OpenCV是一個(gè)強(qiáng)大的圖像處理工具,用于機(jī)器學(xué)習(xí)、圖像處理等的跨平臺(tái)開源庫,用于開發(fā)實(shí)時(shí)計(jì)算機(jī)視覺應(yīng)用程序,本文重點(diǎn)給大家介紹使用?OpenCV?開發(fā)虛擬鍵盤的方法,感興趣的朋友一起看看吧

介紹

OpenCV 是最流行的計(jì)算機(jī)視覺任務(wù)庫,它是用于機(jī)器學(xué)習(xí)、圖像處理等的跨平臺(tái)開源庫,用于開發(fā)實(shí)時(shí)計(jì)算機(jī)視覺應(yīng)用程序。

CVzone 是一個(gè)計(jì)算機(jī)視覺包,它使用 OpenCV 和 Media Pipe 庫作為其核心,使我們易于運(yùn)行,例如手部跟蹤、人臉檢測(cè)、面部標(biāo)志檢測(cè)、姿勢(shì)估計(jì)等,以及圖像處理和其他計(jì)算機(jī)視覺相關(guān)的應(yīng)用程序。

使用 OpenCV 實(shí)現(xiàn)虛擬鍵盤

讓我們創(chuàng)建一個(gè)虛擬鍵盤。

首先,讓我們安裝所需的模塊。

pip install numpy
 
pip install opencv-python
 
pip install cvzone
 
pip install pynput

使用 OpenCV 為虛擬鍵盤導(dǎo)入庫

現(xiàn)在讓我們導(dǎo)入所需的模塊

import cv2
import cvzone
from cvzone.HandTrackingModule import HandDetector
from time import sleep
import numpy as np
from pynput.keyboard import Controller

這里我們從 cvzone.HandTrackingModule 導(dǎo)入 HandDetector 模塊,然后為了使虛擬鍵盤工作,我們需要從 pynput.keyboard 導(dǎo)入Controller。

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, 1280)
cap.set(4, 720)

現(xiàn)在讓我們從 cv2.Videocapture 獲取實(shí)時(shí)輸入

detector = HandDetector(detectionCon=0.8)
keyboard_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", ",", ".", "/"]]
final_text = ""

我們以 0.8 的檢測(cè)置信度初始化 HandDetector 并將其分配給檢測(cè)器。

然后我們根據(jù)鍵盤的布局創(chuàng)建一個(gè)列表數(shù)組,并定義一個(gè)空字符串來存儲(chǔ)鍵入的鍵。

定義繪制函數(shù)

keyboard = Controller()
def draw(img, buttonList):
    for button in buttonList:
        x, y = button.pos
        w, h = button.size
        cvzone.cornerRect(img, (button.pos[0], button.pos[1],
                                                   button.size[0],button.size[0]), 20 ,rt=0)
        cv2.rectangle(img, button.pos, (int(x + w), int(y + h)), (255, 144, 30), cv2.FILLED)
        cv2.putText(img, button.text, (x + 20, y + 65),
                    cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
    return img

初始化鍵盤控制器,并定義一個(gè)名為draw()的函數(shù),它接受兩個(gè)參數(shù),即圖像和按鈕列表并返回圖像。在draw()函數(shù)內(nèi)部,我們使用 cvzone 的cornerRect函數(shù)在每個(gè)鍵的角落繪制矩形邊緣。這是為了讓我們的鍵盤布局看起來更好看。就像下面的圖片。

fbae7ea2f2bae2fd145638e43131a4c3.png

你也可以嘗試更改不同的顏色。

class Button():
    def __init__(self, pos, text, size=[85, 85]):
        self.pos = pos
        self.size = size
        self.text = text

然后我們定義一個(gè)名為 Button() 的類,并提供位置、文本和大小作為輸入,以便我們可以按照明確定義的順序排列鍵盤按鍵。

buttonList = []
# mybutton = Button([100, 100], "Q")
for k in range(len(keyboard_keys)):
    for x, key in enumerate(keyboard_keys[k]):
        buttonList.append(Button([100 * x + 25, 100 * k + 50], key))

上面的循環(huán)將遍歷鍵盤按鍵和 Button 對(duì)象,我們?cè)谄渲薪o出位置和文本作為輸入附加在一個(gè)名為 button list 的列表中。稍后我們可以將這個(gè)列表傳遞給 draw 函數(shù)以在我們的實(shí)時(shí)框架之上進(jìn)行繪制。

使用 OpenCV 的虛擬鍵盤主程序

重要的部分來了。

while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bboxInfo = detector.findPosition(img)
    img = draw(img, buttonList)  # change the draw funtion to transparent_layout for transparent keys
    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, button.pos, (x + w, y + h),
(0, 255, 255), cv2.FILLED)
cv2.putText(img, button.text, (x + 20, y + 65),
cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
l, _, _ = detector.findDistance(8,12, img, draw=False)
print(l)
 
if l < 25:
keyboard.press(button.text)
cv2.rectangle(img, button.pos, (x + w, y + h),
(0, 255, 0), cv2.FILLED)
cv2.putText(img, button.text, (x + 20, y + 65),
cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
final_text += button.text
sleep(0.20)
 
cv2.rectangle(img, (25,350), (700, 450),
(255, 255, 255), cv2.FILLED)
cv2.putText(img, final_text, (60, 425),
cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
 
# cv2.rectangle(img, (100,100), (200,200),
# (100, 255, 0), cv2.FILLED)
# cv2.putText(img, 'Q', (120,180), cv2.FONT_HERSHEY_PLAIN, 5,
# (0, 0, 0), 5)
 
# img = mybutton.draw(img)
cv2.imshow("output", img)
cv2.waitKey(1)

在 while 循環(huán)中,首先我們讀取實(shí)時(shí)輸入幀并將其存儲(chǔ)在一個(gè)名為img的變量中。然后我們將該圖像傳遞給*檢測(cè)器.findHands()*以便在幀中找到手。然后在該圖像中,我們需要找到檢測(cè)到的手的位置和邊界框信息。

在這里我們可以找到我們的食指和中指的頂點(diǎn)之間的距離,如果兩者之間的距離小于某個(gè)閾值,那么我們就可以輸入我們所指示的字母。

一旦我們獲得了位置,我們就會(huì)遍歷整個(gè)位置列表。從該列表中,我們找到按鈕位置和按鈕大小,然后根據(jù)明確定義的方式將其繪制在框架上。

c1b9bd31f364e0b0f6a0e0660b1b8ead.png

圖 1:手地標(biāo)模型

之后,我們需要找到食指和中指的頂點(diǎn)之間的距離。在上圖中,你可以看到我們需要的最高點(diǎn)是點(diǎn) 8 和點(diǎn) 12。因此,我們需要在距離查找函數(shù)中傳遞 8, 12 以獲得它們之間的距離。

在上面的代碼中,你可以看到 detector.findDistance(),我們通過了 8、12 和圖像來查找距離,并將繪制標(biāo)志設(shè)置為 false,這樣我們就不需要兩點(diǎn)之間的任何線。

如果點(diǎn)之間的距離非常小,我們將使用 press() 函數(shù)來按下按鍵。在上面的代碼keyboard.press() 中,我們傳遞button.text以顯示按下的鍵。最后,我們?cè)阪I盤布局下方繪制一個(gè)小的白色矩形框,以顯示按下的鍵。

一旦你執(zhí)行了整個(gè)代碼,它看起來像這樣。

2ef7f4bd182782f8b068dd5a67382a8c.png

將食指和中指靠近特定字母的頂部后,你可以鍵入該字母。

f59cdfe271d822da40879257161a8961.png

如果你需要更自定義的鍵盤布局,我們可以使鍵盤布局透明。我們只需要添加一個(gè)透明布局函數(shù)并將*draw()函數(shù)替換為transparent_layout()*函數(shù)即可。

讓我們定義transparent_layout()函數(shù)。下面是函數(shù)的代碼,它采用與draw()函數(shù)相同的輸入。在這里,我們將 numpy 的zero_like()函數(shù)分配給 名為imgNew的變量,并對(duì)其執(zhí)行所需的操作,例如獲得角矩形、為每個(gè)鍵創(chuàng)建矩形框并將文本放入框內(nèi)。之后,我們將該圖像復(fù)制到一個(gè)新變量并創(chuàng)建一個(gè)imgNew掩碼,然后我們使用 OpenCV 的*addWeighted()*函數(shù)將掩碼放置在實(shí)際圖像的頂部。因此,這使鍵盤布局透明。

自定義鍵盤

def transparent_layout(img, buttonList):
    imgNew = np.zeros_like(img, np.uint8)
    for button in buttonList:
        x, y = button.pos
        cvzone.cornerRect(imgNew, (button.pos[0], button.pos[1],
                                                   button.size[0],button.size[0]), 20 ,rt=0)
        cv2.rectangle(imgNew, button.pos, (x + button.size[0], y + button.size[1]),
                                   (255, 144, 30), cv2.FILLED)
        cv2.putText(imgNew, button.text, (x + 20, y + 65),
                    cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
        out = img.copy()
        alpaha = 0.5
        mask = imgNew.astype(bool)
        print(mask.shape)
        out[mask] = cv2.addWeighted(img, alpaha, imgNew, 1-alpaha, 0)[mask] 
        return out

一旦將while 循環(huán)中的*draw()函數(shù)替換為transparent_layout()*函數(shù),它將如下所示。(下圖)

ac2b894f4e35179f231a7473f9df3cce.png

使用 OpenCV 的虛擬鍵盤的完整代碼

下面是完整的代碼

import cv2
import cvzone
from cvzone.HandTrackingModule import HandDetector
from time import sleep
import numpy as np
from pynput.keyboard import Controller
 
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cap.set(3, 1280)
cap.set(4, 720)
 
detector = HandDetector(detectionCon=0.8)
keyboard_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", ",", ".", "/"]]
 
final_text = ""
 
keyboard = Controller()
 
 
def draw(img, buttonList):
    for button in buttonList:
        x, y = button.pos
        w, h = button.size
        cvzone.cornerRect(img, (button.pos[0], button.pos[1],
                                                   button.size[0],button.size[0]), 20 ,rt=0)
        cv2.rectangle(img, button.pos, (int(x + w), int(y + h)), (255, 144, 30), cv2.FILLED)
        cv2.putText(img, button.text, (x + 20, y + 65),
                    cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
    return img
 
 
def transparent_layout(img, buttonList):
    imgNew = np.zeros_like(img, np.uint8)
    for button in buttonList:
        x, y = button.pos
        cvzone.cornerRect(imgNew, (button.pos[0], button.pos[1],
                                                   button.size[0],button.size[0]), 20 ,rt=0)
        cv2.rectangle(imgNew, button.pos, (x + button.size[0], y + button.size[1]),
                                   (255, 144, 30), cv2.FILLED)
        cv2.putText(imgNew, button.text, (x + 20, y + 65),
                    cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
 
    out = img.copy()
    alpaha = 0.5
    mask = imgNew.astype(bool)
    print(mask.shape)
    out[mask] = cv2.addWeighted(img, alpaha, imgNew, 1-alpaha, 0)[mask]
    return out
 
 
class Button():
    def __init__(self, pos, text, size=[85, 85]):
        self.pos = pos
        self.size = size
        self.text = text
 
 
buttonList = []
# mybutton = Button([100, 100], "Q")
for k in range(len(keyboard_keys)):
    for x, key in enumerate(keyboard_keys[k]):
        buttonList.append(Button([100 * x + 25, 100 * k + 50], key))
 
 
while True:
    success, img = cap.read()
    img = detector.findHands(img)
    lmList, bboxInfo = detector.findPosition(img)
    img = draw(img, buttonList)  # change the draw funtion to transparent_layout for transparent keys
 
    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, button.pos, (x + w, y + h),
                              (0, 255, 255), cv2.FILLED)
                cv2.putText(img, button.text, (x + 20, y + 65),
                            cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
                l, _, _ = detector.findDistance(8,12, img, draw=False)
                print(l)
 
                if l < 25:
                    keyboard.press(button.text)
                    cv2.rectangle(img, button.pos, (x + w, y + h),
                                  (0, 255, 0), cv2.FILLED)
                    cv2.putText(img, button.text, (x + 20, y + 65),
                                cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
                    final_text += button.text
                    sleep(0.20)
 
    cv2.rectangle(img, (25,350), (700, 450),
                  (255, 255, 255), cv2.FILLED)
    cv2.putText(img, final_text, (60, 425),
                cv2.FONT_HERSHEY_PLAIN, 4, (0, 0, 0), 4)
 
    # cv2.rectangle(img, (100,100), (200,200),
    #               (100, 255, 0), cv2.FILLED)
    # cv2.putText(img, 'Q', (120,180), cv2.FONT_HERSHEY_PLAIN, 5,
    #             (0, 0, 0), 5)
 
    # img = mybutton.draw(img)
    cv2.imshow("output", img)
    cv2.waitKey(1)

結(jié)論

這是虛擬鍵盤的實(shí)現(xiàn),如果你想完善它,你也可以試著添加按鍵聲音,然后我們還可以讓鍵盤布局在框架內(nèi)移動(dòng)。

到此這篇關(guān)于使用 OpenCV 開發(fā)虛擬鍵盤的方法的文章就介紹到這了,更多相關(guān)OpenCV 虛擬鍵盤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python實(shí)現(xiàn)敏感詞過濾的4種方法

    Python實(shí)現(xiàn)敏感詞過濾的4種方法

    這篇文章主要介紹了Python實(shí)現(xiàn)敏感詞過濾的4種方法,幫助大家處理不和諧的言論,感興趣的朋友可以了解下
    2020-09-09
  • Python一行代碼識(shí)別發(fā)票并保存Excel示例詳解

    Python一行代碼識(shí)別發(fā)票并保存Excel示例詳解

    這篇文章主要為大家介紹了Python一行代碼識(shí)別發(fā)票并保存Excel示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 關(guān)于Python函數(shù)參數(shù)的進(jìn)階用法

    關(guān)于Python函數(shù)參數(shù)的進(jìn)階用法

    這篇文章主要給大家分享的是Python函數(shù)參數(shù)的進(jìn)階用法,Python函數(shù)的參數(shù)根據(jù)函數(shù) 在調(diào)用時(shí) 傳參的形式分為關(guān)鍵字參數(shù)和位置參數(shù),下面文章小編就來介紹相關(guān)資料,需要的朋友可以參考一下
    2021-10-10
  • Python+OpenCV實(shí)現(xiàn)閾值分割的方法詳解

    Python+OpenCV實(shí)現(xiàn)閾值分割的方法詳解

    閾值分割法是一種基于區(qū)域的圖像分割技術(shù),原理是把圖像像素點(diǎn)分為若干類。本文將利用Python+OpenCV實(shí)現(xiàn)閾值分割,感興趣的可以了解一下
    2022-05-05
  • python進(jìn)行TCP端口掃描的實(shí)現(xiàn)

    python進(jìn)行TCP端口掃描的實(shí)現(xiàn)

    這篇文章主要介紹了python進(jìn)行TCP端口掃描的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • python獲取圖片顏色信息的方法

    python獲取圖片顏色信息的方法

    這篇文章主要介紹了python獲取圖片顏色信息的方法,涉及Python使用pil模操作圖片的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • python中urllib.request和requests的使用及區(qū)別詳解

    python中urllib.request和requests的使用及區(qū)別詳解

    這篇文章主要介紹了python中urllib.request和requests的使用及區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • pandas?dataframe寫入到hive方式

    pandas?dataframe寫入到hive方式

    這篇文章主要介紹了pandas?dataframe寫入到hive方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python進(jìn)行有限元仿真的使用及創(chuàng)建

    Python進(jìn)行有限元仿真的使用及創(chuàng)建

    這篇文章主要為大家介紹了Python進(jìn)行有限元仿真的創(chuàng)建及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Python處理PDF及生成多層PDF實(shí)例代碼

    Python處理PDF及生成多層PDF實(shí)例代碼

    Python提供了眾多的PDF支持庫,本篇文章主要介紹了Python處理PDF及生成多層PDF實(shí)例代碼,這樣就能夠?qū)崿F(xiàn)圖片掃描上來的內(nèi)容也可以進(jìn)行內(nèi)容搜索的目標(biāo)
    2017-04-04

最新評(píng)論