opencv-python 4.8.1.78 - не хочет работать с русскими именами файлов в пути. Это както можно поправить?
Приветствую всех специалистов! Подскажите пожалуйста: При запуске read_qr_code("./QR/data/участок.png") вылетает ошибка, как видно беда с именем файла, может както надо конвертировать?
[ WARN:[email protected]] global loadsave.cpp:248 cv::findDecoder imread_('./QR/data/╤Г╤З╨░╤Б╤В╨╛╨║.png'): can't open/read file: check file path/integrity error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\qrcode.cpp:31: error: (-215:Assertion failed) !img.empty() in function 'cv::checkQRInputImage'
# pip3 install opencv-python qrcode pyzbar numpy Pillow
import cv2
import os
import qrcode
import numpy as np
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
img_path = "./QR/data/участок.png"
# =========================================================================
def generate_qr_code(data, img_path):
img = qrcode.make(data)
img.save(img_path)
qr = qrcode.QRCode(version=2, box_size=10, border=14)
qr.add_data(data)
qr.make()
# print("The shape of the QR image:", np.array(qr.get_matrix()).shape)
img = qr.make_image(fill_color="black", back_color=(255, 95, 235))
# добавляем надпись в изображение
I1 = ImageDraw.Draw(img)
myFont = ImageFont.truetype('./QR/Roboto-Bold.ttf', size=60)
I1.text((10, 10), data, font=myFont, fill=(0, 0, 0)) # текст из DATA
img.save(img_path)
# ========================================================================
# DECODE QR-CODE
# ========================================================================
def read_qr_code(img_path):
img = cv2.imread(img_path)
detector = cv2.QRCodeDetector()
try:
data, bbox, _ = detector.detectAndDecode(img)
except Exception as e:
print('error: ', e)
return None
# if there is a QR code
if bbox is not None:
print(f"QRCode data:\n{data}")
return data
else:
print("QRCode not found")
return None
# generate_qr_code("TESTisTEXT", "./QR/data/участок.png")
# read_qr_code("./QR/data/участок.png")
Ответы (2 шт):
Можно через open который нормально работает с Unicode
import cv2
import numpy
file = open(u'C:\\Users\\Amgarak\\тест\\тест.jpeg', "rb")
bytes = bytearray(file.read())
numpyarray = numpy.asarray(bytes, dtype=numpy.uint8)
img = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
cv2.imshow('Test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Допустим:
file = "d:\участок/1 участок\стройбригада//фотография участка.jpg"
Имя файла (элементарные правила предотвращения проблем):
file = r"d:\участок/1 участок\\стройбригада//фотография участка.jpg" - так безопаснее.
file = str(pathlib.Path(file).resolve()) - результат: 'D:\\участок\\1 участок\\стройбригада\\фотография участка.jpg'