Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: mwillis on January 25, 2014, 04:50:04 PM

Title: Batch processing of numerous PSZ files
Post by: mwillis on January 25, 2014, 04:50:04 PM
Before I start delving into Python, I thought I'd ask if anyone has already created code that does this or similar:

I have 42 psz files.  I would like Photoscan to:

1) Open the first psz in the sequence
2) Align the photos (generic, high)
3) Build Medium dense point cloud
4) Build Mesh from Dense Point cloud (arbitrary, high)
5) Save the psz file
6) Open the next psz file in the sequence and then repeat steps 1 through 6.

This is probably pretty basic stuff but I'd appreciate any sample code or a nudge in the right direction.

Thanks!
Title: Re: Batch processing of numerous PSZ files
Post by: Alexey Pasumansky on January 27, 2014, 08:21:37 PM
Hello mwillis,

Do you have all the .psz files in the single folder?
Title: Re: Batch processing of numerous PSZ files
Post by: mwillis on January 28, 2014, 06:03:48 PM
Hi Alexey, yes, all the .psz files are in a single directory.

Thanks!
Title: Re: Batch processing of numerous PSZ files
Post by: Alexey Pasumansky on January 29, 2014, 02:39:37 PM
I think something like the following should work:

Code: [Select]
import PhotoScan, os

path = PhotoScan.app.getExistingDirectory("Please choose the folder with .psz files:")

print("Script started")
doc = PhotoScan.app.document
doc.clear()
project_list = os.listdir(path)

for project_name in project_list:
if ".PSZ" in project_name.upper():
doc.open(path + "/" + project_name)
chunk = doc.chunks[0]

chunk.matchPhotos(accuracy = "high", preselection = "generic", point_limit = 40000)
chunk.alignPhotos()

chunk.buildDenseCloud(quality = "medium", filter = "aggressive", gpu_mask = 1, cpu_cores_inactive = 0)
chunk.buildModel(surface = "arbitrary", source = "dense", interpolation = "enabled", faces = "high")

doc.save()
print("Processed project: " + project_name)

else:
continue

print("Script finished.")

On script run you'll be asked to point to the folder that contains project files to be processed. I've checked it on the smaller number of projects and haven't implemented any special checks of existing projects (like no images in project or unsuccessful processing operation). So if you need any of such features to be added to script please let me know. And I recommend that the script is working on a few test projects.
Title: Re: Batch processing of numerous PSZ files
Post by: mwillis on January 30, 2014, 03:30:05 AM
Alexey, you are the man!  Thanks so much. Greatly appreciate it.

Mark
Title: Re: Batch processing of numerous PSZ files
Post by: Arie on April 18, 2015, 05:07:12 PM
Hi,
any chance to get an updated version of this script for photoscan 1.1.X?
That would be awesome!
Cheers!

Edit: Well, this was way easier than I thought.

Code: [Select]
import PhotoScan, os

path = PhotoScan.app.getExistingDirectory("Please choose the folder with .psz files:")

print("Script started")
doc = PhotoScan.app.document
doc.clear()
project_list = os.listdir(path)

for project_name in project_list:
if ".PSZ" in project_name.upper():
doc.open(path + "/" + project_name)
chunk = doc.chunks[0]

chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
chunk.alignCameras()

chunk.buildDenseCloud(quality=PhotoScan.HighQuality)
chunk.buildModel(surface=PhotoScan.Arbitrary, interpolation=PhotoScan.EnabledInterpolation)

chunk.buildUV(mapping=PhotoScan.GenericMapping)
chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096)

doc.save()
print("Processed project: " + project_name)

else:
continue

print("Script finished.")
Title: Re: Batch processing of numerous PSZ files
Post by: matt07lx on August 07, 2019, 05:12:48 PM
Sorry for restarting an old post, but is there any way to change this script to include psz files in all subdirectories, too? I have all of my projects organized in this way and want to batch process a number of different psz files in multiple subdirectories. I tried to make this work, but with my limited knowledge of Python I couldn't make it work.

Thanks so much!
Title: Re: Batch processing of numerous PSZ files
Post by: Alexey Pasumansky on August 07, 2019, 05:36:45 PM
Hello matt07lx,

Please check the following script example:
Code: [Select]
import Metashape, os, glob

path = Metashape.app.getExistingDirectory("Please choose the folder with .psz files:")
print("Script started...")
doc = Metashape.app.document
doc.clear()

TYPE = ".PSZ"
project_list = [file for file in glob.iglob(path + "/**/*.*" , recursive = True) if (file.upper().endswith(TYPE) and os.path.isfile(file))]

for project_name in project_list:

doc.open(project_name)
chunk = doc.chunks[0]

chunk.matchPhotos(accuracy = "high", preselection = "generic", point_limit = 40000)
chunk.alignPhotos()

chunk.buildDenseCloud(quality = "medium", filter = "aggressive", gpu_mask = 1, cpu_cores_inactive = 0)
chunk.buildModel(surface = "arbitrary", source = "dense", interpolation = "enabled", faces = "high")

doc.save()
print("Processed project: " + project_name)

print("Script finished.")
Title: Re: Batch processing of numerous PSZ files
Post by: matt07lx on August 07, 2019, 07:45:07 PM
Hi Alexey,

Thank you so much for your very quick response! This works perfectly, and I was able to adapt it for my needs. Can I also ask if you plan to add a Python function to enable select dense cloud points by shape? I would like to batch process a number of projects with a single shape to produce models with the exact same extents.
Title: Re: Batch processing of numerous PSZ files
Post by: Alexey Pasumansky on August 07, 2019, 07:58:09 PM
Hello matt07lx,

We are planning to implement SelectPointsByShape feature, yet not sure if it would be included to 1.5.4 update.