Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: akasch on June 27, 2016, 09:09:12 PM

Title: Batch Processing Workflow: Reference and Camera Calibration File Loading
Post by: akasch on June 27, 2016, 09:09:12 PM
Hi all,

I am trying to run a workflow that has been discussed in previous threads but I am having trouble with one aspect. I have The camera calibration file saved as an xml. My workflow is:

1.) Create a new chunk based on the name of the image file.
2.) Label the chunk based on the name of the image file.
3.) Add photos to the chunk based on the name of the image file.
4.) Load a reference file to enter roll, pitch yaw data.
5.) LOAD A CAMERA CALIBRATION FILE TO EACH CHUNK.
6.) Align photos in each chunk.
7.) Build Mesh (based on sparse point cloud)
8.) Build Ortho
9.) Export Ortho and name based on name of image file.

My problem is:
1.) The camera calibration file does not seem to be able to be read. When I load the images into the chunk, a sensor is automatically created, but I do not know how to access it with the code.

Thanks!!

Here is my script (apart from the line about camera calibration file loading, it works great):

Code: [Select]
import os, PhotoScan, fnmatch

#file path where raw images exist: User Parameter
rawImages = PhotoScan.app.getExistingDirectory("Choose Location of Raw Unmosaicked Tifs")

#Create Output Folder, set output workspace, save .psx photoscan project, set output coordinate system
NewFolder = rawImages + "\\OutputOrthos"
if not os.path.isdir(NewFolder):
    os.makedirs(NewFolder)
NewPhotoScanProject = rawImages + r"\PhotoScanProject.psx"
projectedcrs = PhotoScan.app.getCoordinateSystem("Select Output CoordinateSystem") #User Parameter

#Define active document and save it
doc = PhotoScan.app.document
doc.save(NewPhotoScanProject)

outputcsv = "\IMUData.csv"


#Put files in rawImages folder in a list
ListTifs = os.listdir(rawImages)

#Search through files in ListTifs by ID and put them into sets of tifs based on the ID
allFields = [rasterName[0:8] for rasterName in ListTifs if rasterName[-3:] == "tif"]
uniqueFields = set(allFields)

#For each set, create chunk, define its coordinate system, align photos, build mesh, build orthomosaic, and export orthomosaic
for ID in uniqueFields:
    field = doc.addChunk()
    field.label = ID
    matchingIDs = fnmatch.filter(ListTifs, ID + "*.tif")
    doc.chunk.crs = PhotoScan.CoordinateSystem("EPSG::4326")
    for match in matchingIDs:
        field.addPhotos([rawImages + "\\" + match])
    doc.chunk.loadReference(outputcsv,'csv',' nxyzabc',delimiter=',',group_delimiters=False,skip_rows=1)
    field.matchPhotos(accuracy=PhotoScan.HighAccuracy,preselection=PhotoScan.ReferencePreselection)
    doc.save()
    field.alignCameras()
    doc.save()
    PhotoScan.Calibration(rawImages + '\\CameralCal.xml',format='xml')
    doc.save()
    field.buildModel(surface=PhotoScan.HeightField)
    doc.save()
    field.buildOrthomosaic(surface=PhotoScan.DataSource.ModelData,blending=PhotoScan.BlendingMode.MosaicBlending,color_correction=False,projection=projectedcrs)
    doc.save()
    field.exportOrthomosaic(NewFolder + "\\" + ID + ".tif","tif",raster_transform=PhotoScan.RasterTransformNone,projection=projectedcrs)
    doc.save()
Title: Re: Batch Processing Workflow: Reference and Camera Calibration File Loading
Post by: jpvega on June 29, 2016, 03:57:48 PM
Hi akasch,

Hope this helps. Make sure you use absolute paths for references and calibration.

Code: [Select]
import PhotoScan

def set_references(chunk, references):
    x, y, z, y, p, r = references['accuracy']  # tuple([1, 1, 3, 5, 5, 5])
    chunk.accuracy_cameras = (x, y, z)
    chunk.accuracy_cameras_ypr = (y, p, r)

    wkt = references['wkt']                    # str('EPSG::4326')
    crs = PhotoScan.CoordinateSystem(wkt)
    chunk.crs = crs

    path = references['path']                  # str('references.csv')                 
    format = references['format']              # str('csv')
    columns = references['columns']            # str('nxyzabc')
    delimiter = reference['delimiter']         # str(';')
    chunk.loadReference(path, format, columns, delimiter)

def set_calibration(chunk, calibration):
    path = calibration['path']                 # str('calibration.xml')
    format = calibration['format']             # str('xml')
    calib = PhotoScan.Calibration()
    calib.load(path, format)
   
    for sensor in chunk.sensors:
        sensor.user_calib = calib
        sensor.fixed = True
        # sensor.fixed = False