Forum

Author Topic: build dem and export model/dem error  (Read 11694 times)

iceman11

  • Newbie
  • *
  • Posts: 29
    • View Profile
build dem and export model/dem error
« on: August 24, 2017, 01:06:00 AM »
Hi,
I have put together a script to automate 3D model processing. How do I make sure that my coordinate system is running in wgs84? Also I want the script to ignore adaptive camera fitting setting used in the alignment process. Here the script complaints about saving project in .psx format before processing the dem. Can you please review my code and suggest any changes. Can you also suggest a way to speed up the 3D modeling process, how should I subdivide my data into smaller chunks and do the 3d model build?
thanks,
Tarun 

Code: [Select]

import PhotoScan
import os
import argparse

def parse_args():
    parser = argparse.ArgumentParser(description="Input/Output folder to save 3D model")
    parser.add_argument('--inputpath', '-p', action="store", help="path to photos", required=True)
    parser.add_argument('--outputfile', '-o', action="store", help="output file name", required=True)
    return parser.parse_args()

def main():
    print("Script is starting")
    args = parse_args()
    doc = PhotoScan.app.document #application class
    chunk = doc.addChunk()

    # folder location
    #path_photos = '\Users\tarunluthra\Downloads\PS_test\'
    #path_photos += "\\"
    #print(path_photos)

    # Specify folder
#    path_photos = PhotoScan.app.getExistingDirectory("Specify folder with input photos:")
    path_photos = args.inputpath
    project_path = args.outputfile
    doc.save(project_path)
    PhotoScan.app.update()


    # Specify project file name
    #project_path = PhotoScan.app.getSaveFileName("Specify project filename for saving:")

    if not project_path.lower().endswith(".psx"):
        project_path += ".psx"

    #
    image_list = [
                  os.path.join(path_photos, path)
                  for path in os.listdir(path_photos)
                  if path.lower().endswith(("jpg", "jpeg", "tif", "png"))
                  ]
    chunk.addPhotos(image_list)
    PhotoScan.app.update()

    ## image quality
 #   app = PhotoScan.Application()
#doc = PhotoScan.app.document
#chu=doc.chunk
    chunk.estimateImageQuality()
    for image in chunk.cameras:
        if float(image.photo.meta['Image/Quality'])<0.5:
            image.enabled = False
            print ('DISABLE %s' %(image))

    # Update
#PhotoScan.app.update()
#   chunk.loadReference(path, "xml")   # add gps reference position
#   chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.NoPreselection, filter_mask=False,keypoint_limit=40000, tiepoint_limit=0)
#   chunk.alignCameras()


    #Align photos
    # chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
    # chunk.alignCameras()
    # chunk.buildDenseCloud(quality=PhotoScan.MediumQuality)

    #Align photos
    chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
    chunk.alignCameras()
    chunk.buildDenseCloud(quality=PhotoScan.MediumQuality)
    chunk.buildModel(surface=PhotoScan.Arbitrary, interpolation=PhotoScan.EnabledInterpolation)
    chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)
    chunk.buildUV(mapping=PhotoScan.GenericMapping)
    chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096)
    chunk.exportDem(project_path[:-4]+'t_rgb.tif', image_format=ps.ImageFormatTIFF, nodata = -99999, write_kml=False, write_world=False)
#format=ps.RasterFormatXYZ, #projection=ps.EPSG:4326,
  #][, region][, dx][, dy]
  # [, blockw][, blockh][,
    chunk.PointCloud.export(project_path[:-4]+'ptcloud.obj', format=obj)
    PhotoScan.app.update()

    try:
        doc.save()
        PhotoScan.app.update()
    except RuntimeError:
        PhotoScan.app.messageBox("Can't save project")
      # if not doc.save(project_path):
      #   # PhotoScan.app.messageBox("Can't save project")
      #   raise Exception("Cannot save project")


if __name__ == "__main__":
    main()

Here's the error
BuildDem: source data = Dense cloud, interpolation = Enabled, resolution = 0
Please save project in PSX format before processing
Traceback (most recent call last):
  File "PS_test.py", line 95, in <module>
    main()
  File "PS_test.py", line 74, in main
    chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)
RuntimeError: Empty frame path

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: build dem and export model/dem error
« Reply #1 on: August 24, 2017, 10:32:26 AM »
Hello Tarun,

