简体   繁体   English

如何使用 cv2.putText() 覆盖文本

[英]How to overwrite text using cv2.putText()

I have a dict of as show below which are keys for a virtual keyboard with their keyIndex and text values.我有一个如下所示的字典,它是虚拟键盘的键及其 keyIndex 和文本值。

{0: "A", 1: "B", 2: "C", 3: "D"}

I can display these values on a keyboard as below我可以在键盘上显示这些值,如下所示

keyboard = np.zeros((400, 1600, 3), np.uint8)

#key values
width = 100
height = 100
th = 3 # thickness
cv2.rectangle(keyboard, (x + th, y + th), (x + width - th, y + height - th), (246, 248, 250), -1) cv2.line(keyboard, (800, 0), (800, 400), (255, 255, 255), 3)

#Inside the rectangle now we put the letter. So we define the sizes and style of the text and we center it.
#Text settings
font_letter = cv2.FONT_HERSHEY_PLAIN
font_scale = 5
font_th = 4
text_size = cv2.getTextSize(text, font_letter, font_scale, font_th)[0]
width_text, height_text = text_size[0], text_size[1]
text_x = int((width - width_text) / 2) + x
text_y = int((height + height_text) / 2) + y
cv2.putText(keyboard, text, (text_x, text_y), font_letter, font_scale, (0, 0, 0), font_th)

But I want the user to be able to narrow down the set of keys by pressing 'l' or 'r' where left would display only A and B and right would display C and D where the user could then narrow it down again to a single character.但我希望用户能够通过按“l”或“r”来缩小键集,其中左侧将仅显示 A 和 B,右侧将显示 C 和 D,然后用户可以再次将其缩小到单个字符。

for k in currentSet:
    key(k, currentSet[k])

if currentSet == fullSet and kb.is_pressed('l'):
        currentSet = left
        for k in currentSet:
            key(k, currentSet[k])
elif currentSet == fullSet and kb.is_pressed('r'):
        currentSet = right
        for k in currentSet:
            key(k, currentSet[k])

cv2.imshow("KEYBOARD",keyboard)
cv2.waitKey(0)
cv2.destroyAllWindows()

The above code allows me to display a full set but when I try and press l or r, the program closes.上面的代码允许我显示完整的集合,但是当我尝试按 l 或 r 时,程序关闭。

Any help would be appreciated.任何帮助,将不胜感激。

EDIT: Updated loop编辑:更新循环

currentSet = fullSet
keyboard = np.zeros((400, 1600, 3), np.uint8)


while True:

    keyboard[:] = (255, 255, 255)

    for k in currentSet:
        key(k, currentSet[k])


    cv2.imshow("Keyboard", keyboard)

    kb = cv2.waitKey(0) & 0xff

    if kb == ord('q'):
        break

    if currentSet == fullSet:
        if kb == ord('l'):
            currentSet = left
            print(currentSet)
        if kb == ord('r'):
            currentSet = right
            print(currentSet)

    elif currentSet == left:
        if kb == ord('l'):
            currentSet == left_left
            print(currentSet)
        if kb == ord('r'):
            currentSet == left_right
            print(currentSet)

    elif currentSet == right:
        if kb == ord('l'):
            currentSet == right_left
            print(currentSet)
        if kb == ord('r'):
            currentSet == right_right
            print(currentSet)       


cv2.destroyAllWindows()

This is the keyboard displaying fullSet这是显示 fullSet 的键盘全套

I then press 'l' to select left然后我按“l”到 select 离开左置

Why am I unable to then press 'l' again to set currentSet to left_left?为什么我无法再按“l”将 currentSet 设置为 left_left?

It runs all your is_pressed before you even touch keyboard, and it stops on waitKey(0) .它会在您触摸键盘之前运行您所有的is_pressed ,并在waitKey(0)上停止。

And when you press any key (not only l or r ) then it leaves waitKey() and it goes to cv2.destroyAllWindows() and it finishes program.当您按下任意键(不仅是lr )时,它会离开waitKey()并转到cv2.destroyAllWindows()并完成程序。

