summaryrefslogtreecommitdiff
path: root/src/py/examples
diff options
context:
space:
mode:
Diffstat (limited to 'src/py/examples')
-rwxr-xr-xsrc/py/examples/simple-cam.py20
-rwxr-xr-xsrc/py/examples/simple-capture.py33
-rwxr-xr-xsrc/py/examples/simple-continuous-capture.py26
3 files changed, 32 insertions, 47 deletions
diff --git a/src/py/examples/simple-cam.py b/src/py/examples/simple-cam.py
index 2b81bb65..1cd1019d 100755
--- a/src/py/examples/simple-cam.py
+++ b/src/py/examples/simple-cam.py
@@ -19,8 +19,9 @@ TIMEOUT_SEC = 3
def handle_camera_event(cm):
- # cm.get_ready_requests() will not block here, as we know there is an event
- # to read.
+ # cm.get_ready_requests() returns the ready requests, which in our case
+ # should almost always return a single Request, but in some cases there
+ # could be multiple or none.
reqs = cm.get_ready_requests()
@@ -258,12 +259,7 @@ def main():
allocator = libcam.FrameBufferAllocator(camera)
for cfg in config:
- ret = allocator.allocate(cfg.stream)
- if ret < 0:
- print('Can\'t allocate buffers')
- return -1
-
- allocated = len(allocator.buffers(cfg.stream))
+ allocated = allocator.allocate(cfg.stream)
print(f'Allocated {allocated} buffers for stream')
# --------------------------------------------------------------------
@@ -288,15 +284,9 @@ def main():
requests = []
for i in range(len(buffers)):
request = camera.create_request()
- if not request:
- print('Can\'t create request')
- return -1
buffer = buffers[i]
- ret = request.add_buffer(stream, buffer)
- if ret < 0:
- print('Can\'t set buffer for request')
- return -1
+ request.add_buffer(stream, buffer)
# Controls can be added to a request on a per frame basis.
request.set_control(libcam.controls.Brightness, 0.5)
diff --git a/src/py/examples/simple-capture.py b/src/py/examples/simple-capture.py
index a6a9b33e..4b85408f 100755
--- a/src/py/examples/simple-capture.py
+++ b/src/py/examples/simple-capture.py
@@ -14,6 +14,7 @@
import argparse
import libcamera as libcam
+import selectors
import sys
# Number of frames to capture
@@ -42,8 +43,7 @@ def main():
# Acquire the camera for our use
- ret = cam.acquire()
- assert ret == 0
+ cam.acquire()
# Configure the camera
@@ -59,8 +59,7 @@ def main():
w, h = [int(v) for v in args.size.split('x')]
stream_config.size = libcam.Size(w, h)
- ret = cam.configure(cam_config)
- assert ret == 0
+ cam.configure(cam_config)
print(f'Capturing {TOTAL_FRAMES} frames with {stream_config}')
@@ -82,15 +81,13 @@ def main():
req = cam.create_request(i)
buffer = allocator.buffers(stream)[i]
- ret = req.add_buffer(stream, buffer)
- assert ret == 0
+ req.add_buffer(stream, buffer)
reqs.append(req)
# Start the camera
- ret = cam.start()
- assert ret == 0
+ cam.start()
# frames_queued and frames_done track the number of frames queued and done
@@ -100,18 +97,24 @@ def main():
# Queue the requests to the camera
for req in reqs:
- ret = cam.queue_request(req)
- assert ret == 0
+ cam.queue_request(req)
frames_queued += 1
# The main loop. Wait for the queued Requests to complete, process them,
# and re-queue them again.
+ sel = selectors.DefaultSelector()
+ sel.register(cm.event_fd, selectors.EVENT_READ)
+
while frames_done < TOTAL_FRAMES:
- # cm.get_ready_requests() blocks until there is an event and returns
- # all the ready requests. Here we should almost always get a single
+ # cm.get_ready_requests() does not block, so we use a Selector to wait
+ # for a camera event. Here we should almost always get a single
# Request, but in some cases there could be multiple or none.
+ events = sel.select()
+ if not events:
+ continue
+
reqs = cm.get_ready_requests()
for req in reqs:
@@ -147,13 +150,11 @@ def main():
# Stop the camera
- ret = cam.stop()
- assert ret == 0
+ cam.stop()
# Release the camera
- ret = cam.release()
- assert ret == 0
+ cam.release()
return 0
diff --git a/src/py/examples/simple-continuous-capture.py b/src/py/examples/simple-continuous-capture.py
index fe78a2dd..e1cb931e 100755
--- a/src/py/examples/simple-continuous-capture.py
+++ b/src/py/examples/simple-continuous-capture.py
@@ -28,8 +28,7 @@ class CameraCaptureContext:
# Acquire the camera for our use
- ret = cam.acquire()
- assert ret == 0
+ cam.acquire()
# Configure the camera
@@ -37,8 +36,7 @@ class CameraCaptureContext:
stream_config = cam_config.at(0)
- ret = cam.configure(cam_config)
- assert ret == 0
+ cam.configure(cam_config)
stream = stream_config.stream
@@ -62,8 +60,7 @@ class CameraCaptureContext:
req = cam.create_request(idx)
buffer = allocator.buffers(stream)[i]
- ret = req.add_buffer(stream, buffer)
- assert ret == 0
+ req.add_buffer(stream, buffer)
self.reqs.append(req)
@@ -73,13 +70,11 @@ class CameraCaptureContext:
def uninit_camera(self):
# Stop the camera
- ret = self.cam.stop()
- assert ret == 0
+ self.cam.stop()
# Release the camera
- ret = self.cam.release()
- assert ret == 0
+ self.cam.release()
# A container class for our state
@@ -88,8 +83,9 @@ class CaptureContext:
camera_contexts: list[CameraCaptureContext] = []
def handle_camera_event(self):
- # cm.get_ready_requests() will not block here, as we know there is an event
- # to read.
+ # cm.get_ready_requests() returns the ready requests, which in our case
+ # should almost always return a single Request, but in some cases there
+ # could be multiple or none.
reqs = self.cm.get_ready_requests()
@@ -144,8 +140,7 @@ class CaptureContext:
for cam_ctx in self.camera_contexts:
for req in cam_ctx.reqs:
- ret = cam_ctx.cam.queue_request(req)
- assert ret == 0
+ cam_ctx.cam.queue_request(req)
# Use Selector to wait for events from the camera and from the keyboard
@@ -176,8 +171,7 @@ def main():
# Start the cameras
for cam_ctx in ctx.camera_contexts:
- ret = cam_ctx.cam.start()
- assert ret == 0
+ cam_ctx.cam.start()
ctx.capture()