cv2.cudacodec.createVideoReader deal with unicode characters in video path

Hi, everyone, I’m using cv2.cudacodec.createVideoReader(r’video.mp4’) function, I have Unicode characters in my video path, like Chinese characters, and it’ll encounter this error

video_gpu = cv2.cudacodec.createVideoReader(video_path, params=params)
cv2.error: OpenCV(4.7.0) D:\opencv_build\opencv_contrib-4.7.0\modules\cudacodec\src\cuvid_video_source.cpp:66: error: (-217:Gpu API call) CUDA_ERROR_FILE_NOT_FOUND [Code = 301] in function 'cv::cudacodec::detail::CuvidVideoSource::CuvidVideoSource'

cv2.VideoCapture works fine with Unicode path, but I need to accelerate video encoding and decoding. I just found some solutions to deal with reading and writing images with Unicode characters in the path, but didn’t find anything about this function, are there any solutions for this problem?

anything help? I’ll be very appreciate

Just to confirm if you supply the same video without unicode chars to createVideoReader does it work?

yes, delete the Chinese characters in the video path works fine

Can you supply the filename you are using and I will try to take a look.

sure, video_path is r’D:\test\2231221\CU测试汉字_K988+11\K0+000.mp4’, I know there’s ‘+’ there, but I tested, it’s Chinese characters which really caused the problem. and thank you very much!

known issue with OpenCV in general. it can’t handle unicode in file names at runtime. this usually shows up with non-CUDA imread() uses but apparently it also affects everything else in OpenCV.

I don’t know when they’ll figure out a way to fix this, or if they have already.

You could try the v5 branch of OpenCV. it’s not released yet. maybe it contains a fix.

I am not familiar with using unicode however in python I can open a file with the path you supplied with both cv::VideoCapture and cudacodec::VideoReader in OpenCV 4.8.0. e.g.

cap = cv2.VideoCapture(‘d:/media/CU测试汉字_K988+11/K0+000.265’)
and
reader = cv2.cudacodec.createVideoReader(‘d:/media/CU测试汉字_K988+11/K0+000.265’)

As cudacodec::VideoReader uses cv::VideoCapture to open video files I am suprised that for you one worked and the other did not.

Maybe there is an error somewhere else, can you supply the exact code you are using and I will try it on my setup?

issue may potentially be modulated by windows system settings relating to unicode. I think they’re introducing more and more inherent unicode support into windows that wasn’t there before. this may also affect old APIs, which OpenCV is likely to use.

sorry for the late replay, here is my code snippet

    cv2.cuda.setDevice(gpu_num)
    video_path = r'D:\test\2231221\CU测试汉字_K988+11\K0+000.mp4'

    params = cv2.cudacodec.VideoReaderInitParams()
    params.targetSz = (1920, 1080)
    video_gpu = cv2.cudacodec.createVideoReader(video_path, params=params)
    video_gpu.set(cv2.cudacodec.COLOR_FORMAT_BGR)

    encoder_params_in = cv2.cudacodec.EncoderParams()
    stream = cv2.cuda.Stream()
    out = cv2.cudacodec.createVideoWriter('{}_output_.h264', (1920, 1080), cv2.cudacodec.HEVC, fps=25,
                                          colorFormat=cv2.cudacodec.COLOR_FORMAT_BGR,
                                          params=encoder_params_in,
                                          stream=stream)
    while True:
        ret, frame = video_gpu.nextFrame()
        if not ret:
            break
   
        out.write(frame)
    out.release()

and my Opencv is 4.7.0, I noticed you’re runing it both fine under OpenCV 4.8.0, maybe I should try it under 4.8.0 too

thank you, I’ll try it out later using v5 branch

That’s working for me on 4.8.0 however as @crackwitz said it could be related to windows system settings rather than the OpenCV version.

so does this means I should try later version of windows system

Did you try a later version of OpenCV? Can you share the output you are getting in the console, info/warnings/errors etc.

I am not sure what to suggest as I am not familiar with unicode support on Windows, maybe @berak, @laurent.berger or @crackwitz can offer some assistance?

I have no clue how to dig into this.

Check whether this is reproducible with plain imread(), no CUDA. whatever outcome, that’s a vital bit of information.

then check existing issues (CUDA issues would be on the opencv_contrib repo). if there’s nothing suitable, create a new one.

sorry I didn’t try later verison of opencv, I tried build opencv 4.8 with cuda, but encounter error LNK2001 unresolved external symbol error, and didn’t find a solution yet

I use imread to read an image path with Unicode characters returns None, but this seems normal? and I’ll check the issues, thanks a lot

If you give me more details on the error I might be able to help. Did you update both repos to 4.8.0? If you are building again I would checkout the 4.9.0. tag from both repos.

I successfully built OpenCV4.8 eventually, and it works fine with the Unicode path, turns out OpenCV version does affect. thanks a lot for your help!

1 Like