Hi Alexey,
It worked perfectly, thanks for the help.
And since so much of my work has been sourced from codes posted here, I thought I'd share mine as well. Below is my code that loops through multiple directories (representing different years), imports images from the year, and aligns images with an existing project. If non of the new images align, that is all, but if any of the new images align, the script continues through the PS workflow, with the ultimate goal of orthorectifying the images. The code takes a while to run depending on the accuracy of aligning points and the resolution of the dense cloud, so it also writes the results of each loop to a text file so that the progress can be checked while it's running.
#!/usr/bin/python
# python script for photoscan- loop through all years (directories) of glacier photos, run through PS workflow, and orthorectify glacier photos after matching
import PhotoScan
import os
import time
import glob
project_mask = "/Volumes/Brew_mask.psx" # link to existing mask to use
old_photos_path = "/Volumes/BrewPhotos/" # path to old photos
save_path = "/Volumes/BrewProjects/" # where to save projects
save_filepre = "brew" # file name to save
file = open(save_path+"generate_orthophoto_log.txt", "w") # log file
file.write("Generate Orthophotos, mask: %s\n" % project_mask)
file.close()
# loop through directory for each year of old photos
year_list = os.listdir(old_photos_path) # list all directories (years)
for i in year_list:
start = time.time() # time each loop
file = open(save_path+"generate_orthophoto_log.txt", "a")
file.write("Year: %s\n" % i)
# open existing mask
doc = PhotoScan.app.document
doc.open(project_mask)
chunk = doc.chunk
# go to correct directory, list photos in that directory (for that year)
os.chdir(old_photos_path+i)
photo_list = glob.glob("*.tif") # list of paths for each tiff
# build model
chunk.addPhotos(photo_list)
#crs = PhotoScan.CoordinateSystem("EPSG::2193") # 2193 is NZTM 2000, this shouldn't be needed since mask is already in NZTM
chunk.matchPhotos(accuracy=PhotoScan.LowAccuracy,preselection=PhotoScan.NoPreselection,filter_mask=True,keypoint_limit=100000,tiepoint_limit=4000)
chunk.alignCameras()
aligned_photos = [] # empty list
for camera in chunk.cameras:
if camera.label in photo_list and camera.transform:
aligned_photos.append(camera) # creates list of glacier photos that aligned
a = len(aligned_photos)
if a > 0: # if loop to only go through workflow and orthorectify if glacier photos were matched
chunk.buildDenseCloud(quality=PhotoScan.LowestQuality,filter=PhotoScan.ModerateFiltering)
chunk.buildModel(surface=PhotoScan.HeightField,source=PhotoScan.DenseCloudData,face_count=PhotoScan.HighFaceCount,interpolation=PhotoScan.EnabledInterpolation)
chunk.buildUV(mapping=PhotoScan.GenericMapping)
#chunk.buildTexture(blending=PhotoScan.MosaicBlending, size=4096) # don't need this to build ortho, but can uncomment if wanted to build model
chunk.buildOrthomosaic(surface=PhotoScan.ModelData,blending=PhotoScan.MosaicBlending,color_correction=False) # can add 'projection=crs' if need to specify projection
# orthorectify photos
for camera in chunk.cameras:
camera.enabled = False # disables all photos
for camera in chunk.cameras:
if camera.label in photo_list:
camera.enabled = True # enables EoSS photos
chunk.exportOrthophotos(save_path+"ortho_{filename}.tif", projection = chunk.crs)
doc.save(path = save_path+save_filepre+i+".psx")
file.write("Photos aligned: %s\n" % aligned_photos)
else:
doc.save(path = save_path+save_filepre+i+".psx")
file.write("No EoSS photos aligned from this year\n")
end = time.time()
compt = end - start
file.write("time: %f\n" % compt)
file.close()