We have developed a script to automate the background masking process. 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.

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


This article describes the following processing steps:


How to install  

Windows:

Note: that 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 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:

Note: that 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 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 

Note: 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. 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.