Forum

Author Topic: Creating and exporting Agisoft outputs in Python  (Read 9145 times)

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Creating and exporting Agisoft outputs in Python
« on: December 18, 2017, 04:33:01 PM »
I'm new to agisoft and have a beginners level python skill set, what I am trying to do is:

Align Photos
Set Coordinate system
Build Dense Cloud (and export)
Build Mesh
Build DEM (and export)
Build Ortho (and export)

So I am running this section by section in the workflow>batch process> RunScript and I don't get any errors, but nothing seems to be actually working. None of my photos are aligned (all say NA), and transforming my coordinate system didn't change anything either. All the steps work when I do them in the GUI, but that is time consuming.

So here is my sad code, any help would be appreciated:
Code: [Select]
chunk = PhotoScan.app.document.chunk
doc = PhotoScan.app.document
doc.chunk = doc.chunks[0]

#Set variables
project_path = "insert path here"
project_name = "Name here"

#Align photos
chunk.matchPhotos(accuracy = Photoscan.HighAccuracy, generic_preselection=True, refernce_preselection=True,filter_mask = False, keypoint_limit = 350000, tiepoint_limit = 0) 
chunk.alignCameras()

#save
doc.save(project_path)


#Set Coordinate System
chunk.crs = PhotoScan.CoordinateSystem("EPSG::32611")
chunk.updateTransform()

#save
doc.save(project_path)


#build dense cloud
chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = Photoscan.AggressiveFiltering)
chunk.exportPoints(project_path + project_name + ".las", binary=True, precision=6, normals=True, colors=True, format=‘las’)

#save
doc.save(project_path)

#build mesh
chunk.buildModel(surface = Photoscan.HeightField, interpolation = Photoscan.EnabledInterpolation, face_count=Photoscan.MediumFaceCount )

#save
doc.save(project_path)

#Build DEM
chunk.buildDem(source=Photoscan.DensePoints, interpolation=Photoscan.EnabledInterpolation)
chunk.exportDem(project_path + project_name + "_DEM.tif", format=’tif ’,chunk.region, nodata=-32767, write_kml=False, write_world=True)
 

#save
doc.save(project_path)


#Build Ortho
chunk.buildOrthomosaic(surface=Photoscan.OrthoSurfaceDem, blending=Photoscan.MosaicBlending, color_correction=False)
chunk.exportOrthomosaic(project_path + project_name + ".tif", format=’tif ’, raster_transform=Photoscan.RasterTransformNone, chunk.region, write_kml=False, write_world=True)


#save
doc.save(project_path)


Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #1 on: December 18, 2017, 08:46:05 PM »
Hello ETomps,

Please try the following code.
I've corrected some arguments types for the version 1.3, also you can see that the project is saved in the very beginning with the path argument (analogue of Save As operation) and later it is just re-saved.

If you have any errors in the Console pane related to the script, please let me know.

Code: [Select]
import PhotoScan

doc = PhotoScan.app.document

#Set variables
project_path = "insert path here"
project_name = "Name here"
#save
doc.save(project_path)

if len(doc.chunks):
chunk = PhotoScan.app.document.chunk
else:
chunk = doc.addChunk()




#Align photos
chunk.matchPhotos(accuracy = PhotoScan.HighAccuracy, generic_preselection=True, refernce_preselection=True,filter_mask = False, keypoint_limit = 100000, tiepoint_limit = 20000) 
chunk.alignCameras()

#save
doc.save()

#Set Coordinate System
chunk.crs = PhotoScan.CoordinateSystem("EPSG::32611")
chunk.updateTransform()

#save
doc.save()

#build dense cloud
chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = PhotoScan.AggressiveFiltering)

#save
doc.save()

chunk.exportPoints(project_path + project_name + ".las", binary=True, precision=6, colors=True, format=PhotoScan.PointsFormatLAS)

#build mesh
chunk.buildModel(surface = PhotoScan.HeightField, interpolation = PhotoScan.EnabledInterpolation, face_count=PhotoScan.MediumFaceCount )

#save
doc.save()

#Build DEM
chunk.buildDem(source=PhotoScan.DensePoints, interpolation=PhotoScan.EnabledInterpolation)

#save
doc.save()

chunk.exportDem(project_path + project_name + "_DEM.tif", image_format=PhotoScan.ImageFormatTIFF, raster_format = PhotoScan.RasterFormatTiles, nodata=-32767, write_kml=False, write_world=True)

#Build Ortho
chunk.buildOrthomosaic(surface=PhotoScan.ElevationData, blending=PhotoScan.MosaicBlending, color_correction=False)

#save
doc.save()

chunk.exportOrthomosaic(project_path + project_name + ".tif", image_format=PhotoScan.ImageFormatTIFF, raster_format = PhotoScan.RasterFormatTiles, raster_transform=PhotoScan.RasterTransformNone, write_kml=False, write_world=True)

print("Finished")
« Last Edit: December 19, 2017, 12:20:07 AM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #2 on: December 18, 2017, 11:21:05 PM »
So it doesn't seem to be working, it zooms through the steps and then I get an error:

