简体   繁体   English

cv2.putText() 不使用可变参数

[英]cv2.putText() not working with variable parameters

I have project where there is a string named "sen" which is changing with time as user presses some key.我有一个项目,其中有一个名为“sen”的字符串,它随着用户按下某个键而随时间变化。

Code:代码:

sen = ""
k = 0 
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)

    some code ...

    key = cv2.waitKey(1) & 0xFF 
    if key == ord('q'):
        break
    elif key == ord('d'):
        sen += predict(mask) # predict(mask) returns a character
        k += 20

    x_org = 300-k 
    frame = cv2.putText(frame, sen, (x_org, 450), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 1, cv2.LINE_AA)


So basically what I am doing is that whenever user presses 'd' key, I am adding some character in string "sen" and then I defined x_org = 300-k, which I am passing to cv2.putText() as ax coordinate so that whenever some character is being added to a string "sen" the text shown on the screen will shift to left by 20px as 'k' is incrementing by 20 whenever the user press 'd'.所以基本上我正在做的是每当用户按下'd'键时,我都会在字符串“sen”中添加一些字符,然后我定义 x_org = 300-k,我将其传递给 cv2.putText() 作为轴坐标所以每当将某个字符添加到字符串“sen”时,屏幕上显示的文本将向左移动 20px,因为每当用户按下“d”时,“k”都会增加 20。

When I execute my code, everything works fine like new character gets appended after pressing 'd' key but text doesn't shifts towards left when new character is appended.当我执行我的代码时,一切正常,就像在按下“d”键后添加了新字符,但是在添加新字符时文本不会向左移动。

So what could be the problem here and how to resolve this problem??那么这里可能是什么问题以及如何解决这个问题?

You have to add 20 to k to shift sen left 20 pixels您必须将 20 添加到 k 才能将 sen 向左移动 20 个像素

k = + 20 will keep at same position always, It will be k += 20 k = + 20 将始终保持相同的 position,它将是 k += 20

Updated code更新代码

import cv2
sen = ""
k = 0 
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)

    some code ...

    key = cv2.waitKey(1) & 0xFF 
    if key == ord('q'):
        break
    elif key == ord('d'):
        sen += predict(mask) # predict(mask) returns a character
        k += 20

    x_org = 300-k 
    frame = cv2.putText(frame, sen, (x_org, 450), cv2.FONT_HERSHEY_SIMPLEX,1, (0, 0, 0), 1, cv2.LINE_AA)
    cv2.imshow('frame', frame)

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

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