Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Virtual Keyboard with OpenCV

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to use OpenCV to develop a virtual keyboard", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use OpenCV to develop a virtual keyboard.

Introduction

OpenCV is the most popular computer vision task library. It is a cross-platform open source library for machine learning and image processing. It is used to develop real-time computer vision applications.

CVzone is a computer vision package that uses OpenCV and Media Pipe libraries as its core, making it easy for us to run, such as hand tracking, face detection, facial logo detection, pose estimation, as well as image processing and other computer vision-related applications.

Using OpenCV to realize Virtual Keyboard

Let's create a virtual keyboard.

First, let's install the required modules.

Pip install numpy pip install opencv-python pip install cvzone pip install pynput uses OpenCV to import libraries for virtual keyboards

Now let's import the required modules

Import cv2import cvzonefrom cvzone.HandTrackingModule import HandDetectorfrom time import sleepimport numpy as npfrom pynput.keyboard import Controller

Here we import the HandDetector module from cvzone.HandTrackingModule, and then we need to import Controller from pynput.keyboard in order for the virtual keyboard to work.

Cap = cv2.VideoCapture (0, cv2.CAP_DSHOW) cap.set (3, 1280) cap.set (4720)

Now let's get real-time input from 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 ="

We initialize the HandDetector with a detection confidence of 0.8 and assign it to the detector.

Then we create a list array based on the layout of the keyboard and define an empty string to store the typed keys.

Define the rendering function 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

Initialize the keyboard controller and define a function called draw (), which takes two parameters, the image and button list, and returns the image. Inside the draw () function, we use cvzone's cornerRect function to draw a rectangular edge at the corner of each key. This is to make our keyboard layout look better. Just like the picture below.

You can also try to change different colors.

Class Button (): def _ _ init__ (self, pos, text, size= [85,85]): self.pos = pos self.size = size self.text = text

Then we define a class called Button () and provide the location, text, and size as input so that we can arrange the keyboard keys in a clearly defined order.

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

The above loop iterates through keyboard keys and Button objects, where we give the location and text to be appended as input to a list called button list. We can pass this list to the draw function later to draw on top of our real-time framework.

The main program of virtual keyboard using OpenCV

Here comes the important part.

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]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report