This repository contains all scripts used during the 3D digitization of insect type specimens at the Entomological Collection at ETH Zürich. The digitization was performed using the 3D-Scanner from Small World Vision.
- Workflow
- Metashape Helper (01_metashape_helper)
- Create Web Optimized Model (02_export_decimate_batch)
- Miscellaneous Scripts (03_utils)
- Contact
After scanning the insect, our workflow looked like the following:
- Use the Metashape helper calculate script to create the model from the photo data.
- Use the Metashape helper export script to create an .obj file.
- Use the Decimate Export script to create a .glb file.
These scripts are designed to process multiple projects simultaneously, allowing us to scan many insects during the day and perform calculations overnight without human intervention.
This script helps with automating the model creation from the edof images.
- Calculate and export dataset models with only one click
- Adjust settings only once
- Automatically retrieve the parameters needed from the datasets (f number, camPos, image size, num images)
- Show a progress overview
- Log files for calculation and export
The datasets we get from the 3D-Scanners have the following structure:
ETHZ-ENT0574269 --> Dataset folder with unique number
├── edof --> Image folder (extended depth of focus)
│ ├── image_0001_-70_0.png --> Edof image
│ ├── image_0002_-70_29.2.png --> Edof image
│ ├── image_0003_-70_58.4.png --> Edof image
│ └── ... --> ~395 more edof images
├── ScanInformation.pdf --> File with scan parameters
└── CamPos.txt --> File with camera positions
We use four folders to indicate the dataset's state. Every dataset will pass through each folder.
1_SCANNED --> Here are all uncalculated datasets (directly from scanner) stored.
2_CALCULATED --> Here are all calculated datasets (calculated with the calculation helper) stored.
3_UNPINNED --> Here are all unpinned datasets (pin manually removed from insect) stored.
4_EXPORTED --> Here are all exported datasets (exported with the export helper) stored.
Our current workflow
To use the two helpers (calculate/export) do the following:
- Add datasets into the input folders.
- Execute the needed helper by clicking on of the
calculate.bat
orexport.bat
file. - Wait until the helper has finished processing the datasets or cancel the process by closing the window
If there has been an error you can check out the log file.
Because transparent wings are difficult to reproduce, a batch file is available to override standard settings. These settings inflate the model and then reduce it afterward, improving wing quality.
Ensure that all paths are correctly set in settings.py (and in the batch files if path changes are needed for special cases).
- The settings are validated (
settings.py
&settings_validator.py
). - If settings are passed through the command line, the standard settings are overriden.
- The window is created (
helper_window.py
&tkinter_helper.py
) - A new thread is created for the calculation/export (
calculate.py
&export.py
) - The datasets are retrieved from the calculation/export input folder (
dataset_helper.py
) - All datasets are calculated/exported (by
metashape_helper.py
) - The calculated/exported detasets are moved to the calculation/export output folder (
dataset_helper.py
)
The .obj
models of the insects are roughly 500MB in size, which is too large for use on websites. To reduce the model size, we follow these steps:
- Decimate model (reduce polygon count to 1000000). For many applications, this can be reduced further.
- Remap the texture
- Export the model as a
glb
file and apply a fixed metallic/roughness value.
This process is implemented in the decimate_export_batch.py
script, using Metashape for steps 1 and 2, and pygltflib for the metallic/roughness value. Additionally, the script:
- Exports the camera positions as an xml file using Metashape. This is necessary to use the models with the photogrammetry viewer.
- Saves all images in the edof folder as JPGs in a folder called redof.
- Moves the entire project to a new folder
There are two ways to run this script:
- Install Metashape as python module
- Run the script inside Metashape
Right now decimate_export_batch.bat
, which starts the python script, uses the second variant.
The full high-poly model. | After decimation |
---|---|
The insect | The corresponding .glb |
---|---|
- Install the required libraries: pygltflib and Pillow.
- You can run this script in different ways:
- Install Metashape as a Python module following the instructions here, or
- Launch the script directly within Metashape. The batch files are configured for this method. Ensure that the Metashape path is correctly specified in the batch files.
- Adjust the settings in
settings.py
as needed (documentation is provided within the file). - Execute the script using either the batch file or a Python interpreter of your choice, depending on your approach in step
This process takes roughly 5 minutes per insect. If the files are saved on a server it might take slightly longer.
This folder contains various smaller scripts.
The create_preview
project creates three predetermined pictures of the glb models. This is allows for an easy quality control. The current implementation uses a flask server. The extracted images all have a black background. To change this we use a Photoshop automation which automatically adds a white background with a subtle gradient and then exports the file again as a JPEG-file. In the end we have three images:
top-view | bottom_view | side-view |
---|---|---|
This can be used in combination with Extract Images to quickly assess the quality of the glb files.
To run this script, ensure you have Flask installed and access to a web browser.
- Go to the
create_preview
folder. - Update the script by replacing
static_folder
with the path to the folder containing your projects. - Move the
main.js
file to the path given above. - Start the Flask server using one of the following commands, depending on your Flask installation:
python3.12.exe -m flask --app app run
orflask --app app run
- Open
localhost:8000
in your web browser and press "Start". The program will then automatically capture images of the 3D model from three different perspectives. The images will be saved to your Downloads folder. Make sure your browser is in the foreground the entire time.
The extract_projects.py
file writes all subfolders of a directory to a file. This is useful to extract all projects in a directory.
The mCamPos_Inner_Outer_NS.py
script is used to automatically duplicates all lines in the CamPos.txt
file to account for the newly created masking images. This is achieved by simply adding inner
and outer
to the image names. This is only required when the insect is placed on top of plexiglass (Lucerne Collection). The new files is saved under mCamPos.txt
.
The rotate_obj_blender.py
script imports all .obj
files in blender exports, rotates them and saves the resulting blender project. This assumes all project folders share the same structure, folders that don’t fit this structure get skipped. It also sets the transform orientation to local.
Extracts images that are closest to a list of positions. Used in combination with Create Preview for quality check.
Copies a file (ScanInformation.pdf
) and saves it under a different name for all projects.
Extracts the keywords and rating of the images and exports them as a CSV file. It also keywords of different images of the same insect, such that every insect has a single entry in the CSV file.
Splits a PDF into several files where each page in the original is now its own file.
Overwrites certain values in the ScanInformation.pdf
. It takes these values from a CSV file. It extracts all text from the pdf and writes to a completely new file. This means that the new file look different from the original.
Extracts certain values from the ScanInformation.pdf
file and saves them in a CSV file.
The script final_check
ensures all project folders have the following files and folders:
- cameras.xml
- CamPos.txt
- ScanInformation.pdf or ParamsText.txt
- edof
- png images
- redof
- jpg images
- Model
- MODEL_NAME.files
- MODEL_NAME.glb
- MODEL_NAME.mtl
- MODEL_NAME.obj
- MODEL_NAME.png
- MODEL_NAME.psx
It also ensures that not too many or too few files exist and allows for some variance. The old folders contain additional files we want to keep. It also makes sure the edof and redof folder contain the same number of images.
These small scripts are standalone programs. Install all dependencies and run them. Each script includes comments for documentation. They are not needed for the process but were helpful for us during the project.
- Dr. Michael Greeff - [email protected]
- Christian Felsner - [email protected]