| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# A robust script to run the COLMAP GUI inside a Docker container, |
| 4 |
# with automatic GPU detection and flexible argument passing. |
| 5 |
|
| 6 |
# --- Initial Checks --- |
| 7 |
# Exit immediately if a command exits with a non-zero status. |
| 8 |
set -e |
| 9 |
|
| 10 |
# --- Cleanup Function and Trap --- |
| 11 |
# This function will be called automatically when the script exits |
| 12 |
# to ensure that X server permissions are always restored. |
| 13 |
function cleanup { |
| 14 |
echo "Revoking X-server access..." |
| 15 |
# The '|| true' prevents the script from failing if xhost has issues. |
| 16 |
xhost -local:root > /dev/null || true |
| 17 |
} |
| 18 |
trap cleanup EXIT |
| 19 |
|
| 20 |
# Check if any argument is provided. |
| 21 |
if [ $# -eq 0 ]; then |
| 22 |
echo "Usage: $0 <host_directory>" |
| 23 |
echo "Example: $0 ../dataset/" |
| 24 |
exit 1 |
| 25 |
fi |
| 26 |
|
| 27 |
# --- Docker Image Selection --- |
| 28 |
# Check if local colmap:latest image exists (in case you ran build.sh), otherwise use official image |
| 29 |
if docker image inspect colmap:latest >/dev/null 2>&1; then |
| 30 |
echo "Using local COLMAP Docker image..." |
| 31 |
COLMAP_IMAGE="colmap:latest" |
| 32 |
else |
| 33 |
echo "Local COLMAP image not found, pulling official image..." |
| 34 |
docker pull colmap/colmap:latest |
| 35 |
COLMAP_IMAGE="colmap/colmap:latest" |
| 36 |
fi |
| 37 |
|
| 38 |
# Get absolute path |
| 39 |
HOST_DIR=$(realpath "$1") |
| 40 |
if [ ! -d "$HOST_DIR" ]; then |
| 41 |
echo "Error: Directory '$HOST_DIR' does not exist." |
| 42 |
exit 1 |
| 43 |
fi |
| 44 |
echo "Running COLMAP container with directory: $HOST_DIR" |
| 45 |
|
| 46 |
|
| 47 |
# --- Build Docker Arguments --- |
| 48 |
# Start with the base arguments for GUI forwarding. |
| 49 |
DOCKER_ARGS=( |
| 50 |
-it --rm |
| 51 |
--net=host |
| 52 |
-e DISPLAY |
| 53 |
-v "${HOST_DIR}:/working" |
| 54 |
-w /working |
| 55 |
) |
| 56 |
|
| 57 |
# --- GPU Detection and Configuration --- |
| 58 |
echo "Testing for GPU acceleration..." |
| 59 |
# A successful `nvidia-smi` call is the most reliable test. |
| 60 |
if docker run --rm --runtime=nvidia "${COLMAP_IMAGE}" nvidia-smi >/dev/null 2>&1; then |
| 61 |
echo "✅ GPU detected. Using --runtime=nvidia and mapping all graphics devices." |
| 62 |
# Use the nvidia runtime AND also pass through the host's render devices. |
| 63 |
# This can solve driver conflicts on hybrid graphics systems. |
| 64 |
DOCKER_ARGS+=( |
| 65 |
--runtime=nvidia |
| 66 |
-e NVIDIA_DRIVER_CAPABILITIES=all |
| 67 |
) |
| 68 |
if [ -d /dev/dri ]; then |
| 69 |
DOCKER_ARGS+=( --device=/dev/dri ) |
| 70 |
fi |
| 71 |
else |
| 72 |
echo "⚠️ GPU not detected. Falling back to CPU rendering." |
| 73 |
# For CPU mode, we give the container access to the host's render devices. |
| 74 |
if [ -d /dev/dri ]; then |
| 75 |
DOCKER_ARGS+=( --device=/dev/dri ) |
| 76 |
fi |
| 77 |
fi |
| 78 |
|
| 79 |
# --- X11 Forwarding Security --- |
| 80 |
# Grant permissions just before running the container. |
| 81 |
xhost +local:root > /dev/null |
| 82 |
|
| 83 |
# --- Execute the Container --- |
| 84 |
# Pass all arguments after the directory path ("${@:2}") to the colmap gui command. |
| 85 |
echo "Launching GUI..." |
| 86 |
# "${@:2}" accepts extra arguments for the COLMAP GUI. |
| 87 |
docker run "${DOCKER_ARGS[@]}" "${COLMAP_IMAGE}" colmap gui "${@:2}" |
| 88 |
|
| 89 |
# The `trap` will automatically call the `cleanup` function here, |