Encodings do not work in an Opencv file in Python 3

Asked

Viewed 279 times

1

I followed the face detection tutorial using Opencv and wrote in Python 3. This file is not used print. I already know that Python 3 is international, is compatible with UTF-8 and does not need encodings. But even so, there was no accent on an image. Note that the file is saved in UTF-8.

I put it like this:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- coding: latin-1 -*-
# -*- coding: iso-8859-15 -*-
import os
import sys

# import the necessary packages
import argparse
import cv2

I’ve put three encodings to work on three different operating systems.

I changed from English to Portuguese, writing a sentence "It’s a cat":

# loop over the cat faces and draw a rectangle surrounding each
for (i, (x, y, w, h)) in enumerate(rects):
    cv2.rectangle(image, (x, y), (x + w, y + h), (140, 175, 84), 2)
    cv2.putText(image, "É um gato  # {}".format(i + 1), (x, y - 10),
             cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)  #5faf54

I ran the integrated terminal of Visual Studio Code on Ubuntu 16.04. It is no use to suggest that you run on the native terminal because the same thing happened.

I also tried to:

cv2.encode('utf-8')

And it didn’t work.

Appeared "?? a cat #1" in an image when running and compiling:

Exemplo

1 answer

2


As far as I know, there is no implementation in the Puttext function for Unicode support and special characters, so the problem is not with python.

A widely used feature, however, for this and other special character prints is the draw.text function of Pillow (PIL).

Which it uses, of standard a Unicod source.

PS: The Encode function of Opencv (now imencode), refers only to the encoding of the image itself. Documentation: Here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.