简体   繁体   English

cv2.putText 未在屏幕上显示亚美尼亚字母 python OpenCV

[英]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.我遇到了一个问题,当我使用手语应用程序时,我正在尝试 output 亚美尼亚字母。 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.我相信 opencv 的 putText() 既没有被弃用,也没有出现问题,或者可能没有以正确的方式实施。 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)

The builtin vector fonts don't come with those glyphs.内置矢量 fonts 不带有这些字形。 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.有一些 unicode 的 Hershey 向量 fonts 的扩展。我不知道 OpenCV 是否附带这些。

You can try the freetype module from the contrib repository.您可以尝试contrib存储库中的freetype模块。 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 )随机使用示例: https://github.com/opencv/opencv/issues/14579createFreeType2 ,在实例上加载loadFontData ,然后使用.putText

In OpenCV v5 (no release date yet) there will be proper font support.在 OpenCV v5(尚未确定发布日期)中将提供适当的字体支持。

The function putText() in OpenCV uses Hershey fonts . OpenCV 中的 function putText() 使用好时 fonts This is the full set of available characters in Hershey.这是 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.下面介绍如何使用 Pillow 代替 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")

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

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