opencv: Can't open webcan or video file with python3 and opencv
System information (version)
- OpenCV => 3.2.0-1
- Python OpenCV => 3.2.0.6
- Python => 3.5.2
- Operating System / Platform => ArchLinux 64bits
Detailed description
Can’t open VideoCapture don’t work with files and /dev/video
Steps to reproduce
import cv2
video = cv2.VideoCapture(0)
if video.isOpened():
while True:
check, frame = video.read()
if check:
cv2.imshow('Color Frame', frame)
key = cv2.waitKey(50)
if key == ord('q'):
break
else:
print('Frame not available')
print(video.isOpened())
The code don’t show webcan or video file Works with python2 …
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 25 (5 by maintainers)
I have had a comparable issue on Ubuntu, using the pip install opencv-python process. Solution used : I recompiled both ffmpeg and opencv 3.2 from sources on my station (following the install processes indicated on offical portals). Everything worked well, but I still was not able to open video from cv2.videocapture on python.
My solution has been to replace the cv2.so library provided by pypi with the one compiled from sources :
sudo cp opencv-3.2.0/build/lib/cv2.so /usr/local/lib/python2.7/dist-packages/cv2/
And I now can play videos with cv2, from my python scripts. Hope this will help those experiencing the same issue (I spent some time on trying to find a solution and never found this suggestion so …).
This is still looks like an usage question. OpenCV requires 3rdparty libraries to work with cameras/videos. You need to install them properly before building OpenCV Python bindings.
opencv-python
doesn’t support them (almost all 3rdparty dependencies are turned off, because they are system dependent - system with minimal configuration is used during build).For more information please refer to this question and others:
(https://pypi.python.org/pypi/opencv-python)
opencv-python
binary package is not officially supported by OpenCV dev team, so we can’t help here.Usage questions should go to Users OpenCV Q/A forum: http://answers.opencv.org
For future reference:
If you have installed either one of the precompiled distributions
opencv-python
oropencv-contrib-python
from pip there are a few things to note if you are using Linux or macOS (Windows builds have had video support always):Changelogs are available for every release at the
opencv-python
package repository https://github.com/skvark/opencv-python/releases.In my case i simply un-installed and installed back opencv-python Before re-installation i had opencv-python-3.3.0.10. after re-install i got a newer version opencv-python-3.4.0.12
I did a similar thing to @iiAnthony where I compiled OpenCV 3.2.0 from source, double checking with CMake that it can find both python 2 and python 3
then I replaced the compiled
.so
dynamic library with the one that comes withopencv-python
python3 package. The difference between my comment and @iiAnthony’s is that his caters for Python2 users?Or better yet, just the
ln
command to symlink the built.so
instead of copying, so that if you decide to compile OpenCV3 with different compilation flags you don’t have to copy the library file over again.Note: Your
.so
library file may have a different name to mine, but the idea is the same, compile your own opencv3.so
library file and replace the one thatopencv-python
comes with.Can’t open videos or webcam:
System information (version)
opencv-python
Version: 3.2.0.7python
Version: 3.6.1 Operating System / Platform:4.10.11-1-ARCH
LinuxDetailed description VideoCapture doesn’t work with files or with /dev/video
Steps to reproduce
I had this problem and couldn’t find a solution, but on the web, it was working. My solution was almost funny - you can’t have two streams. I turned off the web stream and then it was fine!
I know people don’t appreciate you @FilipBielickiPL, but that is certainly my problem, so thx! Basically, I have a configurable program (video vs webcam) and despite the WebcamVideoStream is not used during video mode, it’ll disturb the way VideoCapture working.
I recently solved issue for my use case. So my old (BAD configs) were:
OpenCV
2.4.5 (afteryum install opencv
)opencv_python
3.3.0.10 (afterpip3 install opencv_python
)And in fact I have Assertion error, as it’s described here: https://github.com/ageitgey/face_recognition/issues/191#issuecomment-350223266
Then, I compiled GOOD config - OpenCV from sources (git cloned and checked out 3.3.1 branch) and I had 3.3.1 version. But even with opencv 3.3.0.10 IT WAS NOT working, because
yes, opencv_python installs into
... lib64 ...
So, then there are 2 ways of resolving issue:
And later on
import cv2
will import from/usr/lib/python3.4/site-packages/
(which is 3.3.1)I did both, and my application was working in both cases. But still, I am not confident what is the best way.
PS.
So far I have 2 web cameras: built in and external via USB, and my python code can’t access external. It always open built in. If someone know how to do it, please let me know. Here is piece of code:
That could be a number of things:
cmake ..
stage (like in the screenshot I took in the previous post).python-dev
orpython3-dev
packages)git checkout 3.3.1
)find . -name "cv2.*.so"
to find if it is hidden somewhere? Though I am not convinced that building on CentOS would mean the.so
file is at a different location during build.I just downloaded and built the zipped OpenCV 3.3.1 in Ubuntu 16.04 and have found the
cv2.so
in thebuild/lib/python3
folder so you’ll have to double check your environment.Same issue with Python 3.6.0 under MacOS 10.12 Sierra.
Great answer! It works for me
@chutsu Why the thumbs down? I solved my error exactly like what @FilipBielickiPL did. Is that not clear enough?
it would help if you can share a minimal reproducible example so that people can try out what the error is. Without example code we can’t help you.
BTW,
You have custom python setup (without any information how you get it).
This file set is not generated by original OpenCV. You have installed something else from other sources.