user18573205
user18573205

Reputation: 21

cv2.putText not showing Armenian letters on screen in python OpenCV

I am coming to a problem that I am trying to output Armenian letter when I am using my hand which is a sign language application. So, then I am getting "???" why signing a letter. I believe the putText() of opencv is not either deprecated or having issues or maybe not implementing the proper way. Please help. thanks!

Here is my code:

 dict_letter = {0: 'Ա', 1: 'Բ', 2: 'Գ', 3: 'Դ', 4: 'Ե', 5: 'Զ', 6: 'Է', 7: 'Ը', 8: 'Թ', 9: 'Ժ', 10: 'Ի', 11: 'Լ', 12: 'Խ', 13: 'Ծ', 14: 'Կ', 15: 'Հ',
                16: 'Ձ', 17: 'Ղ', 18: 'Ճ', 19: 'Մ', 20: 'Յ', 21: 'Ն', 22: 'Շ', 23: 'Ո', 24: 'Չ', 25: 'Պ', 26: 'Ջ', 27: 'Ռ', 28: 'Ս', 29: 'Վ', 30: 'Տ', 31: 'Ր',
                 32: 'Ց', 33: 'Փ', 34: 'Ք', 35: 'Ֆ'}

img = cv2.putText(image_hand,
                                   f"{dict_letter[pred]} - {str(probabs)}",
                                  (x_square, y_square - 5), cv2.FONT_HERSHEY_SIMPLEX,
                                   0.6, (0,0,0), 2)

Using Pillow:

            cv2_im_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    pil_im = Image.fromarray(cv2_im_rgb)

                    draw = ImageDraw.Draw(pil_im)

# Choose a font
                    font = ImageFont.truetype("/arnamu.ttf", 50)

# Draw the text
                    draw.text((0, 0), dict_letter[pred], font=font)

Upvotes: 2

Views: 822

Answers (2)

Esraa Abdelmaksoud
Esraa Abdelmaksoud

Reputation: 1699

The function putText() in OpenCV uses Hershey fonts. This is the full set of available characters in Hershey. If your special language characters are not available in the set, it means this is the reason why it doesn't work.

Here's how you can use Pillow to add text instead of OpenCV.

from PIL import Image
from PIL import ImageDraw
 
# Open an Image
img = Image.open('my_img.png')
 
I1 = ImageDraw.Draw(img)
 
# Add Text to image
I1.text((28, 36), "nice image", fill=(255, 0, 0))
 
# Display edited image
img.show()
 
# Save the edited image
img.save("my_img2.png")

Upvotes: 1

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15518

The builtin vector fonts don't come with those glyphs. Or at least you can't rely on that. There are extensions of the Hershey vector fonts for some unicode. I don't know if OpenCV ships with those.

You can try the freetype module from the contrib repository. It has bugs too. Right-to-left script is/was broken. Random usage example: https://github.com/opencv/opencv/issues/14579 (createFreeType2, loadFontData on the instance, then use .putText)

In OpenCV v5 (no release date yet) there will be proper font support.

Upvotes: 1

Related Questions