You have to run it in loop.你必须循环运行它。

  • clean screen清洁屏幕
  • draw current set of keys绘制当前键集
  • waitKey and get pressed key waitKey并获得按下的键
  • check key and change set检查密钥和更改集
  • go back to the beginning go回到开头

Minimal working code最小的工作代码

  • q - quit q - 退出
  • l or left arrow - left part lleft arrow - 左侧部分
  • r or right arrow - right part rright arrow - 右侧部分
  • u or up arrow - back to bigger set uup arrow - 返回更大的集合
  • space or down arrow - copy selected char to text spacedown arrow - 将选定的字符复制到文本

在此处输入图像描述

import cv2
import numpy as np

# --- constans ---  # PEP8: `UPPER_CASE_NAMES` for constants

KEY_WIDTH  = 100
KEY_HEIGHT = 100
TH = 3 # thickness

#Inside the rectangle now we put the letter. So we define the sizes and style of the text and we center it.
#Text settings
FONT_LETTER = cv2.FONT_HERSHEY_PLAIN
FONT_SCALE = 5
FONT_TH = 4

# --- functions ---  # PEP8: `lower_case_names` (verbs) for functions
                     
def draw_key(keyboard, text, x, y):
    cv2.rectangle(keyboard, (x + TH, y + TH), (x + KEY_WIDTH - TH, y + KEY_HEIGHT - TH), (246, 248, 250), -1)

    text_w, text_h = cv2.getTextSize(text, FONT_LETTER, FONT_SCALE, FONT_TH)[0]
    text_x = x + int((KEY_WIDTH  - text_w) / 2)
    text_y = y + int((KEY_HEIGHT + text_h) / 2)

    cv2.putText(keyboard, text, (text_x, text_y), FONT_LETTER, FONT_SCALE, (0, 0, 0), FONT_TH)

# --- main ---  # PEP8: `lower_case_names` (nouns) for variables

full_set  = ['A', 'B', 'C', 'D']
left_set  = ['A', 'B']
right_set = ['C', 'D']

left_left_set  = ['A']
left_right_set = ['B']

right_left_set  = ['C']
right_right_set = ['D']

current_set = full_set

keyboard = np.zeros((400, 1600, 3), np.uint8)

text = []
while True:

    # clean it before drawing new keys    
    keyboard[:] = (0,0,0)

    # draw line    
    cv2.line(keyboard, (800, 0), (800, 400), (255, 255, 255), 3)
        
    # draw keys
    x = 0
    y = 0
    for char in current_set:
        draw_key(keyboard, char, x, y)
        x += KEY_WIDTH + TH
        
    # draw text
    x = 800 + TH
    y = 0
    for char in text:
        draw_key(keyboard, char, x, y)
        x += KEY_WIDTH + TH
    
    # display it 
    cv2.imshow("KEYBOARD", keyboard)
    
    # wait for key
    kb = cv2.waitKey(0) & 0xff
    #print(kb)
    
    # Q - exit
    if kb == ord('q'):
        break
    
    if current_set == full_set:
        if kb == ord('l') or kb == 81: # or left arrow
            current_set = left_set
        if kb == ord('r') or kb == 83: # or right arrow
            current_set = right_set

    elif current_set == left_set:
        if kb == ord('u') or kb == 82: # or up arrow
            current_set = full_set
        if kb == ord('l') or kb == 81: # or left arrow
            current_set = left_left_set
        if kb == ord('r') or kb == 83: # or right arrow
            current_set = left_right_set

    elif current_set == right_set:
        if kb == ord('u') or kb == 82: # or up arrow
            current_set = full_set
        if kb == ord('l') or kb == 81: # or left arrow
            current_set = right_left_set
        if kb == ord('r') or kb == 83: # or right arrow
            current_set = right_right_set

    elif current_set in (left_left_set, left_right_set):
        if kb == ord('u') or kb == 82: # or up arrow
            current_set = left_set
        if kb == ord(' ') or kb == 84: # or down arrow
            print(current_set[0])
            text.append( current_set[0] )

    elif current_set in (right_left_set, right_right_set):
        if kb == ord('u') or kb == 82: # or up arrow
            current_set = right_set
        if kb == ord(' ') or kb == 84: # or down arrow
            print(current_set[0])
            text.append( current_set[0] )

# - end -

cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM