Forum

Author Topic: Python script to generate an orthomosaic from the drone's photo folder  (Read 7134 times)

Sylvain M.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Hi everyone,

I'm a beginner on Metashape, as well as on Python.
My goal is to automate in Python a processing chain to create an orthomosaic from a folder of multispectral photos (from a DJI Mavic 3M drone).
For the moment, I've created an Agisoft batch job (XML) that meets my needs.

By any chance, is there a tool that can convert a batch job into a Python script?

If not, I'd like to take advantage of this thread to work on this script step by step, with the help of anyone who can help me.
So I'll start by sharing the first bits of code I've written (I confess, with the help of ChatGPT), which could perhaps be optimised?

Code: [Select]
import Metashape
import os

# Params
project_path = r"D:\PROJECTPATH\MYPROJECT.psx"
photos_folder = r"D:\PHOTOSPATH"

# Create project and chunk
doc = Metashape.Document()
doc.save(path=project_path)
chunk = doc.addChunk()

# Photos import
# I don't think this method is optimal: it should be possible to specify that this is a multi-camera system
photo_list = [os.path.join(photos_folder, photo) for photo in os.listdir(photos_folder) if photo.lower().endswith(('.jpg', '.jpeg', '.png', '.tif', '.tiff'))]
chunk.addPhotos(photo_list)

doc.save()

Thank you very much for any help you can give me, or any examples you can send me links to.

Sylvain M.

  • Newbie
  • *
  • Posts: 18
    • View Profile
PS. : I forgot to mention that I'm a French speaker: please excuse me if some of the wording isn't optimal or understandable!  ;)

Sylvain M.

  • Newbie
  • *
  • Posts: 18
    • View Profile
I found this:
https://github.com/agisoft-llc/metashape-scripts/blob/master/src/samples/general_workflow.py

This will help me a lot!  :D
I'll be back soon with a more complete code!
(but maybe not for several days, as I'm working on other things in parallel)

Sylvain M.

  • Newbie
  • *
  • Posts: 18
    • View Profile
Now I've managed to write a script that sequences the necessary tasks.
I'm sharing it here to get your feedback and possible optimizations :
(I just deleted the print() which was in French)

Code: [Select]
import Metashape
import os, sys, time

# Params
project_path =  r"D:\PATH_TO_PROJECT\Project.psx"
photos_folder = r"D:\PATH_TO_PHOTOS\Photos"
ortho_folder =  r"D:\PATH_TO_ORTHO\Orthomosaic.tif"

# Find files function
def find_files(folder, types):
    return [entry.path for entry in os.scandir(folder) if (entry.is_file() and os.path.splitext(entry.name)[1].lower() in types)]

# Create Project and Chunk
doc = Metashape.Document()
doc.save(path=project_path)
chunk = doc.addChunk()

# Photos import
photos = find_files(photos_folder, [".jpg", ".jpeg", ".tif", ".tiff"])
chunk.addPhotos(photos)
doc.save()

# match photos
chunk.matchPhotos(keypoint_limit = 40000, tiepoint_limit = 10000, generic_preselection = True, reference_preselection = True)
doc.save()

# Align cameras
chunk.alignCameras()
doc.save()

# Build Depth Maps
chunk.buildDepthMaps(downscale = 2, filter_mode = Metashape.MildFiltering)
doc.save()

# Build Model
chunk.buildModel()
doc.save()

# Build DEM
chunk.buildDem(source_data=Metashape.DepthMapsData)
doc.save()

# Build Orthomosaic
chunk.buildOrthomosaic(surface_data=Metashape.ElevationData)
doc.save()

# Export Orthomosaique (missing reprojection to EPSG 2154)
chunk.exportRaster(ortho_folder, source_data = Metashape.OrthomosaicData)
doc.save()

# Quit Metashape
Metashape.app.quit()
#exit()

Now I just need to find the syntax to convert the orthomosaic from WGS84 (EPSG 4326) to Lambert 93 (EPSG 2154).
But I think it's best to open a new topic on this precise point.

To be continued here : https://www.agisoft.com/forum/index.php?topic=16598.0
« Last Edit: July 29, 2024, 05:51:05 PM by Sylvain M. »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15465
    • View Profile
Hello Sylvain,

I have posted the following code for export in the mentioned thread:
Code: [Select]
proj = Metashape.OrthoProjection()
proj.crs = Metashape.CoordinateSystem("EPSG::2154")
chunk.exportRaster(path, source_data = Metashape.OrthomosaicData, projection = proj)

You may need to add some additional parameters, if necessary, like export resolution, for example, but those should be easy to implement.
Best regards,
Alexey Pasumansky,
Agisoft LLC