Forum

Author Topic: Python script crashes Metashape  (Read 2103 times)

smescarzaga

  • Newbie
  • *
  • Posts: 25
    • View Profile
Python script crashes Metashape
« on: November 11, 2019, 09:06:53 PM »
I need help understanding why the script attached here is crashing Metashape. It covers gradual selection and optimization and there is essentially three parts: reconstruction uncertainty, projection accuracy and reprojection error in that order. Executing reporjection error isnt as straight forward as I need the script to check total camera error and stop if it is already below some threshold (20cm) and not let the program iterate the process more than 4 times.

The reconstruction uncertainty and projection accuracy sections together run just fine. It's when I add the reporjection error section that Metashape crashes. I've also attached here the log file of the crash. It stops before recalculating and reporting camera error in the for-loop in the script. I believe something here is causing it to crash.

Do you have any ideas on how to fix this issue?

Script:

Code: [Select]
import Metashape, math, sys


doc = Metashape.app.document #specifies open document
chunk = doc.chunk #specifies active chunk
T = chunk.transform.matrix
crs = chunk.crs
pc = chunk.point_cloud #PointCloud object of sparse cloud points
pc_init = len(pc.points) #returns integer of number of sparse cloud points
print("****Number of starting points:", pc_init) #prints initial point number in raw sparse cloud

# Below starts the gradual selection, filtering and optimization process

#Reconstrution Uncertainty
threshold = 15
f = Metashape.PointCloud.Filter()
f.init(pc, criterion = Metashape.PointCloud.Filter.ReconstructionUncertainty)
values = f.values.copy()
values.sort()
thresh = values[int(len(values) * (1-threshold/100))]
f.selectPoints(thresh)
nselected = len([p for p in pc.points if p.selected])
pc.removeSelectedPoints()
print("****", nselected, " points removed during reconstuction uncertainty filtering")
#Camera optimization
chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=False, fit_b2=False, fit_k1=True,
fit_k2=True, fit_k3=True, fit_k4=False, fit_p1=True, fit_p2=True, fit_p3=False,
fit_p4=False, adaptive_fitting=False, tiepoint_covariance=False)
#Report Total Camera Error
sums = 0
num = 0
for camera in chunk.cameras:
    if not camera.transform:
         continue
    if not camera.reference.location:
         continue

    estimated_geoc = chunk.transform.matrix.mulp(camera.center)
    error = chunk.crs.unproject(camera.reference.location) - estimated_geoc
    error = error.norm()
    sums += error**2
    num += 1
print("****Total Camera Error: ", round(math.sqrt(sums / num),3))
#doc.save()

#Projection Accuracy
threshold = 15
f = Metashape.PointCloud.Filter()
f.init(pc, criterion = Metashape.PointCloud.Filter.ProjectionAccuracy)
values = f.values.copy()
values.sort()
thresh = values[int(len(values) * (1-threshold/100))]
f.selectPoints(thresh)
nselected = len([p for p in pc.points if p.selected])
pc.removeSelectedPoints()
print("****", nselected, " points removed during projection accuracy filtering")
#Camera optimization
chunk.optimizeCameras(fit_f=True, fit_cx=True, fit_cy=True, fit_b1=False, fit_b2=False, fit_k1=True,
fit_k2=True, fit_k3=True, fit_k4=False, fit_p1=True, fit_p2=True, fit_p3=False,
fit_p4=False, adaptive_fitting=False, tiepoint_covariance=False)
#Report Total Camera Error
sums = 0
num = 0
for camera in chunk.cameras:
    if not camera.transform:
         continue
    if not camera.reference.location:
         continue

    estimated_geoc = chunk.transform.matrix.mulp(camera.center)
    error = chunk.crs.unproject(camera.reference.location) - estimated_geoc
    error = error.norm()
    sums += error**2
    num += 1
print("****Total Camera Error: ", round(math.sqrt(sums / num),3))
#doc.save()

# Reprojection Error
# This step needs to be iterated until until camera accuracy reaches 20cm. Should
# sucessive iterations not decrease that number for some reason, this step will be
# limitted to 4 iterations.

for i in range(4):
if (round(math.sqrt(sums / num))) <= 0.20:
sys.exit('****Camera error has reached ~20cm')
#doc.save()
else:
threshold = 10
f = Metashape.PointCloud.Filter()
f.init(pc, criterion = Metashape.PointCloud.Filter.ReprojectionError)
values = f.values.copy()
values.sort()
thresh = values[int(len(values) * (1-threshold/100))]
f.selectPoints(thresh)
nselected = len([p for p in pc.points if p.selected])
pc.removeSelectedPoints()
print("****", nselected, " points removed during reprojection error filtering")
#Camera optimization
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=True, fit_p1=True, fit_p2=True, fit_p3=True,
fit_p4=True, adaptive_fitting=True, tiepoint_covariance=True)
#Report Total Camera Error
sums = 0
num = 0
for camera in chunk.cameras:
    if not camera.transform:
         continue
    if not camera.reference.location:
         continue

    estimated_geoc = chunk.transform.matrix.mulp(camera.center)
    error = chunk.crs.unproject(camera.reference.location) - estimated_geoc
    error = error.norm()
    sums += error**2
    num += 1
print("****Total Camera Error: ", round(math.sqrt(sums / num),3))
#doc.save()

