Device Interaction
PyXA offers a _Devices_ addition that provides a simple interface to your device’s hardware such as its camera, microphone, and screen.
Device Classes
The following device classes are available:
XACamera
- for capturing photos and videosXAMicrophone
- for recording audioXAScreen
- for capturing screenshots and screen recordings
An overview of each class is provided below.
Camera
The XACamera
class provides two methods: one to capture photos (capture()
) and another to record videos (record()
). Both methods return an instance of the corresponding PyXA class—either XAImage
or XAVideo
—that can be used alongside other areas of PyXA.
For example, to capture a photo and save it to the desktop, you could use the following code:
import PyXA
import os
cam = PyXA.XACamera()
img = cam.capture()
homedir = os.path.expanduser("~")
img.save(f"{homedir}/Desktop/test.png")
You can combine this with other PyXA features to create powerful automations. For example, you could use the XASpeech
class to speak any text detected in the image:
import PyXA
cam = PyXA.XACamera()
img = cam.capture()
img_text = img.extract_text()
PyXA.XASpeech(img_text).speak()
Microphone
The XAMicrophone
class provides a single method, record()
, that records audio from the device’s microphone and returns an instance of the XAAudio
class.
For example, to record 5 seconds of audio and play it back, you could use the following code:
import PyXA
import os
mic = PyXA.XAMicrophone()
homedir = os.path.expanduser("~")
recording = mic.record(f"{homedir}/Downloads/test.wav", 5)
recording.play()
Screen
PyXA’s XAScreen
class provides methods for capturing screenshots and screen recordings. Each method returns either a XAImage
or XAVideo
object. The available methods are:
capture()
- captures a screenshotcapture_rect()
- captures a screenshot of a specific area of the screencapture_window()
- captures a screenshot of a specific windowrecord()
- records a screen recording