Ask Your Question
0

Problem with imread function

asked 2019-01-20 05:20:51 -0600

Hello, I start with opencv with python and I do not suceed to load an image with the imread function. I try with the filename, with the path of the image, with \,\,/ in the path but nothing works. I have alwars error like :

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 12-13: truncated \UXXXXXXXX escape

or

Traceback (most recent call last): File "D:\Users\Rémy\Desktop\tipe\rec forme.py", line 7, in <module> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Someone can help me please ? Thanks (Sorry for my bad english)

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2019-01-20 05:48:26 -0600

berak gravatar image

updated 2019-01-20 05:50:43 -0600

if you have a file path like:

"D:\Users\Rémy\Desktop\tipe\my.png"

python will interpret the \U part (and the following 4 bytes) as a unicode escape sequence, also the \t will be interpreted as a TAB -- no, you don't want that ;)

either use double backslashes:

"D:\\Users\\Rémy\\Desktop\\tipe\\my.png"

or single forward ones (even on windows !)

"D:/Users/Rémy/Desktop/tipe/my.png"

or make a "raw" string of it:

r"D:\Users\Rémy\Desktop\tipe\my.png"

then, you ALWAYS have to CHECK the outcome from imread():

img = imread(...)
if not img:
    # fail, you can't go on !

last, just saying, even IF your name is Rémy, it's rarely a good idea, to have such things as an é in a filepath, also rather try to avoid spaces in your filenames (it's all "asking for trouble" !)

edit flag offensive delete link more

Comments

I try the three and I do not have a problem with unicode but I have the message :

Traceback (most recent call last): File "D:\Users\Rémy\Desktop\tipe\rec forme.py", line 7, in <module> gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

and the image is not loaded, how can I fix it ?

its_ghost gravatar imageits_ghost ( 2019-01-20 16:38:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-20 05:20:51 -0600

Seen: 4,202 times

Last updated: Jan 20 '19