<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何使用 OpenCV 開發(fā)虛擬鍵盤

          共 19008字,需瀏覽 39分鐘

           ·

          2021-12-01 13:50

          點擊上方小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂

          重磅干貨,第一時間送達(dá)

          介紹
          OpenCV 是最流行的計算機視覺任務(wù)庫,它是用于機器學(xué)習(xí)、圖像處理等的跨平臺開源庫,用于開發(fā)實時計算機視覺應(yīng)用程序。
          CVzone 是一個計算機視覺包,它使用 OpenCV 和 Media Pipe 庫作為其核心,使我們易于運行,例如手部跟蹤、人臉檢測、面部標(biāo)志檢測、姿勢估計等,以及圖像處理和其他計算機視覺相關(guān)的應(yīng)用程序。
          使用OpenCV實現(xiàn)虛擬鍵盤
          讓我們創(chuàng)建一個虛擬鍵盤。
          首先,讓我們安裝所需的模塊。
          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(31280)
          cap.set(4720)
          現(xiàn)在讓我們從 cv2.Videocapture 獲取實時輸入
          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 的檢測置信度初始化 HandDetector 并將其分配給檢測器。
          然后我們根據(jù)鍵盤的布局創(chuàng)建一個列表數(shù)組,并定義一個空字符串來存儲鍵入的鍵。
          定義繪制函數(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)), (25514430), cv2.FILLED)
                  cv2.putText(img, button.text, (x + 20, y + 65),
                              cv2.FONT_HERSHEY_PLAIN, 4, (000), 4)
              return img
          初始化鍵盤控制器,并定義一個名為draw()的函數(shù),它接受兩個參數(shù),即圖像和按鈕列表并返回圖像。在draw()函數(shù)內(nèi)部,我們使用 cvzone 的cornerRect函數(shù)在每個鍵的角落繪制矩形邊緣。這是為了讓我們的鍵盤布局看起來更好看。就像下面的圖片。
          你也可以嘗試更改不同的顏色。
          class Button():
              def __init__(self, pos, text, size=[8585]):
                  self.pos = pos
                  self.size = size
                  self.text = text
          然后我們定義一個名為 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 + 25100 * k + 50], key))
          上面的循環(huán)將遍歷鍵盤按鍵和 Button 對象,我們在其中給出位置和文本作為輸入附加在一個名為 button list 的列表中。稍后我們可以將這個列表傳遞給 draw 函數(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),
          (0255255), cv2.FILLED)
          cv2.putText(img, button.text, (x + 20, y + 65),
          cv2.FONT_HERSHEY_PLAIN, 4, (000), 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),
          (02550), cv2.FILLED)
          cv2.putText(img, button.text, (x + 20, y + 65),
          cv2.FONT_HERSHEY_PLAIN, 4, (000), 4)
          final_text += button.text
          sleep(0.20)

          cv2.rectangle(img, (25,350), (700450),
          (255255255), cv2.FILLED)
          cv2.putText(img, final_text, (60425),
          cv2.FONT_HERSHEY_PLAIN, 4, (000), 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)中,首先我們讀取實時輸入幀并將其存儲在一個名為img的變量中。然后我們將該圖像傳遞給*檢測器.findHands()*以便在幀中找到手。然后在該圖像中,我們需要找到檢測到的手的位置和邊界框信息。
          在這里我們可以找到我們的食指和中指的頂點之間的距離,如果兩者之間的距離小于某個閾值,那么我們就可以輸入我們所指示的字母。
          一旦我們獲得了位置,我們就會遍歷整個位置列表。從該列表中,我們找到按鈕位置和按鈕大小,然后根據(jù)明確定義的方式將其繪制在框架上。
          圖 1:手地標(biāo)模型
          之后,我們需要找到食指和中指的頂點之間的距離。在上圖中,你可以看到我們需要的最高點是點 8 和點 12。因此,我們需要在距離查找函數(shù)中傳遞 8, 12 以獲得它們之間的距離。
          在上面的代碼中,你可以看到 detector.findDistance(),我們通過了 8、12 和圖像來查找距離,并將繪制標(biāo)志設(shè)置為 false,這樣我們就不需要兩點之間的任何線。
          如果點之間的距離非常小,我們將使用 press() 函數(shù)來按下按鍵。在上面的代碼keyboard.press() 中,我們傳遞button.text以顯示按下的鍵。最后,我們在鍵盤布局下方繪制一個小的白色矩形框,以顯示按下的鍵。
          一旦你執(zhí)行了整個代碼,它看起來像這樣。
          將食指和中指靠近特定字母的頂部后,你可以鍵入該字母。
          如果你需要更自定義的鍵盤布局,我們可以使鍵盤布局透明。我們只需要添加一個透明布局函數(shù)并將*draw()函數(shù)替換為transparent_layout()*函數(shù)即可。
          讓我們定義transparent_layout()函數(shù)。下面是函數(shù)的代碼,它采用與draw()函數(shù)相同的輸入。在這里,我們將 numpy 的zero_like()函數(shù)分配給 名為imgNew的變量,并對其執(zhí)行所需的操作,例如獲得角矩形、為每個鍵創(chuàng)建矩形框并將文本放入框內(nèi)。之后,我們將該圖像復(fù)制到一個新變量并創(chuàng)建一個imgNew掩碼,然后我們使用 OpenCV 的*addWeighted()*函數(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]),
                                             (25514430), cv2.FILLED)
                  cv2.putText(imgNew, button.text, (x + 20, y + 65),
                              cv2.FONT_HERSHEY_PLAIN, 4, (000), 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ù),它將如下所示。(下圖)
          完整代碼
          下面是完整的代碼
          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(31280)
          cap.set(4720)

          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)), (25514430), cv2.FILLED)
                  cv2.putText(img, button.text, (x + 20, y + 65),
                              cv2.FONT_HERSHEY_PLAIN, 4, (000), 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]),
                                             (25514430), cv2.FILLED)
                  cv2.putText(imgNew, button.text, (x + 20, y + 65),
                              cv2.FONT_HERSHEY_PLAIN, 4, (000), 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=[8585]):
                  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 + 25100 * 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),
                                        (0255255), cv2.FILLED)
                          cv2.putText(img, button.text, (x + 20, y + 65),
                                      cv2.FONT_HERSHEY_PLAIN, 4, (000), 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),
                                            (02550), cv2.FILLED)
                              cv2.putText(img, button.text, (x + 20, y + 65),
                                          cv2.FONT_HERSHEY_PLAIN, 4, (000), 4)
                              final_text += button.text
                              sleep(0.20)

              cv2.rectangle(img, (25,350), (700450),
                            (255255255), cv2.FILLED)
              cv2.putText(img, final_text, (60425),
                          cv2.FONT_HERSHEY_PLAIN, 4, (000), 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é)
          這是虛擬鍵盤的實現(xiàn),如果你想完善它,你也可以試著添加按鍵聲音,然后我們還可以讓鍵盤布局在框架內(nèi)移動。


          下載1:OpenCV-Contrib擴(kuò)展模塊中文版教程
          在「小白學(xué)視覺」公眾號后臺回復(fù):擴(kuò)展模塊中文教程,即可下載全網(wǎng)第一份OpenCV擴(kuò)展模塊教程中文版,涵蓋擴(kuò)展模塊安裝、SFM算法、立體視覺、目標(biāo)跟蹤、生物視覺、超分辨率處理等二十多章內(nèi)容。

          下載2:Python視覺實戰(zhàn)項目52講
          小白學(xué)視覺公眾號后臺回復(fù):Python視覺實戰(zhàn)項目,即可下載包括圖像分割、口罩檢測、車道線檢測、車輛計數(shù)、添加眼線、車牌識別、字符識別、情緒檢測、文本內(nèi)容提取、面部識別等31個視覺實戰(zhàn)項目,助力快速學(xué)校計算機視覺。

          下載3:OpenCV實戰(zhàn)項目20講
          小白學(xué)視覺公眾號后臺回復(fù):OpenCV實戰(zhàn)項目20講即可下載含有20個基于OpenCV實現(xiàn)20個實戰(zhàn)項目,實現(xiàn)OpenCV學(xué)習(xí)進(jìn)階。

          交流群


          歡迎加入公眾號讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器自動駕駛、計算攝影、檢測、分割、識別、醫(yī)學(xué)影像、GAN、算法競賽等微信群(以后會逐漸細(xì)分),請掃描下面微信號加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請按照格式備注,否則不予通過。添加成功后會根據(jù)研究方向邀請進(jìn)入相關(guān)微信群。請勿在群內(nèi)發(fā)送廣告,否則會請出群,謝謝理解~


          瀏覽 58
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲色情免费电影 | 亚日韩欧美在线视频 | 先锋影音俺去也 | 我要操你国产传媒 | 爱爱中文字幕 |