[英]Read/load images with unicode characters in image path (Chinese, Japanese, Korean) with Python OpenCV
I have a directory of images, each image has a Chinese character in it's name.我有一个图像目录,每个图像的名称中都有一个汉字。 I am trying to list all the images, loop on the list, read, and display each image.我正在尝试列出所有图像,在列表上循环,阅读并显示每个图像。
Images Path is something like that https://github.com/sirius-ai/LPRNet_Pytorch/tree/master/data/test图像路径类似于https://github.com/sirius-ai/LPRNet_Pytorch/tree/master/data/test
Using glob
in python3.6.9 , images names are like that在python3.6.9 中使用glob
,图像名称是这样的
Causing Segmentation Fault
when I read them with cv2.imread
当我用cv2.imread
读取它们时导致Segmentation Fault
How can I solve this problem?.我怎么解决这个问题?。
This is not an OpenCV
issue, rather it's getting the correct string representation of your file name.这不是OpenCV
问题,而是获取文件名的正确字符串表示形式。 We could delve into how all that works, but such issue should be fixed in python3
, so first: are you sure you are using python3
and not python2
?我们可以深入研究所有这些是如何工作的,但是这样的问题应该在python3
,所以首先:你确定你使用的是python3
而不是python2
吗? I'll follow up if you're sure.如果你确定,我会跟进。
Follow up: So if you do these two commands, do you also get a Seg Fault?跟进:所以如果你执行这两个命令,你是否也会得到 Seg Fault?
wget https://raw.githubusercontent.com/sirius-ai/LPRNet_Pytorch/master/data/test/%E4%BA%ACPL3N67.jpg
python3 -c 'import cv2; import glob; print(cv2.imread(glob.glob("*")[0]).shape)'
You should get (800,800,3) printed to the screen.您应该将 (800,800,3) 打印到屏幕上。
If not, what does python3 -c 'import subprocess; subprocess.call(["ls"])'
如果没有, python3 -c 'import subprocess; subprocess.call(["ls"])'
是什么python3 -c 'import subprocess; subprocess.call(["ls"])'
python3 -c 'import subprocess; subprocess.call(["ls"])'
give you? python3 -c 'import subprocess; subprocess.call(["ls"])'
给你? It could be the files you downloaded just are saved with the "byte names".可能是您刚刚下载的文件以“字节名称”保存。
One method is to use np.fromfile()
to convert the image to a 1-D ndarray
then use cv2.imdecode()
to convert it to the normal 3-D shaped BGR
image format.一种方法是使用np.fromfile()
将图像转换为 1-D ndarray
然后使用cv2.imdecode()
将其转换为普通的 3-D 形状的BGR
图像格式。 Depending on your image format (if it has transparency), you can change the decoding flag.根据您的图像格式(如果它具有透明度),您可以更改解码标志。 Take a look here for a full list of flags. 在这里查看完整的标志列表。
import cv2
import numpy as np
import glob
for path in glob.glob("images/*.jpg"):
# Image is in BGR format
image = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
cv2.imshow('image', image)
cv2.waitKey(1000)
Note: It seems to work with any unicode image file (Chinese, Japanese, Korean, Russian, etc.)注意:它似乎适用于任何 unicode 图像文件(中文、日文、韩文、俄文等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.