PS D:\Agisoft_Codes> python .\Agisoft_wholecode.py
  File ".\Agisoft_wholecode.py", line 57
    chunk.exportDem(project_path + project_name + "_DEM.tif", image_format=PhotoScan.ImageFormatTIFF, raster_format = PhotoScan.RasterFormatTilesnodata=-32767, write_kml=False, write_world=True)


So when I opened up the project all the photos (0/832) are not aligned, and the Coordinate system says: "local coordinates (m)" instead of EPSG::32611


Any advice? It aligns properly if I do it manually, but I'm using all the same settings.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #3 on: December 18, 2017, 11:35:06 PM »
I've missed a "comma" in the line 57, corrected now. Can you check it now?

Also is there anything else in the Console pane after you run the script?
Best regards,
Alexey Pasumansky,
Agisoft LLC

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #4 on: December 18, 2017, 11:47:45 PM »
Thanks for getting back to me so quick,


Running the script in toold>runscript> I get the error "Can't run script" after fixing the comma

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #5 on: December 19, 2017, 12:21:18 AM »
Hello ETomps,

I've made additional correction: variables definition is now before their first utilization (as it should be). If the script still doesn't run, can you post the related Console pane output?
Best regards,
Alexey Pasumansky,
Agisoft LLC

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #6 on: December 19, 2017, 04:02:45 PM »
Still no luck, here is what happens when I run the script


And this is the script I am using:
Code: [Select]
import PhotoScan

doc = PhotoScan.app.document

#Set variables
project_path = "D:\\Processed\\20171118LFF1a7RATX82_section"
project_name = "20171118LFF1a7RATX82_section"

#save
doc.save(project_path)

if len(doc.chunks):
chunk = PhotoScan.app.document.chunk
else:
chunk = doc.addChunk()


#Align photos
chunk.matchPhotos(accuracy = PhotoScan.HighAccuracy, generic_preselection=True, refernce_preselection=True,filter_mask = False, keypoint_limit = 100000, tiepoint_limit = 20000) 
chunk.alignCameras()

#save
doc.save()

#Optimize Cameras
chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=True, fit_b2=True, fit_k1=True,fit_k2=True, fit_k3=True, fit_k4=False, fit_p1=True, fit_p2=True, fit_p3=False,fit_p4=False)

#save
doc.save()

#Set Coordinate System
chunk.crs = PhotoScan.CoordinateSystem("EPSG::32611")
chunk.updateTransform()

#save
doc.save()

#build dense cloud
chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = PhotoScan.AggressiveFiltering)

#save
doc.save()

chunk.exportPoints(project_path + project_name + ".las", binary=True, precision=6, colors=True, format=PhotoScan.PointsFormatLAS)

#build mesh
chunk.buildModel(surface = PhotoScan.HeightField, interpolation = PhotoScan.EnabledInterpolation, face_count=PhotoScan.MediumFaceCount )

#save
doc.save()

#Build DEM
chunk.buildDem(source=PhotoScan.DensePoints, interpolation=PhotoScan.EnabledInterpolation)

#save
doc.save()

chunk.exportDem(project_path + project_name + "_DEM.tif", image_format=PhotoScan.ImageFormatTIFF, raster_format = PhotoScan.RasterFormatTiles, nodata=-32767, write_kml=False, write_world=True)

#Build Ortho
chunk.buildOrthomosaic(surface=PhotoScan.ElevationData, blending=PhotoScan.MosaicBlending, color_correction=False)

#save
doc.save()

chunk.exportOrthomosaic(project_path + project_name + ".tif", image_format=PhotoScan.ImageFormatTIFF, raster_format = PhotoScan.RasterFormatTiles, raster_transform=PhotoScan.RasterTransformNone, write_kml=False, write_world=True)

print("Finished")

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #7 on: December 19, 2017, 04:37:28 PM »
Hello ETomps,

The path passed to the doc.save operation should contain the PSX extension.

Also corrected some minor issues with the parameter names:
Code: [Select]
import PhotoScan

doc = PhotoScan.app.document

#Set variables
project_path = "D:\\Processed\\"
project_name = "20171118LFF1a7RATX82_section"

#save
doc.save(project_path + project_name + ".psx")

if len(doc.chunks):
chunk = PhotoScan.app.document.chunk
else:
chunk = doc.addChunk()


#Align photos
chunk.matchPhotos(accuracy = PhotoScan.HighAccuracy, generic_preselection=True, reference_preselection=True,filter_mask = False, keypoint_limit = 100000, tiepoint_limit = 20000) 
chunk.alignCameras()

#save
doc.save()

#Optimize Cameras
chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=True, fit_b2=True, fit_k1=True,fit_k2=True, fit_k3=True, fit_k4=False, fit_p1=True, fit_p2=True, fit_p3=False,fit_p4=False)

#save
doc.save()

#Set Coordinate System
chunk.crs = PhotoScan.CoordinateSystem("EPSG::4326")
chunk.updateTransform()

#save
doc.save()

#build dense cloud
chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = PhotoScan.AggressiveFiltering)

#save
doc.save()

chunk.exportPoints(project_path + project_name + ".las", binary=True, precision=6, colors=True, format=PhotoScan.PointsFormatLAS)

