Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalerCrops handling #1087

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/app_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ def toggle_hidden_controls():
"AfWindows",
"AfPause",
"AfMetering",
"ScalerCrops"
}

# Main widgets
Expand Down
2 changes: 1 addition & 1 deletion picamera2/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def align(self, optimal=True):


class StreamConfiguration(Configuration):
_ALLOWED_FIELDS = ("size", "format", "stride", "framesize")
_ALLOWED_FIELDS = ("size", "format", "stride", "framesize", "preserve_ar")
_FIELD_CLASS_MAP = {}
_FORWARD_FIELDS = {}

Expand Down
23 changes: 16 additions & 7 deletions picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ def _make_initial_stream_config(stream_config: dict, updates: dict, ignore_list=
"""
if updates is None:
return None
valid = ("format", "size", "stride")
valid = ("format", "size", "stride", "preserve_ar")
for key, value in updates.items():
if isinstance(value, SensorFormat):
value = str(value)
Expand Down Expand Up @@ -687,9 +687,9 @@ def create_preview_configuration(self, main={}, lores=None, raw={}, transform=li
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (640, 480)}, main)
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (640, 480), "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -721,9 +721,9 @@ def create_still_configuration(self, main={}, lores=None, raw={}, transform=libc
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "BGR888", "size": self.sensor_resolution}, main)
main = self._make_initial_stream_config({"format": "BGR888", "size": self.sensor_resolution, "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -755,9 +755,9 @@ def create_video_configuration(self, main={}, lores=None, raw={}, transform=libc
if not self._is_rpi_camera():
raw = None
sensor = None
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (1280, 720)}, main)
main = self._make_initial_stream_config({"format": "XBGR8888", "size": (1280, 720), "preserve_ar": True}, main)
self.align_stream(main, optimal=False)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"]}, lores)
lores = self._make_initial_stream_config({"format": "YUV420", "size": main["size"], "preserve_ar": False}, lores)
if lores is not None:
self.align_stream(lores, optimal=False)
raw = self._make_initial_stream_config({"format": self.sensor_format, "size": main["size"]},
Expand Down Expand Up @@ -1116,6 +1116,15 @@ def configure_(self, camera_config="preview") -> None:
self.controls = Controls(self, controls=self.camera_config['controls'])
self.configure_count += 1

if "ScalerCrops" in self.camera_controls:
par_crop = self.camera_controls["ScalerCrops"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'm guessing par_crop means preserve_ar_crop? Actually, for my curiosity, how does this work? Presumably libcamera is calculating the AR-preserving crop for each of the cameras - except I thought asking for camera controls was supposed to give us min/max/default triple. So I think I'm a bit confused!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably libcamera is calculating the AR-preserving crop for each of the cameras - except I thought asking for camera controls was supposed to give us min/max/default triple. So I think I'm a bit confused!

This is a bit of a hack where the controlinfo for rpi::ScalerCrops actually gives us the default (i.e. preserved ar) crop for output 0 and output 1 in the min/max fields. Unfortunately there is no other mechanism that can be used to get this out for span controls.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yes, that's a bit sneaky! Maybe worth a comment as it was a bit head-scratchy?

full_fov = self.camera_controls["ScalerCrop"][1]
scaler_crops = [par_crop[0] if camera_config["main"]["preserve_ar"] else full_fov]
if self.lores_index >= 0:
scaler_crops.append(par_crop[1] if camera_config["lores"]["preserve_ar"] else scaler_crops[0])
self.set_controls({"ScalerCrops": scaler_crops})


def configure(self, camera_config="preview") -> None:
"""Configure the camera system with the given configuration."""
self.configure_(camera_config)
Expand Down
24 changes: 24 additions & 0 deletions tests/crop_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/python3

# Test setting the "preserve_ar" stream configuration flag

import cv2

from picamera2 import Picamera2

picam2 = Picamera2()

for m, l in [(False, False), (False, True), (True, False), (True, True)]:
cfg = picam2.create_video_configuration(main={"size": (1920, 1080), "format": 'XRGB8888', "preserve_ar": m},
lores={"size": (640, 640), "format": 'XRGB8888', "preserve_ar": l},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering what this does on a Pi 4 where you can't have RGB on the lores?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - I assumed the validate() would change this to YUV and everything would be good. But I've not explicitly tried it...

Actually, there's no reason lores needs to be RGB, perhaps I should just change it to YUV.

display="main")
picam2.configure(cfg)
picam2.start(show_preview=True)

for _ in range(100):
im = picam2.capture_array("lores")
cv2.imshow("lores", im)
cv2.resizeWindow("lores", (640, 640))
cv2.waitKey(1)

picam2.stop()
1 change: 1 addition & 0 deletions tests/test_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tests/colour_spaces.py
tests/config_with_sensor.py
tests/configurations.py
tests/context_test.py
tests/crop_test.py
tests/display_transform_null.py
tests/display_transform_qt.py
tests/easy_video2.py
Expand Down
Loading