Project should be really saved in PSX format to enable DEM, Orthomosaic and Tiled model generation.

I can suggest to use chunk = doc.addChunk() assignment after you have saved your project for the first time (doc.save(project_path)), then in the code use just doc.save() lines to re-save the project, I recommend to use it right before chunk.buildDem() line.

To export points you need to use chunk.exportPoints() function.

To disable adaptive camera model fitting, use chunk.alignCameras(adaptive_fitting = False).

If you are loading the coordinate information in WGS84 system then to set up the coordinate system for chunk use:
Code: [Select]
wgs84 = PhotoScan.CoordinateSystem("EPSG::4326")
chunk.crs = wgs84


The sample script for splitting the aligned chunk into blocks for the dense cloud or mesh processing in smaller volumes is available on our Wiki pages:
http://wiki.agisoft.com/wiki/Split_in_chunks.py
Best regards,
Alexey Pasumansky,
Agisoft LLC

iceman11

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: build dem and export model/dem error
« Reply #2 on: August 25, 2017, 06:23:17 PM »
Alexey,
I made the changes you suggested...I have got this new error in the script. Can you please help?

saved project in 0.448727 sec
BuildDem: source data = Dense cloud, interpolation = Enabled, resolution = 0
Please save project in PSX format before processing
Traceback (most recent call last):
  File "PS_test.py", line 106, in <module>
    main()
  File "PS_test.py", line 84, in main
    chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)
RuntimeError: Empty frame path
Code: [Select]
#PhotoScan.app.enumGPUDevices()

import PhotoScan
import os
import argparse

def parse_args():
    parser = argparse.ArgumentParser(description="Input/Output folder to save 3D model")
    parser.add_argument('--inputpath', '-p', action="store", help="path to photos", required=True)
    parser.add_argument('--outputfile', '-o', action="store", help="output file name", required=True)
    return parser.parse_args()

def main():
    print("Script is starting")
    args = parse_args()
    doc = PhotoScan.app.document #application class
    chunk = doc.addChunk()

    # folder location
    #path_photos = '\Users\tarunluthra\Downloads\PS_test\'
    #path_photos += "\\"
    #print(path_photos)

    # Specify folder
#    path_photos = PhotoScan.app.getExistingDirectory("Specify folder with input photos:")
    path_photos = args.inputpath
    project_path = args.outputfile
    doc.save(project_path)
    chunk = doc.addChunk()
    PhotoScan.app.update()


    # Specify project file name
    #project_path = PhotoScan.app.getSaveFileName("Specify project filename for saving:")

    if not project_path.lower().endswith(".PSX"):
        project_path += ".PSX"
       
        #doc.save(project_path)
        chunk = doc.addChunk()
       
       

    #
    image_list = [
                  os.path.join(path_photos, path)
                  for path in os.listdir(path_photos)
                  if path.lower().endswith(("jpg", "jpeg", "tif", "png"))
                  ]
    chunk.addPhotos(image_list)
    PhotoScan.app.update()
   
    wgs84 = PhotoScan.CoordinateSystem("EPSG::4326")
    chunk.crs = wgs84

    ## image quality
 #   app = PhotoScan.Application()
#doc = PhotoScan.app.document
#chu=doc.chunk
    chunk.estimateImageQuality()
    for image in chunk.cameras:
        if float(image.photo.meta['Image/Quality'])<0.5:
            image.enabled = False
            print ('DISABLE %s' %(image))

    # Update
#PhotoScan.app.update()
#   chunk.loadReference(path, "xml")   # add gps reference position
#   chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.NoPreselection, filter_mask=False,keypoint_limit=40000, tiepoint_limit=0)
#   chunk.alignCameras()


    #Align photos
    # chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
    # chunk.alignCameras()
    # chunk.buildDenseCloud(quality=PhotoScan.MediumQuality)

    #Align photos
    chunk.matchPhotos(accuracy=PhotoScan.HighAccuracy, preselection=PhotoScan.GenericPreselection)
    chunk.alignCameras(adaptive_fitting = False)
    chunk.buildDenseCloud(quality=PhotoScan.MediumQuality)
    chunk.buildModel(surface=PhotoScan.Arbitrary, interpolation=PhotoScan.EnabledInterpolation)
    doc.save()
    chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)
    chunk.buildUV(mapping=PhotoScan.GenericMapping)
    chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096)
    chunk.exportDem(project_path[:-4]+'t_rgb.tif', image_format=PhotoScan.ImageFormatTIFF, nodata = -99999, write_kml=False, write_world=False)
    chunk.exportPoints()
