We have developed a script to automate the background masking process in Agisoft Metashape Professional. The script is available on our GitHub page - https://github.com/agisoft-llc/metashape-scripts/blob/master/src/automatic_masking.py#L9-L29. The script is only available for the professional version of the program, starting from version 1.7.4. This script is suitable for custom cases with a turntable.

This article describes the following processing steps:

The latest Agisoft Metashape Professional release version is available on our website: https://www.agisoft.com/downloads/installer/.


Starting from version 2.0 and using the latest version of the script, you don't need to install additional libraries, they will be installed automatically during the script run.



How to install 


Windows



You will need around 14 GB of free space on drive C:


1. Launch cmd.exe with the administrator privileges


2. "%programfiles%\Agisoft\Metashape Pro\python\python.exe" -m pip install --use-feature=2020-resolver rembg==2.0.10 torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html 

3. To not encounter the error "Attempted to compile AOT function without the compiler used by numpy.distutils present. Cannot find suitable msvc.":

    3.1 Open https://visualstudio.microsoft.com/visual-cpp-build-tools/

    3.2 Download and launch 'Build Tools'

    3.3 Tick "Desktop development with C++" and after that in "Installation details" tick "MSVC v140 - VS 2015 C++ build tools" - see screenshot on forum https://www.agisoft.com/forum/index.php?topic=11387.msg54298#msg54298

    3.4 Reboot the computer

4. To not encounter error "...\aot.cp38-win_amd64.lib" failed with exit status 1104":

    4.1 Launch cmd.exe with the administrator privileges

    4.2 "%programfiles%\Agisoft\Metashape Pro\python\python.exe" -c "import rembg; import rembg.bg"

5. Add this script to auto-launch - https://agisoft.freshdesk.com/support/solutions/articles/31000133123-how-to-run-python-script-automatically-on-metashape-professional-start

copy automatic_masking.py script to C:/Users/<username>/AppData/Local/Agisoft/Metashape Pro/scripts/


Linux:



You will need around 5 GB of free space in Metashape Pro installation location.


1. cd .../metashape-pro 

LD_LIBRARY_PATH=`pwd`/python/lib/ python/bin/python3.8 -m pip install rembg==2.0.10 torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html 


2. Add this script to auto-launch - https://agisoft.freshdesk.com/support/solutions/articles/31000133123-how-to-run-python-script-automatically-on-metashape-professional-start

copy automatic_masking.py script to /home/<username>/.local/share/Agisoft/Metashape Pro/scripts/



Workflow


Add photos 


Add photos using Add Photos... command from Workflow menu. In the Add Photos dialog, browse the source folder and select files to be processed. The images will be added to the active chunk of the project.



Run script 


before starting using script, we recommend open Console pane to view information about the script operation. To open Console pane, select View > Console pane. 


Select Custom Menu > Automatic background masking for starting masking background. Please wait for the script to finish working, the time of the masking process will depend on your set and the number of photos in the project. 


Our script uses library with the neural network. Therefore, when you first run the script, it will take time for this library to download and the effect of "freezing" Metashape may occur. Please wait a maximum of 5 - 10 minutes for the library to download. On further runs of the script, this this effect will not happen again because this file will be reused from disk.


After ending the process to display the mask in Model view click Show mask on the Toolbar. 



To see which image masks are created, you can also click on the Show mask in the Photo pane.