Skip to content

Commit

Permalink
Samples: Update bgra() method
Browse files Browse the repository at this point in the history
This commit updates all relevant samples to use hte Zivid API for
extracting `bgr` color information, instead of using third-part library.
  • Loading branch information
csu-bot-zivid authored and chrisasc committed May 31, 2023
1 parent 841004e commit 48c5d3a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 50 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,34 @@ from the camera can be used.

## Installation

-----

Note:

The recommended Python version for these samples is 3.6 - 3.9.

-----

1. [Install Zivid
Software](https://support.zivid.com/latest//getting-started/software-installation.html).

2. [Install Zivid
Python](https://github.com/zivid/zivid-python#installation). Note:
The recommended Python version for these samples is 3.6 - 3.9.

3. [Download Zivid Sample
2. [Download Zivid Sample
Data](https://support.zivid.com/latest//api-reference/samples/sample-data.html).

4. Install the runtime requirements using IDE or command line:
3. Install the runtime requirements using IDE or command line:

``` sourceCode
``` sourceCode bash
pip install -r requirements.txt
```

5. Add the directory source to PYTHONPATH. Navigate to the root of the
4. Add the directory source to PYTHONPATH. Navigate to the root of the
repository and run:

- PowerShell: `$env:PYTHONPATH=$env:PYTHONPATH + ";$PWD\source"`
- cmd: `set PYTHONPATH="$PYTHONPATH;$PWD\source"`
- bash: `export PYTHONPATH="$PYTHONPATH:$PWD/source"`

> - PowerShell: `$env:PYTHONPATH=$env:PYTHONPATH + ";$PWD\source"`
> - cmd: `set PYTHONPATH="$PYTHONPATH;$PWD\source"`
> - bash: `export PYTHONPATH="$PYTHONPATH:$PWD/source"`
6. Open and run one of the samples.
5. Open and run one of the samples.

## Support

Expand Down
20 changes: 18 additions & 2 deletions continuous-integration/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR=$(realpath "$SCRIPT_DIR/..")

ZIVID_SDK_EXACT_VERSION=2.9.0+4dbba385-1
ZIVID_TELICAM_EXACT_VERSION=3.0.1.1-3

export DEBIAN_FRONTEND=noninteractive
source /etc/os-release || exit $?

function apt-yes {
apt-get --assume-yes "$@"
}

function install_www_deb {
TMP_DIR=$(mktemp --tmpdir --directory zivid-python-install-www-deb-XXXX) || exit $?
pushd $TMP_DIR || exit $?
wget -nv "$@" || exit $?
apt-yes install --fix-broken ./*deb || exit $?
popd || exit $?
rm -r $TMP_DIR || exit $?
}

apt-yes update || exit
apt-yes dist-upgrade || exit

apt-yes install \
python3-pip ||
python3-pip \
wget ||
exit $?

python3 -m pip install --upgrade pip || exit
install_www_deb "https://downloads.zivid.com/sdk/releases/${ZIVID_SDK_EXACT_VERSION}/u${VERSION_ID:0:2}/zivid-telicam-driver_${ZIVID_TELICAM_EXACT_VERSION}_amd64.deb" || exit $?
install_www_deb "https://downloads.zivid.com/sdk/releases/${ZIVID_SDK_EXACT_VERSION}/u${VERSION_ID:0:2}/zivid_${ZIVID_SDK_EXACT_VERSION}_amd64.deb" || exit $?

python3 -m pip install --upgrade pip || exit
python3 -m pip install --requirement "$ROOT_DIR/requirements.txt" || exit

echo Success! ["$(basename $0)"]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ open3d
pyyaml
scipy
robodk
zivid
17 changes: 6 additions & 11 deletions source/applications/advanced/create_depth_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cv2
import numpy as np
import zivid
from sample_utils.display import display_bgr
from sample_utils.paths import get_sample_data_path


Expand Down Expand Up @@ -44,27 +45,21 @@ def _point_cloud_to_cv_bgr(point_cloud: zivid.PointCloud) -> np.ndarray:
bgr: BGR image (HxWx3 ndarray)
"""
rgba = point_cloud.copy_data("rgba")
# Applying color map
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
bgra = point_cloud.copy_data("bgra")

return bgr
return bgra[:, :, :3]


def _visualize_and_save_image(image: np.ndarray, image_file: str, window_name: str) -> None:
def _visualize_and_save_image(image: np.ndarray, image_file: str, title: str) -> None:
"""Visualize and save image to file.
Args:
image: BGR image (HxWx3 ndarray)
image_file: File name
window_name: OpenCV Window name
title: OpenCV Window name
"""
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.imshow(window_name, image)
print("Press any key to continue")
cv2.waitKey(0)
cv2.destroyWindow(window_name)
display_bgr(image, title)
cv2.imwrite(image_file, image)


Expand Down
24 changes: 6 additions & 18 deletions source/applications/advanced/gamma_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cv2
import numpy as np
import zivid
from sample_utils.display import display_bgr


def _options() -> argparse.Namespace:
Expand Down Expand Up @@ -46,10 +47,9 @@ def _capture_bgr_image(camera: zivid.Camera, gamma: float) -> np.ndarray:

print("Capturing 2D frame")
with camera.capture(settings_2d) as frame_2d:
image = frame_2d.image_rgba()
rgba = image.copy_data()
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
return bgr
image = frame_2d.image_bgra()
bgra = image.copy_data()
return bgra[:, :, :3]


def _combine_images(image_one: np.ndarray, image_two: np.ndarray) -> np.ndarray:
Expand All @@ -69,19 +69,6 @@ def _combine_images(image_one: np.ndarray, image_two: np.ndarray) -> np.ndarray:
return combined_image


def _display_bgr(image: np.ndarray, bgr_name: str) -> None:
"""Display BGR image using OpenCV.
Args:
image: BGR image to be displayed
bgr_name: Name of the OpenCV window
"""
cv2.imshow(bgr_name, image)
print("Press any key to continue")
cv2.waitKey(0)


def _main() -> None:
app = zivid.Application()

Expand All @@ -100,7 +87,8 @@ def _main() -> None:

print(f"Displaying color image before and after gamma correction: {user_options.gamma}")
combined_image = _combine_images(bgr_original, bgr_adjusted)
_display_bgr(combined_image, "Original on left, adjusted on right")
cv2.imwrite("combined_image.jpg", combined_image)
display_bgr(combined_image[:, :, 0:3], title="Original on left, adjusted on right")


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions source/applications/basic/file_formats/convert_zdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ def _convert_2_2d(point_cloud: zivid.PointCloud, file_name: str) -> None:
"""
print(f"Saving the frame to {file_name}")
rgba = point_cloud.copy_data("rgba")
bgr = cv2.cvtColor(rgba, cv2.COLOR_RGBA2BGR)
cv2.imwrite(file_name, bgr)
bgra = point_cloud.copy_data("bgra")
cv2.imwrite(file_name, bgra[:, :, :3])


def _main() -> None:
Expand Down
6 changes: 3 additions & 3 deletions source/sample_utils/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def display_rgb(rgb: np.ndarray, title: str = "RGB image", block: bool = True) -
plt.show(block=block)


def display_bgr(bgr: np.ndarray, bgr_name: str) -> None:
def display_bgr(bgr: np.ndarray, title: str = "RGB image") -> None:
"""Display BGR image using OpenCV.
Args:
bgr: BGR image (HxWx3 ndarray)
bgr_name: Name of the OpenCV window
title: Name of the OpenCV window
"""
cv2.imshow(bgr_name, bgr)
cv2.imshow(title, bgr)
print("Press any key to continue")
cv2.waitKey(0)

Expand Down

0 comments on commit 48c5d3a

Please sign in to comment.