From df61f3992dacf52b6b157409d4ad9947befe7c56 Mon Sep 17 00:00:00 2001 From: hesuicong Date: Mon, 21 Jul 2025 10:10:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker/Dockerfile | 16 +++++++ docker/QUICK_START.sh | 2 + docker/README.md | 37 +++++++++++++++ docker/buildFromScratch.cmd | 49 +++++++++++++++++++ docker/buildFromScratch.sh | 47 ++++++++++++++++++ docker/buildInDocker.sh | 95 +++++++++++++++++++++++++++++++++++++ 6 files changed, 246 insertions(+) create mode 100644 docker/Dockerfile create mode 100755 docker/QUICK_START.sh create mode 100644 docker/README.md create mode 100644 docker/buildFromScratch.cmd create mode 100755 docker/buildFromScratch.sh create mode 100755 docker/buildInDocker.sh diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..95a5c5d --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,16 @@ +ARG BASE_IMAGE=ubuntu:22.04 + +FROM $BASE_IMAGE + +ARG MASTER=0 +ARG USER_ID +ARG GROUP_ID +ARG CUDA=0 + +COPY buildInDocker.sh /tmp/buildInDocker.sh +RUN /tmp/buildInDocker.sh --cuda $CUDA --user_id $USER_ID --group_id $GROUP_ID --master $MASTER && rm /tmp/buildInDocker.sh + +USER user + +# Add binaries to path +ENV PATH /usr/local/bin/OpenMVS:$PATH diff --git a/docker/QUICK_START.sh b/docker/QUICK_START.sh new file mode 100755 index 0000000..cd5caa1 --- /dev/null +++ b/docker/QUICK_START.sh @@ -0,0 +1,2 @@ +docker pull openmvs/openmvs-ubuntu:latest +docker run -w /working -v $1:/working -it openmvs/openmvs-ubuntu:latest diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..f80a7ce --- /dev/null +++ b/docker/README.md @@ -0,0 +1,37 @@ + +# Building & running openMVS using Docker + +## Quick start: + +1. Make sure docker is installed on your local machine. +2. Run the 'easy start' script, using the *full local path* to the folder with your SFM input files (perhaps output from openMVG or COLMAP): + +``` +./QUICK_START.sh /path/where/your/SFM/results/are +``` + +3. This will put you in a directory (inside the Docker container) mounted to the local path you specified so that you can run openMVS binaries on your own SFM inputs. Enjoy! + +## Build from scratch: + +You can also build the docker image from scratch based on the **Dockerfile** (perhaps with your own changes / modifications) using: + +```sh +./buildFromScratch.sh --workspace /path/where/your/SFM/results/are +# With CUDA support: +./buildFromScratch.sh --cuda --workspace /path/where/your/SFM/results/are +# From master branch: +./buildFromScratch.sh --master --workspace /path/where/your/SFM/results/are +``` + +## NOTES + ++ This workflow is pinned to build from [openMVS 1.0](https://github.com/cdcseacave/openMVS/releases/tag/v1.0). To build from a different release, or build from the latest commit on master, open up the Dockerfile and comment/uncomment as directed. + ++ Display is also supported inside docker. If there are any issues with `xauth` make sure to run `xhost +` on your host machine. + ++ Running openMVS binaries can use a lot of memory (depending on the size of your data set/ imagery). Docker has a relatively small default memory setting (2Gb on Mac). You will probably want to increase this before you run any larger workflows. From Docker desktop on Mac for example, just open the Docker GUI, go to the *Advanced* tab and increase via the slider: + +![alt text][dockerParam] + +[dockerParam]: https://i.stack.imgur.com/6iWiW.png "Recommend increasing memory to >4Gb" diff --git a/docker/buildFromScratch.cmd b/docker/buildFromScratch.cmd new file mode 100644 index 0000000..bbb10a1 --- /dev/null +++ b/docker/buildFromScratch.cmd @@ -0,0 +1,49 @@ +@echo off + +set WORKSPACE=%CD% + +set CUDA_BUILD_ARGS= +set CUDA_RUNTIME_ARGS= +set CUDA_CONTAINER_SUFFIX= +set MASTER_ARGS= +set DISPLAY_ARGS= + +:parse_args +if "%~1" == "" goto build_image +if "%~1" == "--cuda" ( + set "CUDA_BUILD_ARGS=--build-arg CUDA=1 --build-arg BASE_IMAGE=nvidia/cuda:11.8.0-devel-ubuntu22.04" + set "CUDA_RUNTIME_ARGS=--gpus all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,graphics" + set "CUDA_CONTAINER_SUFFIX=-cuda" + shift + goto parse_args +) +if "%~1" == "--master" ( + set "MASTER_ARGS=--build-arg MASTER=1" + shift + goto parse_args +) +if "%~1" == "--workspace" ( + set "WORKSPACE=%~2" + shift + shift + goto parse_args +) +if "%~1" == "--gui" ( + set "XSOCK=\\.\pipe\X11-unix" + set "XAUTH=%TEMP%\docker.xauth" + set "DISPLAY_ARGS=-e ^"DISPLAY=%COMPUTERNAME%:0.0^" -v ^"%XAUTH%:%XAUTH%:rw^" -e ^"XAUTHORITY=%XAUTH%^"" + shift + goto parse_args +) +echo Unknown argument: %~1 +exit /b 1 + +:build_image +echo Running with workspace: %WORKSPACE% + +docker build -t="openmvs-ubuntu%CUDA_CONTAINER_SUFFIX%" --build-arg "USER_ID=1001" --build-arg "GROUP_ID=1001" %CUDA_BUILD_ARGS% %MASTER_ARGS% . +if errorlevel 1 exit /b 1 + +docker run %CUDA_RUNTIME_ARGS% --entrypoint bash --ipc=host --shm-size=4gb -w /work -v "%WORKSPACE%:/work" %DISPLAY_ARGS% -it openmvs-ubuntu%CUDA_CONTAINER_SUFFIX% + +exit /b 0 diff --git a/docker/buildFromScratch.sh b/docker/buildFromScratch.sh new file mode 100755 index 0000000..3d3e6dd --- /dev/null +++ b/docker/buildFromScratch.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# Example use: +# ./buildFromScratch.sh --cuda --master --workspace /home/username/datapath/ + +WORKSPACE=$(pwd) + + +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + --cuda) + CUDA_BUILD_ARGS="--build-arg CUDA=1 --build-arg BASE_IMAGE=nvidia/cuda:11.8.0-devel-ubuntu22.04" + + CUDA_RUNTIME_ARGS="--gpus all -e NVIDIA_DRIVER_CAPABILITIES=compute,utility,graphics" + + CUDA_CONTAINER_SUFFIX="-cuda" + shift + ;; + --master) + MASTER_ARGS="--build-arg MASTER=1" + shift + ;; + --workspace) + WORKSPACE=$2 + shift + shift + ;; + *) + echo "Unknown argument: $key" + exit 1 + ;; + esac +done + +# no need to do `xhost +` anymore +XSOCK=/tmp/.X11-unix +XAUTH=/tmp/.docker.xauth +touch $XAUTH +xauth nlist "$DISPLAY" | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge - +DISPLAY_ARGS="--volume=$XSOCK:$XSOCK:rw --volume=$XAUTH:$XAUTH:rw --env=XAUTHORITY=$XAUTH --env=DISPLAY=unix$DISPLAY" + +echo Running with workspace: "$WORKSPACE" + +docker build -t="openmvs-ubuntu$CUDA_CONTAINER_SUFFIX" --build-arg "USER_ID=$(id -u)" --build-arg "GROUP_ID=$(id -g)" $CUDA_BUILD_ARGS $MASTER_ARGS . +docker run $CUDA_RUNTIME_ARGS --entrypoint bash --ipc=host --shm-size=4gb -w /work -v "$WORKSPACE:/work" $DISPLAY_ARGS -it openmvs-ubuntu$CUDA_CONTAINER_SUFFIX + diff --git a/docker/buildInDocker.sh b/docker/buildInDocker.sh new file mode 100755 index 0000000..c33917a --- /dev/null +++ b/docker/buildInDocker.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +set -e + +# Parse arguments to see if --cuda was passed and equals 1 or --master was passed and equals 1 +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + --cuda) + CUDA="$2" + shift + shift + ;; + --master) + MASTER="$2" + shift + shift + ;; + --user_id) + USER_ID="$2" + shift + shift + ;; + --group_id) + GROUP_ID="$2" + shift + shift + ;; + *) + echo "Unknown argument: $key" + exit 1 + ;; + esac +done + +if [[ "$CUDA" == "1" ]]; then + echo "Building with CUDA support" + EIGEN_BUILD_ARG="-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/" + OPENMVS_BUILD_ARG="-DOpenMVS_USE_CUDA=ON -DCMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs/ -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda/ -DCUDA_INCLUDE_DIRS=/usr/local/cuda/include/ -DCUDA_CUDART_LIBRARY=/usr/local/cuda/lib64 -DCUDA_NVCC_EXECUTABLE=/usr/local/cuda/bin/" +else + echo "Building without CUDA support" + EIGEN_BUILD_ARG="" + OPENMVS_BUILD_ARG="-DOpenMVS_USE_CUDA=OFF" +fi + +if [[ "$MASTER" == "1" ]]; then + echo "Pulling from master branch" +else + echo "Pulling from develop branch" +fi + +apt-get update -yq + +apt-get -yq install build-essential git cmake libpng-dev libjpeg-dev libtiff-dev libglu1-mesa-dev libglew-dev libglfw3-dev + +# Eigen +git clone https://gitlab.com/libeigen/eigen --branch 3.4 +mkdir eigen_build +cd eigen_build &&\ + cmake . ../eigen $EIGEN_BUILD_ARG &&\ + make && make install &&\ + cd .. && rm -rf eigen_build eigen + +# Boost +apt-get -y install libboost-iostreams-dev libboost-program-options-dev libboost-system-dev libboost-serialization-dev + +# OpenCV +DEBIAN_FRONTEND=noninteractive apt-get install -yq libopencv-dev + +# CGAL +apt-get -yq install libcgal-dev libcgal-qt5-dev + +# VCGLib +git clone https://github.com/cdcseacave/VCG.git vcglib + +# Build from stable openMVS release or the latest commit from the develop branch +if [[ "$MASTER" == "1" ]]; then + git clone https://github.com/cdcseacave/openMVS.git --branch master +else + git clone https://github.com/cdcseacave/openMVS.git --branch develop +fi + +mkdir openMVS_build +cd openMVS_build &&\ + cmake . ../openMVS -DCMAKE_BUILD_TYPE=Release -DVCG_ROOT=/vcglib $OPENMVS_BUILD_ARG + +# Install OpenMVS library +make -j4 &&\ + make install &&\ + cd .. && rm -rf openMVS_build vcglib + +# Set permissions such that the output files can be accessed by the current user (optional) +echo "Setting permissions for user $USER_ID:$GROUP_ID" +addgroup --gid $GROUP_ID user &&\ + adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID user