How to programmatically capture a webcam photo - python

What's the simplest way in Ubuntu 11.10 to programmatically guide (either from Bash or Python) the user to capture a webcam photo of themselves?
I can launch a simple app like Cheese, but I don't see an easy way to immediately detect or retrieve the photo it captures. I can also access and record the webcam stream directly via OpenCV, but I'd have to reinvent the GUI to communicate with the user.
Is there any kind of script that's a happy medium, where I can launch it, and it prints on stdout the filename of the image the user took?

I like using pygame for that -
it does not require you to open a Pygame SDL window, unlike when you want to use it to capture keyboard events, for example.
import pygame.camera
pygame.camera.init()
cam = pygame.camera.Camera(pygame.camera.list_cameras()[0])
cam.start()
img = cam.get_image()
import pygame.image
pygame.image.save(img, "photo.bmp")
pygame.camera.quit()
Though Pygame will only save uncompressed "bmp" files - you may want to combine it with PIL to write to other formats.

If you want to do this via Python, it looks like you have a few options. The Pygame library has the ability to access cameras.
If that's unsatisfactory, you can go much lower level and access the Video 4 Linux 2 API directly using ioctl calls using Python's fcntl library.

Related

Asynchronous usage of cv::imshow

I would like to get this functionality with OpenCV 4.5.3 and Python 3.7 on Raspberry Pi 4 with 2021-05-07-raspios-buster-armhf-full:
cv::imshow("window", img);
do_something_while_img_is_displayed();
cv::destroyWindow("window");
I tried 2 options after a call to cv::imshow:
cv::waitKey(10)
cv::pollKey()
both of which display only a window frame without an image
What is the way to accomplish it?
NOTE
My intent is to have an image persistently displayed on the screen and be able to call other OpenCV functions at that time, e.g. capture and process images from a camera. I don't care about event processing loop - there will be no UI interaction at that time.
This snippet works on Ubuntu 18.04 and on 2021-05-07-raspios-buster-armhf-full:
import cv2, sys, time
image = cv2.imread("t1-4.png")
cv2.imshow("window", image)
cv2.waitKey(1000) # this delay has to be long on RPi
time.sleep(3) # substitute for do something
cv2.destroyWindow("window")
Equivalent C++ code works on both OSes as well.

Capture Screen (Specific Window) with Python

I want the ability to capture my screen (ideally from a specific application) using Python. Is it possible to do this?
If not, is it possible to build a GUI in Python that is transparent so I can drag it over a pre-existing application and visually capture what's behind it?
Can cv2 preform this? Not sure what to put inside "VideoCapture" though.
import cv2
video_capture = cv2.VideoCapture(....)
I have found solutions online that capture the entire screen. Is it possible to select a specific window/application for capturing?

Overwrite image in PIL instead of opening new window

I'm sending a stream of JPEG images from a Raspberry Pi to my MBP via a simple socket programme in Python 2.7.
When I read the image from the stream on my MBP, it opens up in Preview and opens a new Preview window for every separate image. I have an fps of about 2/3 and obviously 2/3 new windows per second is impossible to work with.
How can I go about only opening one Preview window and simply overwriting the displayed image? Would OpenCV be the best way to go? If so I am unsure how to.
Here is how I read the stream and display the images:
image_len = struct.unpack('<L', connection.read(struct.calcsize('<L')))[0]
if not image_len:
break
image_stream = io.BytesIO()
image_stream.write(connection.read(image_len))
image_stream.seek(0)
image = Image.open(image_stream)
image.show()
OS X Preview seems to automatically reload open images at intervals (always when the window receives focus), but Image.show saves a new temporary file each time you use it. I suggest saving each new frame to the same file and then using subprocess.call with the OS X open command.
This being said, the documentation notes that Image.show is primarily for debugging purposes. For a video with more than a few FPS, you probably want something else. One solution would be an HTML interface with WebSockets, perhaps using something like AutoBahn.

adding a quit timer that closes a photo

After many hours of searching online and in my python book I can't seem to find the answer to my question which is what do I add to my code so I can put in a timer that automatically closes the photo? It pulls itself up but then I have to manually close the photo to get back to my main program. Any help would be appreciated.
from PIL import Image
img = Image.open('battleship load screen.png')
img.show()
This is not possible using PIL alone - img.show() is just launching another program, it's intended for debugging really, not for presenting things to the user.
From the docs.
Displays an image. This method is mainly intended for debugging
purposes.
On Unix platforms, this method saves the image to a temporary PPM
file, and calls the xv utility.
On Windows, it saves the image to a temporary BMP file, and uses the
standard BMP display utility to show it.
This method returns None.
If you want to display an image and have control over it, use a graphical toolkit and construct a UI for your purpose. I've linked there to an example using PySide, a set of QT bindings, but of course you could use any toolkit - each will be different.

Taking screenshot from a python script by selecting the area

I saw that post (that is really helpful : Take a screenshot via a python script. [Linux]) about taking a screenshot from python. It works well but I'd like to have the same behavior as gnome-screenshot : having the possibility to choose between :
Capturing the whole desktop
Capturing an active window
Capturing an area
Is there a way to do this in python, or, eventually, to use the gnome-screenshot application to do it, and then getting the file ?
I tried to find the perfect command line for gnome-screenshot to be launched without asking where to save the screenshot after by giving the path at the call, but I can't find it.
Thanks for your help!
I have a wrapper project (pyscreenshot) for scrot, imagemagick, pyqt, wx and pygtk.
If you have one of them, you can use it.
Capturing an active window is missing.
Install:
easy_install pyscreenshot
Example:
import pyscreenshot as ImageGrab
# fullscreen
im=ImageGrab.grab()
im.show()
# part of the screen
im=ImageGrab.grab(bbox=(10,10,500,500))
im.show()
# to file
ImageGrab.grab_to_file('im.png')
If you are not limited to using using gnome-screenshot specifically, ImageMagick's import command can save directly to file without an interactive prompt.
See here for details: http://www.imagemagick.org/script/import.php
In addition to the command line interface, there is also a Python API.

Categories

Resources