Skip to content

Otsu threshold

Goal

Create a container and run the otsu step in the container image tagged localhost/otsu:latest.

Lab

This step has a dedicated lab at practice-labs/2-Containers/otsu.ipynb.

Container

Each step has its own recipe to build the container image.

The otsu step container image recipe is:

otsu/Dockerfile
FROM docker.io/library/python:3.12-slim@sha256:a6e34c598f2467ed0e9a8d349809fcd8b5c603269512df273a0bb1784edc11b1 AS builder
WORKDIR /src
COPY pyproject.toml README.md LICENSE.md ./
COPY src/ ./src/
RUN python -m pip wheel --no-cache-dir --no-deps --wheel-dir /dist .

FROM docker.io/library/python:3.12-slim@sha256:a6e34c598f2467ed0e9a8d349809fcd8b5c603269512df273a0bb1784edc11b1 AS runtime
RUN apt-get update && \
    apt-get install -y --no-install-recommends libexpat1 && \
    rm -rf /var/lib/apt/lists/* && \
    groupadd --gid 2000 neo && \
    useradd --uid 2000 --gid 2000 --create-home neo && \
    python -m venv /app/venv && \
    mkdir -p /work && chown 2000:2000 /work
ENV VIRTUAL_ENV=/app/venv
ENV PATH="/app/venv/bin:$PATH"
COPY --from=builder /dist/ /tmp/wheels/
RUN pip install --no-cache-dir /tmp/wheels/*.whl && \
    otsu --help && \
    rm -rf /tmp/wheels
USER 2000:2000
WORKDIR /work
ENTRYPOINT []

Build the container image with:

terminal
1
2
3
export WORKSPACE=/workspace/mastering-app-package

podman build --format docker -t localhost/otsu:latest ${WORKSPACE}/water-bodies/command-line-tools/otsu

Run the container

The command to run the otsu step in the container is:

podman \
    run \
    -i \
    --userns=keep-id \
    --mount=type=bind,source=/workspace/mastering-app-package/runs,target=/runs \
    --mount=type=bind,source=/workspace/mastering-app-package/runs/norm_diff.tif,target=/inputs/norm_diff.tif,readonly \
    --workdir=/runs \
    --read-only=true \
    --user="$(id -u):$(id -g)" \
    --rm \
    --env=HOME=/runs \
    localhost/otsu:latest \
    otsu \
    --raster /inputs/norm_diff.tif

Step

Run the command to run the otsu step with:

sh -x ${WORKSPACE}/scripts/podman-otsu.sh

Expected outcome

The folder /workspace/mastering-app-package/runs contains:

(base) jovyan@coder-mrossi:~/runs$ tree .
.
├── crop_green.tif
├── crop_nir.tif
├── norm_diff.tif
└── otsu.tif

0 directories, 4 files