Hello,
First, when reading a video, this warning occured, giving me a None value instead of a frame :
[ WARN:0@379.898] global cap_ffmpeg_impl.hpp:1541 grabFrame packet read max attempts exceeded, if your video have multiple streams (video, audio) try to increase attempt limit by setting environment variable OPENCV_FFMPEG_READ_ATTEMPTS (current value is 4096)
I wanted to create the environment variable inside my Python file to be machine-independant so I followed the instructions in page OpenCV: OpenCV environment variables reference
When I executed this code in the given page :
import os
os.environ["MY_ENV_VARIABLE"] = True
import cv2 # variables set after this may not have effect
It raised this error :
Traceback (most recent call last):
File "C:\Users\...\script.py", line 2, in <module>
os.environ["MY_ENV_VARIABLE"] = True
File "C:\ProgramData\miniconda\new_torch\lib\os.py", line 684, in __setitem__
value = self.encodevalue(value)
File "C:\ProgramData\miniconda\new_torch\lib\os.py", line 742, in check_str
raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not bool
Same error if I give an int
instead of a bool
.
So I tried giving a str
instead, to the variable :
import os
os.environ["OPENCV_FFMPEG_READ_ATTEMPTS"] = str(16000)
import cv2 # variables set after this may not have effect
print(os.environ["OPENCV_FFMPEG_READ_ATTEMPTS"])
This prints me 16000, but I still have the first error provided at the top, saying that current value of OPENCV_FFMPEG_READ_ATTEMPTS is 4096, when reading a particular frame of a particular video.
Now that I have created a system environment variable in Windows, plus restarted my computer, it works.
Do anyone know what the opencv Doc is missing and why setting environment variable inside python file seems impossible ?
(Windows 10, opencv-python 4.9.0.80, Python 3.9.12, openCV doc tried on version 4.10.0 and 4.9.0)