sam
sam

Reputation: 494

Align text in the putText() in OpenCV

I have a list of strings like this

['standUp', 'front', 'lookU', 'lookF', 'lookDF', 'HandOnHipR']

I want to print it in the following manner on my video:

['standUp', 
'front', 
'lookU', 
'lookF', 
'lookDF', 
'HandOnHipR']

I have tried this:

offset = 1
x, y = 5, 400
for idx, list in enumerate(lbls):
    cv2.putText(frame, str(lbls), (x, y+offset*idx), font, 1, (0, 0, 255), 1)

The label is a list of lists mentioned at the top. I am confused with the org argument in the putText().

Upvotes: 11

Views: 33493

Answers (2)

A Kruger
A Kruger

Reputation: 2419

The org argument is used to choose the position of the text in the image, and the bottom left corner of the text is put at the point org. If you want the text more to the right, you increase the x value, and if you want it lower, you increase the y value. So each line will be lower because you are increasing the y value with the offset*idx.

As you have it, the second argument str(lbls) will print out the entire array, that should be changed to the variable for the single element, which you have as list. (However, it's best not to use list as a variable because it will replace the built-in function list(). I'll use lbl instead.)

Here's an example where the words in the list are printed:

import cv2
import numpy as np
import matplotlib.pyplot as plt

frame = np.ones([400,400,3])*255
lbls = ['standUp', 'front', 'lookU', 'lookF', 'lookDF', 'HandOnHipR']

offset = 35
x,y = 50,50
for idx,lbl in enumerate(lbls):
    cv2.putText(frame, str(lbl), (x,y+offset*idx), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)

plt.imshow(frame)
plt.show()

enter image description here

If you want to have the brackets and punctuation like you show above, you could possibly print them manually. The first and last elements would need to be printed separately:

cv2.putText(frame, '[\''+str(lbls[0])+'\',', (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
for idx,lbl in enumerate(lbls[1:-1]):
    cv2.putText(frame, '\''+str(lbl)+'\',', (x,y+offset*(idx+1)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)
cv2.putText(frame, '\''+str(lbls[0])+'\']', (x,y+offset*(idx+2)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 2)

enter image description here

Otherwise, you could change the elements to actually include the punctuation in them.

Upvotes: 6

Dodge
Dodge

Reputation: 3309

Parameters are as follows:

cv2.putText(img, text, (org), font, fontScale, color, thickness, linetype)

img: your image
text: a string of text to print on image
org: bottom-left corner of the text string in the image (x,y)
font: font type
fontScale: font scale
color: text color (B,G,R)
thickness: text line thickness
lineType: line type (8)

Here is an example that may help.

Test image:

enter image description here

Code:

import cv2

lbls = ['standUp', 'front', 'lookU', 'lookF', 'lookDF', 'HandOnHipR']

img = cv2.imread("road.jpg")

h,w,c = img.shape

offset = 0

font = cv2.FONT_HERSHEY_SIMPLEX

for itr, word in enumerate(lbls):
    offset += int(h / len(lbls)) - 10
    cv2.putText(img, word, (20, offset), font, 1, (0, 255, 0), 3)

cv2.imwrite("road_OUT.jpg", img)

Result:

enter image description here

Upvotes: 11

Related Questions