#format=ps.RasterFormatXYZ, #projection=ps.EPSG:4326,
  #][, region][, dx][, dy]
  # [, blockw][, blockh][,
    chunk.PointCloud.export(project_path[:-4]+'ptcloud.obj', format=obj)
    PhotoScan.app.update()

    try:
        doc.save()
        PhotoScan.app.update()
    except RuntimeError:
        PhotoScan.app.messageBox("Can't save project")
      # if not doc.save(project_path):
      #   # PhotoScan.app.messageBox("Can't save project")
      #   raise Exception("Cannot save project")


if __name__ == "__main__":
    main()

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: build dem and export model/dem error
« Reply #3 on: August 25, 2017, 07:55:59 PM »
Hello Tarun,

Can you specify, if the project is saved and if so, then under which file name?

I suggest to move lines 36-37
Code: [Select]
    if not project_path.lower().endswith(".PSX"):
        project_path += ".PSX"
and put them before line 28.
Best regards,
Alexey Pasumansky,
Agisoft LLC

iceman11

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: build dem and export model/dem error
« Reply #4 on: August 25, 2017, 08:25:57 PM »
I moved those lines like you suggested now I am getting this error.
Here's how I specify the project name
Code: [Select]
/Applications/PhotoScanPro.app/Contents/MacOS/PhotoScanPro -r PS_test.py -p ~/Downloads/PS_test/ -o bb
 # -p folder location
# -o switch specifies the file name, which should get appended later as bb.psx in the code.
ExportRaster
generating 1179 x 784 raster in 1 x 1 tiles
Traceback (most recent call last):
  File "PS_test.py", line 110, in <module>
    main()
  File "PS_test.py", line 96, in main
    chunk.PointCloud.export(project_path[:-4]+'ptcloud.obj', format=obj)
AttributeError: 'PhotoScan.Chunk' object has no attribute 'PointCloud'
Code: [Select]
    # Specify folder
#    path_photos = PhotoScan.app.getExistingDirectory("Specify folder with input photos:")
    path_photos = args.inputpath
    project_path = args.outputfile
    if not project_path.lower().endswith(".PSX"):
            project_path += ".PSX"
    doc.save(project_path)
   
    #doc.save(project_path)
    chunk = doc.addChunk()
    PhotoScan.app.update()

Code: [Select]
chunk.buildDem(source=PhotoScan.DenseCloudData, interpolation=PhotoScan.EnabledInterpolation)
    chunk.buildUV(mapping=PhotoScan.GenericMapping)
    chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096)
    chunk.exportDem(project_path[:-4]+'t_rgb.tif', image_format=PhotoScan.ImageFormatTIFF, nodata = -99999, write_kml=False, write_world=False)
   #
« Last Edit: August 25, 2017, 08:28:07 PM by iceman11 »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: build dem and export model/dem error
« Reply #5 on: August 25, 2017, 08:44:59 PM »
Hello Tarun,

 chunk.PointCloud.export(project_path[:-4]+'ptcloud.obj', format=obj) - is not a valid expression.

you need to use
Code: [Select]
chunk.exportPoint(project_path[:-4]+'ptcloud.obj', format=PhotoScan.PointsFormatOBJ)
Best regards,
Alexey Pasumansky,
Agisoft LLC

iceman11

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: build dem and export model/dem error
« Reply #6 on: August 25, 2017, 09:05:14 PM »
Hi Alexey,
this is the error now!
File "PS_test.py", line 111, in <module>
    main()
  File "PS_test.py", line 92, in main
    chunk.exportPoint(project_path[:-4]+'ptcloud.obj', format=PhotoScan.PointsFormatOBJ)
AttributeError: 'PhotoScan.Chunk' object has no attribute 'exportPoint'

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15354
    • View Profile
Re: build dem and export model/dem error
« Reply #7 on: August 25, 2017, 09:06:31 PM »
My bad, should be chunk.exportPoints()
Best regards,
Alexey Pasumansky,
Agisoft LLC

iceman11

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: build dem and export model/dem error
« Reply #8 on: August 25, 2017, 09:21:34 PM »
worked! ;D
spasibo Alexey :D