Skip to content

Commit

Permalink
Samples: Add loading projector image with Zivid API
Browse files Browse the repository at this point in the history
Current samples load an image of arbitrary resolution.
OpenCV is then used to resize the image to match the
resolution of the Zivid camera projector. In this
commit, we are adding options to load an image that
matches the resolution of the camera projector, using
Zivid API.
  • Loading branch information
csu-bot-zivid authored and SatjaSivcev committed Dec 20, 2023
1 parent e4654f2 commit 82fa18a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pyproject.constraints
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated file, do not edit @ 2023-10-30 22:22:03
#
# Command:
# python infrastructure/tools/dependency_updater/dependency_updater.py --remote-update --remote-name origin --base-branch master --access-token <ACCESS_TOKEN>
#
# To update all Python requirements and constraints manually, you should run:
#
# python infrastructure/tools/dependency_updater/dependency_updater.py
#
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --all-extras --extra-index-url=http://se-ci-elastic-server-1.localdomain:8081/artifactory/api/pypi/zivid-pypi/simple --output-file=sdk/samples/public/python/pyproject.constraints --strip-extras --trusted-host=se-ci-elastic-server-1.localdomain sdk/samples/public/python/pyproject.toml
#
--extra-index-url http://se-ci-elastic-server-1.localdomain:8081/artifactory/api/pypi/zivid-pypi/simple
--trusted-host se-ci-elastic-server-1.localdomain

43 changes: 40 additions & 3 deletions source/applications/basic/visualization/read_and_project_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,37 @@ def _resize_and_create_projector_image(image_to_resize: np.ndarray, final_resolu
return projector_image


def get_projector_image_file_for_camera(camera):
"""Provides the path to the projector image for a given camera.
Args:
camera: Zivid camera
Returns:
Path to the projector image
Raises:
RuntimeError: Invalid camera model
"""
model = camera.info.model
if model in [zivid.CameraInfo.Model.zividTwo, zivid.CameraInfo.Model.zividTwoL100]:
return get_sample_data_path() / "ZividLogoZivid2ProjectorResolution.png"
if model in [
zivid.CameraInfo.Model.zivid2PlusM130,
zivid.CameraInfo.Model.zivid2PlusM60,
zivid.CameraInfo.Model.zivid2PlusL110,
]:
return get_sample_data_path() / "ZividLogoZivid2PlusProjectorResolution.png"
raise RuntimeError("Invalid camera model")


def _main() -> None:
with zivid.Application() as app:
print("Connecting to camera")
with app.connect_camera() as camera:
image_file = get_sample_data_path() / "ZividLogo.png"
print("Reading 2D image from file: ")
print("Reading 2D image (of arbitrary resolution) from file: ")
input_image = cv2.imread(str(image_file))
if input_image is None:
raise RuntimeError(f"File {image_file} not found or couldn't be read.")
Expand All @@ -61,7 +86,7 @@ def _main() -> None:

print("Displaying the projector image")

with zivid.experimental.projection.show_image_bgra(camera, projector_image) as projected_image:
with zivid.experimental.projection.show_image_bgra(camera, projector_image) as projected_image_handle:
settings_2d = zivid.Settings2D()
settings_2d.acquisitions.append(
zivid.Settings2D.Acquisition(
Expand All @@ -70,14 +95,26 @@ def _main() -> None:
)

print("Capturing a 2D image with the projected image")
frame_2d = projected_image.capture(settings_2d)
frame_2d = projected_image_handle.capture(settings_2d)

captured_image_file = "CapturedImage.png"
print(f"Saving the captured image: {captured_image_file}")
frame_2d.image_bgra().save(captured_image_file)

input("Press enter to stop projecting ...")

projector_image_file_for_given_camera = get_projector_image_file_for_camera(camera)

print(
f"Reading 2D image (of resolution matching the Zivid camera projector resolution) from file: {projector_image_file_for_given_camera}"
)
projector_image_for_given_camera = zivid.Image.load(projector_image_file_for_given_camera, "bgra")

with zivid.experimental.projection.show_image_bgra(
camera, projector_image_for_given_camera.copy_data()
) as projected_image_handle:
input("Press enter to stop projecting ...")


if __name__ == "__main__":
_main()

0 comments on commit 82fa18a

Please sign in to comment.