Hello: I am developing on Linux trying to use Python/OpenCV to process videos.
I have tried using Python2 and Python3 but no results.
import cv2
video = cv2.VideoCapture("myvideo.mp4")
status = video.isOpened()
In this case, status is a boolean and it is always false.
A quick search online pointed to the problem as Python OpenCV module is compiled with FFMPEG library turned on. As a result, it is cannot handle video.
But I am unable to find any solution to it.
Any help much appreciated.
See if you can use ffmpeg from command line and the video (with exactly the same path) as an input for some kind of simple manipulation or conversion.
Doing that, output it as an .avi file with a different coding and see if that makes any difference for OpenCV as an input.
Check if maybe you're able to use the feed from your webcam by modifying the script just a little bit.
Check if the path you provide is correct. Try putting the video in your home folder and providing an absolute path.
Try reinstalling ffmpeg according to these instrcutions.
If none of above work, follow this guide.
Related
I am currently working on a project which would take in my camera input, configure it, and then output the configured video. So whenever I used my camera, it would be showing my configured python camera output.
Are there any modules or methods for me to do this? Any help would be greatly appreciated.
Best way of doing it is by using opencv library. And for running your code in the backgoround you can use this: pythonw <nameOfFile.py>. Here is a link on how to do it:https://www.geeksforgeeks.org/running-python-program-in-the-background/
I'm currently working on an application called SatStressGUI, a GUI application written in Python, which was passed down to me from previous interns who have worked on it. It relies on dependencies such as wxPython (which, as most of you probably know, must be installed on the user's own computer before the application the user is trying to run runs properly if the application calls on the the wxPython library). In the gitHub, under contents->resources->lib->python2.5->sitepackages, you'll see all the libraries which the application is dependent on (wxPython is under the name wx, for example) copied and pasted. If I am correct, the existence of these libraries in sitepackages makes it so that the user does not have to manually install libraries such as wxPython themselves: the user can simply run the GUI application by downloading the whole package under "clone or download" in the main page and run the satstressgui executable file located in contents->MacOS. I believe there is a file somewhere, perhaps boot.py (located in contents->resources) that says, "hey, before the user runs this executable file, download the libraries under contents->resources->lib->python2.5->sitepackages."
All that being said, I want to add a new dependency to the program. I am trying to use FFMPEG, which is an external program that allows you to take a sequence of images and convert it to a video. In satstressgui.py, which is the main source code file for this application (located in contents->resources) I use the subprocess python module to call on the external program FFMPEG. Here is the code snippet:
#FFMPEG is an external program that converts a sequence of images to a video.
subprocess.call(['/usr/local/bin/ffmpeg', '-framerate', \
framerate, '-f', 'image2','-pattern_type', \
'glob', '-i', self.directory + '/orbit_*.png', \
'-r', '10', '-s', '620x380', self.directory +
".avi"])
At the moment, this works on my own computer because I have FFMPEG installed (in case it matters, I installed it using homebrew). How do I make it so that FFMPEG becomes a dependency in my program just like wxPython (i.e., I make it so that FFMPEG is "installed" on my user's computer before they run the application)? I've looked on FFMPEG's website and it looks as if there is a gitHub for it, but other than that I'm quite lost.
NOTE: I've also tried to use imageio (another Python library) to convert a sequence of images to a video in my application, but I've failed. I tried to copy and paste the files from imageio's gitHub to contents->resources->lib->python2.5->sitepackages, but whenever I try to "import imageio" it fails. This might be because I am adding the dependency wrong or because imageio only works on Python 2.6 and above and my program is written in Python 2.5. If someone could give me an answer to any of these questions and help me import a module such as imageio or add an external program such as FFMPEG as a dependency in my application, that'd be very much appreciated! I can't link imageio's main website because of some restrictions by Stack Overflow, but you should be able to find it by simply searching it up on google (the main website has a photo of an astronaut in it's top banner). I would more prefer a solution using FFMPEG, however, seeing as I've already implemented it in my code.
Thank you so much in advance for your help! I'm quite new to computer science and programming so I apologize if a lot of the clarifications or questions I'm asking are rudimentary.
Here is the link to the gitHub of my application: https://github.com/nhudoan/SatStressGUI
Here is the link to FFMPEG's main website: https://ffmpeg.org/download.html#repositories
I'm trying to capture my screen using Python because I'll use it on OpenCV, but I couldn't find a way to make it work on Gnome, since Gnome uses Wayland and all libraries that I've found only work with X11.
For now I'm not considering change my interface. I'm searching a solution to this problem.
Does someone know a solution?
To be more specific, I'll use the images to train an AI and so I need they continuously.
EDIT:
I've found this but how can I pass frames to OpenCV in Python instead of save a video file?
The proper way to do screencasting these days is by using the Screencast portal, which is part of XDG desktop portals and is already supported by GNOME, KDE, wlroots (and more). As an added advantage, this will also work in containerized formats like Flatpaks.
You can find an example on how to do screencasting in Pyhon using this snippet, created by one of the Mutter maintainers. If you look for parse_launch(), you will see a GStreamer pipeline which you can modify to include the GStreamer OpenCV elements that can do the processing for you.
Note: in your edit, you link to a predecessor of that portal, which is GNOME-specifc, internal API, so I wouldn't rely on it ;-)
I am using Ubuntu 14.04. I have installed OpenCV using Adrian Rosebrock's guide. I am also using PyCharm for programming python and opencv.
My problem is that I can use code completion for cv2 modules but code completion wont work for instances initiated from cv2. An example is shown below.
This works:
This does not:
There is no run time error when I write my program as expected. Such that cap.isOpened() works without an error.
Though I am Window user, I also had faced similar problem with you. In my case, I could solve this problem by importing this way:
from cv2 import cv2
As I'm lack of knowledge of how does the python imports module, I can't explain you clearly about why this solve the problem, but it works anyway.
Good luck.
The openCV python module is a dynamically generated wrapper of the underlying c++ library. PyCharm relies on the availability of python source code to provide autocomplete functionality. When the source code is missing (as in the opencv case), pycharm will generate skeleton files with function prototypes and rely on those for autocompletion but with diminished capabilities.
As a result when you autocomplete at
cv2.
it can figure out that the module cv2 has the following members and provide suggestions.
On the other hand when you
cap = cv2.VideoCapture(file_name)
PyCharm can figure out that you just called a method from the cv2 module and assigned it to cap but has no information about the type of the result of this method and does not know where to go look for suggestions for
cap.
If you try the same things in shell mode, you will see the behavior you actually expected to see, since in shell mode will actually introspect live objects (it will ask the created cap object what members it has and provide those as suggestions)
You can also write stubs for the opencv module yourself to enable correct autocompletion in edit mode.
Take a look here
If anyone still is experiencing the issue, downgrading to opencv to 4.5.5.62 helped my case.
I am using PyCharm on windows 10 and faced similar issue on the intellisense for cv2.
This is my solution:
Pycharm>File>Manage IDE settings> Restore Default settings
Restart the Pycharm IDE
Reconfigure Python Interpretor
I'm looking for a Python library that can combine images into a video.
A library that just allows you to create an empty video and feed images into it as frames is ideal.
Preferably with support for MPEG compression of the video file as well.
If you run linux then you can use ffmpeg to do this from the command line there is a python wrapper called pyFFmpeg that you can use - there is also pymedia but it doesn't look to be maintained.
BTW there are a number of projects that provide builds of ffmpeg for windows.
gstreamer is the tool you are looking for. you'll probably need an appsrc or something like that.