#build mesh
chunk.buildModel(surface = PhotoScan.HeightField, interpolation = PhotoScan.EnabledInterpolation, face_count=PhotoScan.MediumFaceCount )

#save
doc.save()

#Build DEM
chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)

#save
doc.save()

chunk.exportDem(project_path + project_name + "_DEM.tif", image_format=PhotoScan.ImageFormatTIFF, format = PhotoScan.RasterFormatTiles, nodata=-32767, write_kml=False, write_world=True)

#Build Ortho
chunk.buildOrthomosaic(surface=PhotoScan.ElevationData, blending=PhotoScan.MosaicBlending, color_correction=False)

#save
doc.save()

chunk.exportOrthomosaic(project_path + project_name + ".tif", image_format=PhotoScan.ImageFormatTIFF, format = PhotoScan.RasterFormatTiles, raster_transform=PhotoScan.RasterTransformNone, write_kml=False, write_world=True)

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

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #8 on: December 19, 2017, 04:56:59 PM »
It worked! Thanks so much :)

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #9 on: December 19, 2017, 07:56:27 PM »
Appears that I have hit a snag and it failed after I reached the Build Dense Cloud option, The code I used was:

Code: [Select]
import PhotoScan

doc = PhotoScan.app.document

#Set variables
project_path = "D:\\Processed\\20171204LFF1PH3PH32_section\\"
project_name = "20171204LFF1PH3PH32_section"

#save
doc.save(project_path + project_name + ".psx")

if len(doc.chunks):
chunk = PhotoScan.app.document.chunk
else:
chunk = doc.addChunk()


#Align photos
chunk.matchPhotos(accuracy = PhotoScan.HighAccuracy, generic_preselection=True, reference_preselection=True,filter_mask = False, keypoint_limit = 350000, tiepoint_limit = 0) 
chunk.alignCameras()

#save
doc.save()

#Optimize Cameras
chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=True, fit_b2=True, fit_k1=True,fit_k2=True, fit_k3=True, fit_k4=False, fit_p1=True, fit_p2=True, fit_p3=False,fit_p4=False)

#save
doc.save()

#Set Coordinate System
chunk.crs = PhotoScan.CoordinateSystem("EPSG::4326")
chunk.updateTransform()

#save
doc.save()

#build dense cloud
chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = PhotoScan.AggressiveFiltering)

#save
doc.save()

chunk.exportPoints(project_path + project_name + ".las", binary=True, precision=6, colors=True, format=PhotoScan.PointsFormatLAS)

#build mesh
chunk.buildModel(surface = PhotoScan.HeightField, interpolation = PhotoScan.EnabledInterpolation, face_count=PhotoScan.MediumFaceCount )

#save
doc.save()

#Build DEM
chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)

#save
doc.save()

chunk.exportDem(project_path + project_name + "_DEM.tif", image_format=PhotoScan.ImageFormatTIFF, format = PhotoScan.RasterFormatTiles, nodata=-32767, write_kml=False, write_world=True)

#Build Ortho
chunk.buildOrthomosaic(surface=PhotoScan.ElevationData, blending=PhotoScan.MosaicBlending, color_correction=False)

#save
doc.save()

chunk.exportOrthomosaic(project_path + project_name + ".tif", image_format=PhotoScan.ImageFormatTIFF, format = PhotoScan.RasterFormatTiles, raster_transform=PhotoScan.RasterTransformNone, write_kml=False, write_world=True)

print("Finished")


Thanks for helping so much, I really appreciate it

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #10 on: December 19, 2017, 09:44:03 PM »
Hello ETomps,

What was the error in the Console pane?

Maybe you should try lower quality option, like PhotoScan.MediumQuality first?
Best regards,
Alexey Pasumansky,
Agisoft LLC

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #11 on: December 19, 2017, 09:50:12 PM »
All it said was "Can't run script",  but I am running it on another mosaic and it hasn't given me an error so far, so fingers crossed. Will let you know if happens again and I will try that change.

Thanks!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #12 on: December 19, 2017, 09:52:44 PM »
Hello ETomps,

If you open Console pane from the View Menu it should contain more detailed response related to the script errors.
Best regards,
Alexey Pasumansky,
Agisoft LLC

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #13 on: December 20, 2017, 03:38:10 PM »
Script worked the second time! So looks like it is all good. Thank you for your help :)

Turns out the error I had earlier was a typo, I had ESPG instead of EPSG
« Last Edit: December 20, 2017, 09:23:16 PM by ETomps »

ETomps

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Creating and exporting Agisoft outputs in Python
« Reply #14 on: December 21, 2017, 03:11:10 PM »
Hi again, so my code was running great and then got the error

"2017-12-21 02:47:46   File "D:/Agisoft_Codes/Agisoft_wholecode_20171211MP1ATXa7R3_section.py", line 31, in <module>
2017-12-21 02:47:46     chunk.buildDenseCloud(quality = PhotoScan.UltraQuality, filter = PhotoScan.AggressiveFiltering)
2017-12-21 02:47:46 MemoryError: bad allocation"


Does this mean that my computer just ran out of memory?