Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dtraparic opened this issue May 3, 2024 · 7 comments · Fixed by #25576
Labels
bug category: documentation Documentation fix or update
Milestone

Comments

@dtraparic
Copy link

dtraparic commented May 3, 2024

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

@dtraparic dtraparic added the category: documentation Documentation fix or update label May 3, 2024
@LaurentBerger
Copy link
Contributor

first read python doc :

image

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

@clevertension
Copy link

i meet with same problem

@dtraparic
Copy link
Author

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
Copy link
Contributor

LaurentBerger commented May 6, 2024

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
Copy link
Contributor

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

@opencv-alalek opencv-alalek added invalid question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org category: documentation Documentation fix or update bug and removed category: documentation Documentation fix or update invalid question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org labels May 8, 2024
@opencv-alalek opencv-alalek added this to the 4.10.0 milestone May 8, 2024
@asmorkalov
Copy link
Contributor

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.

@asmorkalov asmorkalov added question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org and removed bug category: documentation Documentation fix or update labels May 8, 2024
@LaurentBerger
Copy link
Contributor

LaurentBerger commented May 13, 2024

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

@opencv-alalek opencv-alalek added bug category: documentation Documentation fix or update and removed question (invalid tracker) ask questions and other "no action" items here: https://forum.opencv.org labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: documentation Documentation fix or update
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants