camera.cv
camera.cv
- class platypush.plugins.camera.cv.CameraCvPlugin(color_transform: Optional[str] = 'COLOR_BGR2RGB', video_type: str = 'XVID', video_writer: str = 'ffmpeg', **kwargs)[source]
Plugin to control generic cameras over OpenCV.
Requires:
opencv (
pip install opencv-python
)Pillow (
pip install Pillow
)
- __init__(color_transform: Optional[str] = 'COLOR_BGR2RGB', video_type: str = 'XVID', video_writer: str = 'ffmpeg', **kwargs)[source]
- Parameters:
device – Device ID (0 for the first camera, 1 for the second etc.) or path (e.g.
/dev/video0
).video_type – Default video type to use when exporting captured frames to camera (default: 0, infers the type from the video file extension). See here for a reference on the supported types (e.g. ‘MJPEG’, ‘XVID’, ‘H264’, ‘X264’, ‘AVC1’ etc.)
color_transform – Color transformation to apply to the captured frames. See https://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html for a full list of supported color transformations (default: “
COLOR_RGB2BGR
”)video_writer –
Class to be used to write frames to a video file. Supported values:
ffmpeg
: Use the FFmpeg writer (default, and usually more reliable - it requiresffmpeg
installed).
cv
: Use the native OpenCV writer.
The FFmpeg video writer requires
scikit-video
(pip install scikit-video
) andffmpeg
.kwargs – Extra arguments to be passed up to
platypush.plugins.camera.CameraPlugin
.
- capture_frame(camera: Camera, *args, **kwargs)[source]
Capture a frame from a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- capture_image(image_file: str, preview: bool = False, **camera) str
Capture an image.
- Parameters:
image_file – Path where the output image will be stored.
camera – Camera parameters override - see constructors parameters.
preview – Show a preview of the camera frames.
- Returns:
The local path to the saved image.
- capture_preview(duration: Optional[float] = None, n_frames: Optional[int] = None, **camera) dict
Start a camera preview session.
- Parameters:
duration – Preview duration (default: until
stop_capture()
is called).n_frames – Number of frames to display before closing (default: until
stop_capture()
is called).camera – Camera object properties.
- Returns:
The status of the device.
- capture_sequence(duration: Optional[float] = None, n_frames: Optional[int] = None, preview: bool = False, **camera) str
Capture a sequence of frames from a camera and store them to a directory.
- Parameters:
duration – Duration of the sequence in seconds (default: until
stop_capture()
is called).n_frames – Number of images to be captured (default: until
stop_capture()
is called).camera – Camera parameters override - see constructors parameters.
frames_dir
andfps
in particular can be specifically tuned forcapture_sequence
.preview – Show a preview of the camera frames.
- Returns:
The directory where the image files have been stored.
- capture_video(duration: Optional[float] = None, video_file: Optional[str] = None, preview: bool = False, **camera) Union[str, dict]
Capture a video.
- Parameters:
duration – Record duration in seconds (default: None, record until
stop_capture
).video_file – If set, the stream will be recorded to the specified video file (default: None).
camera – Camera parameters override - see constructors parameters.
preview – Show a preview of the camera frames.
- Returns:
If duration is specified, the method will wait until the recording is done and return the local path to the recorded resource. Otherwise, it will return the status of the camera device after starting it.
- capturing_thread(camera: Camera, duration: Optional[float] = None, video_file: Optional[str] = None, image_file: Optional[str] = None, n_frames: Optional[int] = None, preview: bool = False, **kwargs)
Camera capturing thread.
- Parameters:
camera – An initialized
platypush.plugins.camera.Camera
object.duration – Capturing session duration in seconds (default: until
stop_capture()
is called).video_file – If set, the session will be recorded to this output video file (video capture mode).
image_file – If set, the output of the session will be a single image file (photo mode).
n_frames – Number of frames to be captured (default: until
stop_capture()
is called).preview – Start a preview window.
kwargs – Extra arguments to be passed to
capture_frame()
.
- static encode_frame(frame, encoding: str = 'jpeg') bytes
Encode a frame to a target type. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).encoding – Image encoding (e.g.
jpeg
).
- static flip_frame(frame, horizontal_flip: bool = False, vertical_flip: bool = False)
Frame flip logic. Does nothing unless implemented by a derived plugin.
- Parameters:
frame – Image frame (default: a
PIL.Image
object).horizontal_flip – Flip along the horizontal axis.
vertical_flip – Flip along the vertical axis.
- open(device: Optional[Union[str, int]] = None, stream: bool = None, **info) Camera
Initialize and open a device using a context manager pattern.
- Parameters:
device – Capture device by name, path or ID.
stream – If set, the frames will be streamed to
camera.stream
.info – Camera parameters override - see constructors parameters.
- Returns:
The initialized
platypush.plugins.camera.Camera
object.
- open_device(device: Optional[Union[str, int]] = None, stream: bool = False, **params) Camera
Initialize and open a device.
- Returns:
The initialized camera device.
- Raises:
platypush.plugins.camera.CaptureSessionAlreadyRunningException
- prepare_device(device: Camera)[source]
Prepare a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- release_device(device: Camera)[source]
Release a device using the plugin-specific logic - to be implemented by the derived classes.
- Parameters:
device – An initialized
platypush.plugins.camera.Camera
object.
- static rotate_frame(frame, rotation: Optional[Union[float, int]] = None)
Frame rotation logic. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).rotation – Rotation angle in degrees.
- static scale_frame(frame, scale_x: Optional[float] = None, scale_y: Optional[float] = None)
Frame scaling logic. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).scale_x – X-scale factor.
scale_y – Y-scale factor.
- start_camera(camera: Camera, preview: bool = False, *args, **kwargs)
Start a camera capture session.
- Parameters:
camera – An initialized
platypush.plugins.camera.Camera
object.preview – Show a preview of the camera frames.
- start_streaming(duration: Optional[float] = None, stream_format: str = 'mkv', **camera) dict
Expose the video stream of a camera over a TCP connection.
- Parameters:
duration – Streaming thread duration (default: until
stop_streaming()
is called).stream_format – Format of the output stream - e.g.
h264
,mjpeg
,mkv
etc. (default:mkv
).camera – Camera object properties - see constructor parameters.
- Returns:
The status of the device.
- status(device: Optional[Union[str, int]] = None)
Returns the status of the specified camera or all the active cameras if
device
isNone
.
- stop_capture(device: Optional[Union[str, int]] = None)
Stop any capturing session on the specified device.
- Parameters:
device – Name/path/ID of the device to stop (default: all the active devices).
- stop_streaming(device: Optional[Union[str, int]] = None)
Stop a camera over TCP session.
- Parameters:
device – Name/path/ID of the device to stop (default: all the active devices).
- static store_frame(frame, filepath: str, format: Optional[str] = None)
Capture a frame to the filesystem using the
PIL
library - it can be overridden by derived classes.- Parameters:
frame – Frame object (default: a byte-encoded object or a
PIL.Image
object).filepath – Destination file.
format – Output format.
- take_picture(image_file: str, preview: bool = False, **camera) str
Alias for
capture_image()
.- Parameters:
image_file – Path where the output image will be stored.
camera – Camera parameters override - see constructors parameters.
preview – Show a preview of the camera frames.
- Returns:
The local path to the saved image.
- to_grayscale(frame)
Convert a frame to grayscale. The default implementation assumes that frame is a
PIL.Image
object.- Parameters:
frame – Image frame (default: a
PIL.Image
object).