PATHIK GHUGARE
PATHIK GHUGARE

Reputation: 155

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.

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 a x 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' .

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.

So what could be the problem here and how to resolve this problem ??

Upvotes: 0

Views: 1167

Answers (1)

Krunal Sonparate
Krunal Sonparate

Reputation: 1142

You have to add 20 to k to shift sen left 20 pixels

k = + 20 will keep at same position always, It will be 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)

Upvotes: 1

Related Questions