Forum

Author Topic: Python Script for Basic Full Process Chain  (Read 14489 times)

SamC-E

  • Newbie
  • *
  • Posts: 7
    • View Profile
Python Script for Basic Full Process Chain
« on: April 14, 2015, 05:56:42 AM »
Hello,

Firstly I have only been using python for a couple of weeks now so I am not anywhere near what you would call fully python literate but I will probably be able to understand any responses I get and I am running Agisoft on a Ubuntu system.

Basically I have been trying to create a python script and have found only one response (http://www.agisoft.com/forum/index.php?topic=1881.msg10014#msg10014) about a script that can progress through the Agisoft processing but this response was too complex for my needs and my attempts to adapt it so far have failed.
I currently do the process manually but I need to automate it to the greatest extent I can, below I define the basic manual process I carry out for clarity:

- Add Folder
- Align Photos [Accuracy(preferably able to choose different settings), Pair preselection(Disabled), Advanced (Default)]
- Build Dense Cloud [Quality & Depth Filtering(preferably able to choose different settings)]
- Build Mesh [Surface Type(Arbitrary), Source Data(Dense Cloud), Face Count(preferably able to choose different settings),  Advanced(Default)]
- Build Texture [All default settings]
- Save Project as desired
- Export Model as Wavefront OBJ

I'm sure/hope someone out there has a link to a post I have missed, a script or the skeleton of a script that has this functionality, or the ability to quite quickly guide me onto the right path.
Any help anyone could offer would be greatly appreciated as the literature and forums have left me a bit stumped.

Regards
Sam

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #1 on: April 21, 2015, 02:47:52 PM »
Hello Sam,

It should be something like the following:

Code: [Select]
import os
import PhotoScan


def main():

global doc
doc = PhotoScan.app.document

app = QtGui.QApplication.instance()
parent = app.activeWindow()

#prompting for path to photos
path_photos = PhotoScan.app.getExistingDirectory("Specify input photo folder:")
path_export = PhotoScan.app.getExistingDirectory("Specify EXPORT folder:")

#processing parameters
accuracy = PhotoScan.Accuracy.HighAccuracy  #align photos accuracy
preselection = PhotoScan.Preselection.GenericPreselection
keypoints = 40000 #align photos key point limit
tiepoints = 10000 #align photos tie point limit
source = PhotoScan.PointsSource.DensePoints #build mesh source
surface = PhotoScan.SurfaceType.Arbitrary #build mesh surface type
quality = PhotoScan.Quality.MediumQuality #build dense cloud quality
filtering = PhotoScan.FilterMode.AggressiveFiltering #depth filtering
interpolation = PhotoScan.Interpolation.EnabledInterpolation #build mesh interpolation
face_num = PhotoScan.FaceCount.HighFaceCount #build mesh polygon count
mapping = PhotoScan.MappingMode.GenericMapping #build texture mapping
atlas_size = 8192
blending = PhotoScan.BlendingMode.MosaicBlending #blending mode
color_corr = False


print("Script started")

#creating new chunk
doc.addChunk()
chunk = doc.chunks[-1]
chunk.label = "New Chunk"

#loading images
image_list = os.listdir(path_photos)
photo_list = list()
for photo in image_list:
if ("jpg" or "jpeg" or "JPG" or "JPEG") in photo.lower():
photo_list.append(path_photos + "\\" + photo)
chunk.addPhotos(photo_list)

#align photos
chunk.matchPhotos(accuracy = accuracy, preselection = preselection, filter_mask = False, keypoint_limit = keypoints, tiepoint_limit = tiepoints)
chunk.alignCameras()

chunk.optimizeCameras()

#building dense cloud
PhotoScan.app.gpu_mask = 1  #GPU devices binary mask
PhotoScan.app.cpu_cores_inactive = 2  #CPU cores inactive
chunk.buildDenseCloud(quality = quality, filter = filtering)

#building mesh
chunk.buildModel(surface = surface, source = source, interpolation = interpolation, face_count = face_num)

#build texture
chunk.buildUV(mapping = mapping, count = 1)
chunk.buildTexture(blending = blending , color_correction = color_corr, size = atlas_size)

PhotoScan.app.update()

#export

chunk.exportModel(path_export + "\\model.obj", format = "obj", texture_format='jpg')

print("Script finished")


PhotoScan.app.addMenuItem("Custom menu/Process 1", main)


In case you wish to choose different processing settings on script run, you need to create a custom dialog using PySide module.
Best regards,
Alexey Pasumansky,
Agisoft LLC

Arie

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #2 on: August 03, 2015, 10:49:30 AM »
Hi everybody,
what would one have to add to the script if one wants to process multiple objects consecutively?

For example, I have about 100 objects, all have the same folder structure, just the name changes i.e. D:\Projects\Artefacts\I5648\Images (only the I5648 changes to another number).

What needs to be adjusted so that photoscan loads the images, creates a psz, does the processing and when finished switches to the next folder?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #3 on: August 03, 2015, 11:45:39 AM »
Hello Arie,

I think that one of the easiest ways is to create the list to the project folders and perform the loop on the elements from them, where each step is the full processing chain (or just create a custom function that takes the project folder path as input).
Best regards,
Alexey Pasumansky,
Agisoft LLC

Arie

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #4 on: August 03, 2015, 12:23:51 PM »
Hi Alexey,
thanks for your quick reply!
How would that look like in python? Unfortunately I'm not very firm in python.
Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #5 on: August 03, 2015, 12:48:25 PM »
Hello Arie,

I was thinking about something like the following:


Code: [Select]
import os


def process(input_path):

   """
   this function takes 'input_path' argument where input/output data and folders are and would be organized.
   it should be the same for every folder, as the function will be applied to all of them.
   """
   ...
   ...

...
main_path = "......\projects\"
paths = list()

for path in list(os.listdir(main_path)): #creating a list of project paths from the main folder
    if os.path.idir(main_path + path):
        paths.append(main_path + path)

...

for path in paths:

    process(path)


print("Finished")
Best regards,
Alexey Pasumansky,
Agisoft LLC

Arie

  • Full Member
  • ***
  • Posts: 134
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #6 on: August 05, 2015, 11:13:51 AM »
Thanks Alexey!

Is there a typo in: " if os.path.idir(main_path + path):" ?
Should idir = isdir?

And how to add the processing steps to the script?
Thank you so much for reply so far!
Cheers.

pap014

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #7 on: August 11, 2015, 02:26:06 PM »
Hello,
i'm new here. when I try to run the script given in the second post it gives me this error :

NameError: global name 'QtGui' is not defined

How can I solve this ?

Thank you 

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14846
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #8 on: August 11, 2015, 05:13:24 PM »
Hello pap014,

You need to put the following line at the beginning of the script:

Code: [Select]
from PySide import QtGui
Best regards,
Alexey Pasumansky,
Agisoft LLC

pap014

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #9 on: August 11, 2015, 05:37:45 PM »
Thank you very much, it looks like it works, the process has started

pap014

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Python Script for Basic Full Process Chain
« Reply #10 on: August 17, 2015, 06:18:41 PM »
Hello,
Is it possible to export an orthophoto instead of a model with the same kind of script ?
thank you very much !