简体   繁体   中英

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

Using glob in python3.6.9 , images names are like that

在此处输入图片说明

Causing Segmentation Fault when I read them with cv2.imread

How can I solve this problem?.

This is not an OpenCV issue, rather it's getting the correct string representation of your file name. 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 ? I'll follow up if you're sure.

比较

Follow up: So if you do these two commands, do you also get a 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.

If not, what does python3 -c 'import subprocess; subprocess.call(["ls"])' python3 -c 'import subprocess; subprocess.call(["ls"])' give you? 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. 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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