Python | Replacing Camera Output - python

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/

Related

How to capture screen on Wayland(Gnome) in Python code?

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 ;-)

Code completion is not working for OpenCV and Python

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

Python OpenCV unable to handle videos

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.

Debug OpenCV images in Python

Visual Studio allows to visualize OpenCV images using Image Watch plugin (https://visualstudiogallery.msdn.microsoft.com/e682d542-7ef3-402c-b857-bbfba714f78d) during debug. This is very helpful for computer vision coding.
What is the proffered way to visualize images in Python binding to OpenCV? I am aware that it is possible to use cv2.imshow("name", image) but that is not very practical in contrast to Image Watch which allows to show many images at the same time and automatically does the update after change.
Is there any alternative to Image Watch for Python?
If you use VSCode, you can try the simply_view_image_for_python_opencv_debugging VSCode-Extension.
https://marketplace.visualstudio.com/items?itemName=johnguo.simply-view-image-for-python-opencv-debugging
I think maybe you'll want to look at Visual-Logging. It can produce a rather nice collection of your output that you can just open up in a browser. It is pip installable like so:
pip install visual-logging
Here is a page with a concise walkthrough that shows how it is used.
It's not nearly as fancy as that Visual Studio tool, but it might be your best option that already exists.
I had similar problems, so I've just created OpenCV Image Viewer Plugin, which works as you expect. You can install it to any JetBrains IDE, which support Python (directly or via plugin).
https://plugins.jetbrains.com/plugin/14371-opencv-image-viewer

Tablet Pressure with Python on Linux

Is there a simple way to get the pen pressure data from a usb tablet using python on Linux?
You can do it by reading input events on the input device node. I wrote some modules to do this. you can find it in the Pycopia project.
The disadvantage of this is that your program must run as root.
The powerdroid project also uses this, but that's old code now. You can see another example of synthesizing touch input in the devices module. It probably won't work anymore, but you might start with that.
Try using PySide, it's a QT Wrapper here: QTabletEvent.
Or you can use Python and PyGame: Here.

Categories