Log File:
Code: [Select]
2019-11-11 09:54:50 Agisoft Metashape Professional Version: 1.5.2 build 7838 (64 bit)
2019-11-11 09:54:50 Platform: Windows
2019-11-11 09:54:50 CPU: Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz (laptop)
2019-11-11 09:54:50 CPU family: 6 model: 158 signature: 906E9h
2019-11-11 09:54:50 RAM: 31.9 GB
2019-11-11 09:54:51 OpenGL Vendor: NVIDIA Corporation
2019-11-11 09:54:51 OpenGL Renderer: Quadro M1200/PCIe/SSE2
2019-11-11 09:54:51 OpenGL Version: 4.6.0 NVIDIA 431.70
2019-11-11 09:54:51 Maximum Texture Size: 16384
2019-11-11 09:54:51 Quad Buffered Stereo: not enabled
2019-11-11 09:54:51 ARB_vertex_buffer_object: supported
2019-11-11 09:54:51 ARB_texture_non_power_of_two: supported
2019-11-11 09:54:51 Using simple console. Rich console can be enabled in Preferences dialog.
2019-11-11 09:54:51 LoadProject: path = C:/Users/smescarzaga/Google Drive/Thesis _ Proposal/CREST/NERTO/NPS_SfM_Scripting/AK2017_OB_Deadhorse_Job_East_delta.psx
2019-11-11 09:54:51 loaded project in 0.057 sec
2019-11-11 09:54:52 Finished processing in 0.058 sec (exit code 1)
2019-11-11 09:55:10 ****Number of starting points: 57847
2019-11-11 09:55:10 Analyzing point cloud...
2019-11-11 09:55:11 Finished processing in 0.198 sec (exit code 1)
2019-11-11 09:55:11 **** 8677  points removed during reconstuction uncertainty filtering
2019-11-11 09:55:11 OptimizeCameras: f, cx, cy, k1-k3, p1, p2, adaptive fitting = 0
2019-11-11 09:55:11 Optimizing camera locations...
2019-11-11 09:55:11 removed 60 cameras: 1115, 1116, 1117, 1118, 1119, 1120, 1122, 1123, 1124, 1125, 1126, 1135, 1136, 1137, 1148, 1149, 1150, 1151, 1152, 1154, 1155, 1156, 1157, 1158, 1159, 180, 181, 185, 186, 187, 188, 189, 190, 211, 212, 213, 214, 215, 217, 218, 219, 220, 221, 222, 223, 226, 889, 890, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923
2019-11-11 09:55:11 removed 60 stations
2019-11-11 09:55:11 adjusting: xxxx 0.197437 -> 0.197061
2019-11-11 09:55:13 coordinates applied in 0 sec
2019-11-11 09:55:13 Finished processing in 1.652 sec (exit code 1)
2019-11-11 09:55:13 ****Total Camera Error:  0.509
2019-11-11 09:55:13 Analyzing point cloud...
2019-11-11 09:55:13 Finished processing in 0.061 sec (exit code 1)
2019-11-11 09:55:13 **** 7375  points removed during projection accuracy filtering
2019-11-11 09:55:13 OptimizeCameras: f, cx, cy, k1-k3, p1, p2, adaptive fitting = 0
2019-11-11 09:55:13 Optimizing camera locations...
2019-11-11 09:55:14 adjusting: xxx 0.203232 -> 0.20322
2019-11-11 09:55:14 coordinates applied in 0 sec
2019-11-11 09:55:14 Finished processing in 1.054 sec (exit code 1)
2019-11-11 09:55:14 ****Total Camera Error:  0.507
2019-11-11 09:55:14 Analyzing point cloud...
2019-11-11 09:55:15 Finished processing in 0.067 sec (exit code 1)
2019-11-11 09:55:15 **** 4179  points removed during reprojection error filtering
2019-11-11 09:55:15 OptimizeCameras: f, cx, cy, b1, b2, k1-k4, p1-p4, adaptive fitting = 1
2019-11-11 09:55:15 Optimizing camera locations...
2019-11-11 09:55:15 adjusting: xxxxx 0.182986 -> 0.1827
2019-11-11 09:55:16 adjusting: x 0.182714 -> 0.182714
2019-11-11 09:55:16 coordinates applied in 0 sec

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python script crashes Metashape
« Reply #1 on: November 12, 2019, 02:19:02 PM »
Hello smescarzaga,

Is it possible to send the project in PSZ format with the alignment results only that can be used together with the script to reproduce the crash to support@agisoft.com?

Also I'm wondering, whether you have already submitted the crash report of if there's anything related to metashape.exe error in Windows Event Viewer -> Windows Logs -> Application section?
Best regards,
Alexey Pasumansky,
Agisoft LLC

smescarzaga

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Python script crashes Metashape
« Reply #2 on: November 12, 2019, 08:54:47 PM »
Thanks for the reply Alexey.

I've just sent an email with a download link to the .psz file and the script.

I've just recreated the crash using the script, checked the Applications section of the Event Viewer and found nothing.

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14012
    • View Profile
Re: Python script crashes Metashape
« Reply #3 on: November 12, 2019, 10:02:21 PM »
Hello smescarzaga,

Thank you for sending the project over.

Are you sure that the script is crashing the application and it is not just an expected behavior of sys.exit() function? I can suggest to substitute this line with common print statement.
Also make sure that you are properly using round() function when checking the condition of the optimization loop exit. I think you should define the number of decimal digits, if you are comparing the value with 0.20:
Code: [Select]
(round(math.sqrt(sums / num),2))
Best regards,
Alexey Pasumansky,
Agisoft LLC