dxymie
June 13, 2023, 1:14am
1
Previously I found a weird problem which I post on here to ask for some help:
opened 10:34AM - 18 May 23 UTC
closed 07:23AM - 29 May 23 UTC
question
### Expected behaviour
Write here how did you expect the library to function.…
`import cv2
cv2.namedWindow("enhanced",0)
cv2.resizeWindow("enhanced", 1920, 1080)
if __name__=="__main__":
from multiprocessing import cpu_count
addr="rtsp://xxxx"
cap = cv2.VideoCapture(addr)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 2)
print("Number of CPUs:", cv2.getNumberOfCPUs())
print("Number of threads:", cv2.getNumThreads())
while True:
ret, frame = cap.read()
if not ret:
print("break")
break
cv2.imshow('enhanced', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
del(ret)
del(frame)
cap.release()
cv2.destroyAllWindows()`
### Actual behaviour
system information:
- windows server 2016 CPU: E5 2696 v4 (22 cores 44thread support)
- software tools: Virtual Box and VMware workstation16
Write here what went wrong.
when I allocate multiple Cores in virtal mechine compared with single Core running the same code above.
I find the it is faster with less core. And more you allocate the cores the slower it is.
I also tried on my Laptop with AMD R7-6800H and get the same result.
I put the pictures here show the actual delay time VS webcam official webset
1. with multiple cores on my Laptop.
![5b20e64fed4b58f3557451add8b32f9](https://github.com/opencv/opencv-python/assets/71275391/c10c51d9-9d27-4bdb-8ea4-5bd7aba1a576)
2. with less Cores on my Lartop
![924ebdfd57ced77059a48ac1cd18807](https://github.com/opencv/opencv-python/assets/71275391/27519763-5433-4ccd-ac98-866e92aa0c5f)
### Steps to reproduce
- opencv-python version : 4.6.0.66
I have found some suggestions that to build wheels of opencv to support TBB or openMP ,Is it true?
I want to make it out .Any one can give me some advice?
The delay is unbearable
By the way ,on the server with E5 2696 v4 ,it is very slow (almost 3 seconds delayy) with all cores,but it is much faster in the virtual mechaine with less cores. And I tried two virtual mechaine software (VirtualBox and VMware) I think it is not the reason of softeare.
##### Issue submission checklist
- [x] This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
<!--
Use Q&A forums such as https://answers.opencv.org/questions/ and https://stackoverflow.com/ and other communities
to discuss problems. Tickets without real issue statements related to this build toolchain will be closed.
-->
- [x] I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)
<!--
If you have some OpenCV bug report which needs to fixed in the C++ code,
please report issue to the OpenCV repository:
https://github.com/opencv/opencv/issues
See also:
* OpenCV documentation: https://docs.opencv.org
* OpenCV FAQ page: https://github.com/opencv/opencv/wiki/FAQ
* OpenCV forum: https://answers.opencv.org
* Stack Overflow branch: https://stackoverflow.com/questions/tagged/opencv
-->
- [ ] The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")
- [ ] I'm using the latest version of ``opencv-python``
Thanks for their instruction,however I still not figured it out how to set limitation on ffmpeg in cv2.videocapture()function.
I use other way to handle this problem.
After building the opencv with gstreamer support this phenomenon might disappear.Also it has side-effect that the rtsp stream it read is not as fluent as ffmpeg although gstreamer is faster. still want to use the ffmpeg,can anyone give me some advice?
Have you tried setting the number of threads in the VideoCapture
constructor as
cap = cv2.VideoCapture(addr, cv2.CAP_FFMPEG, [CAP_PROP_N_THREADS, nThreads ])
dxymie
June 13, 2023, 5:58am
3
Thanks for your reply.You mean cap = cv2.VideoCapture(addr,cv2.CAP_FFMPEG, [cv2.CAP_PROP_N_THREADS, 2 ]) ? I tried this but cv2 seems dont’t have CAP_PROP_N_THREADS.
I also Tried like this cap = cv2.VideoCapture(addr,cv2.CAP_FFMPEG, [2, 2 ]) It failed ethier.
You need to build from a commit after this pr was merged
opencv:4.x
← cudawarped:ffmpeg_rtsp_low_fps
opened 10:24AM - 14 Jul 22 UTC
When using `VideoCapture` with the FFMpeg backend to stream from an RTSP source … at a low frame rate on a machine with a large number of CPU cores, `VideoCapture::read/grab` can fail due to the interrupt timer exceeding `timeout_after_ms`. To try and fix this, this PR adds the `CAP_PROP_N_THREADS` which can be set on open to reduce the number of threads in this case. This is related to https://github.com/opencv/opencv/issues/20002 but I cannot tell if it fixes the issue as I am unable to re-create it on a machine with reading from a file with only 20 CPU cores.
In addition to this `VideoCapture::open` can get stuck and therefore interupted on the call to `err = avformat_find_stream_info(ic, NULL);` when streaming from an RTSP source at a low frame rate regardless of the number of CPU cores. The call to `open` still succeeds and the frame can be read but because the flag `interrupt_metadata.timeout` has been set in `open` the call to `grab/read` fails. To fix this the `interrupt_metadata.timeout` flag is reset on entry to `grab/read`.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Upgrade to OpenCV 4.7.0.
1 Like
dxymie
June 13, 2023, 6:13am
5
OK, you mean I should download from here GitHub - cudawarped/opencv at ffmpeg_rtsp_low_fps
and build opencv again.But how to check this pr’s opencv version?
Either install the latest pre-built wheel with pip
or build again checking out the 4.7.0 tag
after you have cloned the repository
dxymie
June 13, 2023, 6:35am
7
OKOK.Thanks for your advice. By the way, if I want to limit the ffmepg to use less than 16 cores for example like 4 cores. The only way to do is to rebuild opencv4.7.0 right? Do you know how to change the default setting in opencv 4.7.0?
No. The option to change the number of threads is available at runtime, when you create VideoCapture
as
cap = cv2.VideoCapture(addr, cv2.CAP_FFMPEG, [CAP_PROP_N_THREADS, 16])
You need to upgrade your version of OpenCV python bindings (build from source or pip install) because this feature was added in OpenCV 4.7.0 (is not available in opencv-python version : 4.6.0.66).
dxymie
June 13, 2023, 8:40am
9
cudawarped:
CAP_PROP_N_THREADS
WOW! Thanks a lot. It seems like I am going to the wrong direction! Any way, THANKS!