I have a few avi files which I'm trying to read frame by frame.
I have used opencv 3.1.0 in order to read frames:
import cv2
cap = cv2.VideoCapture(file_path)
and then I just read the frames of cap. This works fine on some of the videos. I took a few more videos recently and the same code comes up empty. I checked:
cap.isOpened() # return False
and it seems like the video is not opened properly. As far as I'm concerned the only thing that changed is that the new videos are in color whereas the old ones were not. I don't know how that would change anything in the code. I checked that the videos open up just fine in other software (imageJ) so I'm confident the video itself is not the issue.
Any ideas why the new videos are not opening properly? I have not been able to find any information on how to debug this issue. Any advice would be greatly appreciated.
I switched to imageio instead of opencv and everything works well
I was making the same mistake.
Solution.
Use Pycharm
Install Anaconda3
conda install -c conda-forge opencv ( open the CMD as an administrator )
Select Anaconda3 in PyCharm ( File -> Settings -> Project Interpreter )
AND TEST
def VideoPlayer():
cap = cv2.VideoCapture("D:\OpenCv4Programmers\datas\TomAndJerry.mp4")
while (True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
After a lot of time of test & fails on this... 2 things needs to be working
ffmpeg must be installed, you can check if it's installed and working with opencv with this:
print(cv2.getBuildInformation())
Apparently there's an environment variable OPENCV_FFMPEG_CAPTURE_OPTIONS that might not be set under some circumstances.... so make sure it is
import os
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp"
tested and working with RSTP from Android using miv.rtspcamera and cvlc from Raspberry Pi
I'm using Windows, Python 2.7.4. I tried Opencv installation but it did not work, I also installed ffmpeg. Then I tried this:
pip install opencv-python
This worked for me.
Try to install ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg
The problem is that you never actually open your cap. I have not done this from a video file but to use the camera it looks something like this:
import cv2
#instantiate videocapture object (0 is for the default webcam)
cap = cv2.VideoCapture(0)
while(True):
#read frame from video file
ret, frame = cap.read()
cv2.imshow('frame',frame)
#if esc key pressed or no more video input
if cv2.waitKey(1) & 0xFF == 27 or ret==False:
break
# When everything done, release the capture
cv2.destroyAllWindows()
cap.release()
Related
I am using opencv and Python3 to read webcam.
The webcam light keeps on even though I released the webcam.
I am doing:
webcam = cv2.VideoCapture(0)
webcam.release()
After the release command, if I try to read the cam I get: (False, None) but the webcam itself is still <VideoCapture 0EE62DC0>. I don't know if this is the issue, but the light keeps on until I kill all python script or python terminal.
I am on a Windows 10, python 3.5, opencv 4.0.1. Am I doing something wrong?
I had exactly the same problem. Was using python3.6 and opencv 3.4.2 (also on Windows10), reinstalled opencv-python version to 3.4.0.14. That worked for me.
Seems that there is a problem with opencv 3.4.2.
That specific version you can install by running the command:
pip install opencv-python==3.4.0.14
Same here, using c++ and OpenCV 3.4.3 on Windows10.
Problem seems related to MSMF backend, disabling creating a environment variable with value:
OPENCV_VIDEOIO_PRIORITY_MSMF=0
solve the problem. Source: here
I had the same problem.
My problem was solved when I changed the while loop condition from:
while(cap.isOpened()):
#Your code
with:
while(True):
ret, img = cap.read()
#Your code
Before changing the loop I also applied the following command on cmd prompt:
setx OPENCV_VIDEOIO_PRIORITY_MSMF 0
Please restart the PC once you applied the above command!
Hope this work for you too.
Cheers!!
I want to load a live video from my web cam using open cv v3 and Python 3.5 on Windows 10 but I always get this 'AttributeError: module 'cv2' has no attribute 'VideoCapture' when loaded from a file. When I import cv2 using the shell, all the modules are accessible. But when I use dir(cv2) and run it in a file, I only see a few modules. Here is my code
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
How can I access the 'VideoCapture' attr from cv2? I need help please. All the solution I find on online don't seem to work. thanks
I am not sure if this will fix the issue but I have had a problems while using OpenCV 3.3/OpenCV 2.4 with Python 3.5. I switched to Python 2.7 and many crashes and similar errors got fixed. I use cv2.VideoCapture() in my projects and it works like a charm! (Using opencv3.3 and python 2.7)
SOLVED: I think Shawn Mathew is right, I had multiple Python Versions installed. When I removed other versions and then I installed Anaconda with Python 3.5, I installed opencv with pip using the binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/. It worked, I hope this will help someone having the same issue
NB: Don't forget to also install Microsoft C++ Redistributable from official Microsoft website
I read nearly all of questions about this subject but I didn't solve it.
I am working on Linux/Mint. I tried to open my webcam (or any video it does not matter) with opencv and read a frame from the video. And I write this code:
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
print ret #It returns always false
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
--> I read the problem might can be because of opencv installation. I checked the installation, it seems true. When I write 'pip freeze', the result list contained opencv-python module so I thought the installation is correct and it isn't the reason for my problem.
--> I tried to change the index in videoCapture(0) to -1 and 1, and it again failed.
--> And then, I read that the other cause can be FFmpeg or codec. But I also installed FFmpeg and the problem didn't change.
--> cv2.VideoCapture() didn't give any error but I want to install cv2 module on the Poject>Settings>Project Interpreter, it gave me an error (executed command: pip install cv2) But when I run this command, it gave me this error : Could not find a version that satisfies the requirement cv2 (from versions: ) No matching distribution found for cv2
I checked the cv2 module in my virtualenv and it seems there. (/cv_env/lib/python2.7/site-packages/cv2)
--> I also tried it with "skvideo.io.VideoCapture()" but I failed again.
So, I didn't understand the problem and I am really new in this subject.
What is the problem?
sudo modprobe bcm2835-v4l2
will "enable" the your pi camera for opencv automatically.
I just came into the world of openCV and python, and I'm having a strange issue with opening the video stream. This is the code that I'm using:
import numpy
import cv2
cap = cv2.VideoCapture('SampleVideo.mp4')
print(cap.isOpened())
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
This code can successfully open the video stream when i run it in terminal(windows command line) but not in Pycharm. This is the result of the run in Pycharm:
False
Process finished with exit code 0
Any help would be appreciated. Thank you all!
Codec: FFmpeg. OS: Windows 8.1. Python: 3.5.2. OpenCV: 3.1
Update Sep.27:
I noticed that the code runs successfully in IDLE, but not in PyCharm. Now I'm really confused.
I can also confirm that imread and imwrite works properly in PyCharm. So maybe it is the Codec that is not working? How to check my codec in OpenCV?
Update Sep.29:
Now I think I've ruled out the possibility that the terminal and PyCharm are using different interpreter. I really think it's somehow that the default codec is not linked in the PyCharm environment. How do I check my default system codec either in OpenCV or Python?
The problem is solved.
I solved it by purging everything, Pycharm, Python and all the packages. So obviously this issue came from some sort of dependency mess-up. Not really sure what it is, but purging is the way to go people.
I am trying to write a small test app with openCV, using Python, to capture frames from a Webcam.
My system is very minimal, and therefor I installed a minimal Ubuntu with Fluxbox.
I can't get any video capture happening with openCV.
When I try the same code on a more complete installation of Ubuntu (such as desktop, or Xubuntu and even Lubuntu) I am able to capture video easily.
My conclusion is that my minimal Ubuntu installation is missing something, and I am trying to find out what it is.
As for the details: I do see my camera listed with I run 'lsusb'
Also, when I execute:
cap = cv.CaptureFromCAM(0)
I do get a valid capture object. But when I try:
frame = cv.QueryFrame(cap)
I get a null object.
I installed python-opencv using apt-get rather than building it myself. It seems to work just fine on Xubuntu, Lubuntu etc.
Thanks in advance for the help
I had the same probleme before, you must to have FFMEPG library installed on your computer for use this function
after you must do a cmake again and recompile opencv library
If you are using python,u can install
pip install opencv-python
then you can use
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow('video', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It works on any minimal setup.Thank you.