Keyboard waiting in the opencv library and operations similar to hands-off detection

When mentioning the functions related to keys in opencv, the first thing that comes to mind must be cv2.waitKey() and cv2.waitKeyEx().

Let me talk about the difference between the two first. waitKey only supports returning the ASCII codes corresponding to some keys, while waitKeyEx supports returning the codes of all keys. In fact, it is the difference between full and incomplete. Generally speaking, we have waitKey is enough. If an unsupported key is pressed while using waitkey(), the return value of the cv2.waitKey() function should be 0. For details, you can check the official documentation of opencv.

OpenCV: High-level GUI

 The return value of cv.waitKey() is a number. If a keyboard key is pressed within the waiting time, the return value is the ASCII code corresponding to the key; if the waiting time is exceeded or the mouse is clicked to close, the return value is - 1.

What still needs to be mastered is the ord() function, which is a built-in function of python. The function is to return the ASCII code corresponding to a character, which can only be a character, not a string. For example:

num = ord('a')
print(num)

Here the value of the variable num is the ASCII code corresponding to a, which is 97.

In the following code, I want to control the speed of the car forward or turn through the keyboard. The variables speed and turn are the duty cycle of the motor forward and turn PWM pulse width modulation signals. As for how to deal with this PWM signal, we have a motor The drive will be shot. Here only use the WS key to control the forward and backward speed and the AD key to control the angle of turning left and turning right. As for the implementation of PWM, it is implemented by calling the GPIO of the Raspberry Pi, which is not the focus of this blog.

import cv2.cv2 as cv

img = cv.imread('xxx.jpg')
cv.imshow('t', img)
speed = 5       # 初始速度
turn = 0        # 初始转向
while True:
    rate = cv.waitKey()
    print(rate)
    if rate == ord('w'):
        speed = speed + 1
        print('前进速度加一,当前前进速度是::{:.0f}'.format(speed))
        while rate == ord('w'):     # 防止长按此键,起和松手检测一样的作用
            rate = cv.waitKey()
    elif rate == ord('s'):
        speed = speed - 1
        print('前进速度减一当前前进速度是::{:.0f}'.format(speed))
        while rate == ord('s'):
            rate = cv.waitKey()
    elif rate == ord('a'):
        turn = turn - 1
        print('左转加一,当前转向速度是::{:.0f}'.format(turn))
        while rate == ord('a'):
            rate = cv.waitKey()
    elif rate == ord('d'):
        turn = turn - 1
        print('右转加一,当前转向速度是::{:.0f}'.format(turn))
        while rate == ord('d'):
            rate = cv.waitKey()

    elif rate == ord('t'):
        exit()

Note that to run this code, we need to put the focus of the mouse on the picture displayed by the cv library before pressing the keyboard button.

But this writing can realize the hand-off detection, that is, to prevent the pwm output duty cycle from increasing or decreasing due to a long press of a key. But there will also be a problem that after pressing the wasd key once, you need to press another key different from the one just now to jump out of the while loop of the release detection to perform the second wasd operation. Due to my limited level, and my actual needs are to combine the opencv library, so I can only think of such a solution when I only use the opencv-python library. Although it is a bit awkward to use, the needs to be realized at any rate hahahaha .

Here are some ideas for improvement, for example, you can combine the time library, you can also use the keyboard library, or refer to the solution of keyboard control car in ros. After the later improvement, I will continue to share with you.

Reprinting is prohibited without the author's permission. If you need to reprint, please indicate the source and contact the author.

Guess you like

Origin blog.csdn.net/weixin_63268005/article/details/129743478