opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
76.67k stars 55.65k forks source link

OpenCV specify environment variables inside python file wrong documentation (OPENCV_FFMPEG_READ_ATTEMPTS) #25536

Closed dtraparic closed 1 month ago

dtraparic commented 1 month ago

Describe the doc issue

Hello,

I think this is documentation issue + bug issue

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)

Fix suggestion

No response

LaurentBerger commented 1 month ago

first read python doc :

image

I don't see anything in your code about reading a video

clevertension commented 1 month ago

i meet with same problem

dtraparic commented 1 month ago

Dear @LaurentBerger,

I understood the Python Doc but I honestly don't know what to understand in your quoted paragraph. 😕

What I said is that the code provided inside the OpenCV Doc to modify os environment variable, executed as is, raises an error.

The code that reads a video frame doesn't matter much, and it's embedded through MMCV that uses OpenCV, so I'm not able to provide a OpenCV-code-only example. I know this part works, because as I said, it works once the environment variable modified inside Windows parameters.

Please tell me if you really need an example for reading a video frame but I think it's not necessary, as I guess the problem will occur on the set up of every possible OpenCV environment variable.

LaurentBerger commented 1 month ago

os.environ["MY_ENV_VARIABLE"] = True True is not a string and now I search in doc and I found reference: image I think you can make a PR to change python code

mshabunin commented 1 month ago

Example in the documentation is wrong because it doesn't use string. However generally this approach should work:

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ["OPENCV_FOR_THREADS_NUM"] = "3"
>>> import cv2
>>> cv2.getNumThreads()
3

Default value without environment change:

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.getNumThreads()
4
asmorkalov commented 1 month ago

Most probably the same as https://stackoverflow.com/questions/5153547/environment-variables-are-different-for-dll-than-exe. It's not related to OpenCV, but OS specific.

LaurentBerger commented 1 month ago

I don't understand : opencv documentation is wrong for python image

there is a bug in opencv documentation because if you follow documentation it raises an execption