Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: Tom2L on May 17, 2022, 10:37:37 AM

Title: align model to model.py without GUI
Post by: Tom2L on May 17, 2022, 10:37:37 AM
Hi all,
I'd like to use the align model to model. py script in a larger scale python project.
In odrer to do so, i've extracted the functions from align model to model. py to my script, but i get the error :

Code: [Select]
Traceback (most recent call last):
  File "C:/Users/Thomas/Desktop/pycharm/crash_code.py", line 532, in <module>
    align_two_point_clouds(chunk.models[-1],chunk.models[1], scale_ratio = 0.99358)
  File "C:/Users/Thomas/Desktop/pycharm/crash_code.py", line 27, in align_two_point_clouds
    assert(isinstance(points1_source, np.ndarray) and isinstance(points2_target, np.ndarray))
AssertionError


I'm fairly new to python so i don't have enough experience to see what went wrong, that's why i'm asking for help.
Could anyone help me with this ?
I've attached the whole code below
Thanks in advance !
Title: Re: align model to model.py without GUI
Post by: PolarNick on May 17, 2022, 12:55:59 PM
Hi, these asserts check that input point clouds (arguments for align_two_point_clouds function) are numpy arrays. You called that function with chunk.models[-1] - which is not a numpy array.

In original script conversion from Metashape's project model representation to numpy array type is implemented via export to ply file (https://github.com/agisoft-llc/metashape-scripts/blob/e1a71de567267c6d4daedfb49cc436b3f2c7b040/src/align_model_to_model.py#L485) and reading it as numpy array (https://github.com/agisoft-llc/metashape-scripts/blob/e1a71de567267c6d4daedfb49cc436b3f2c7b040/src/align_model_to_model.py#L502).
Title: Re: align model to model.py without GUI
Post by: Tom2L on May 17, 2022, 04:45:01 PM
Thanks a lot for your answer. Unfortunately, i don't get it very well : should i export my models as .ply file and then read then as numpy array ?
similary to :
Code: [Select]
                    self.chunk.exportModel(path=filename, binary=True,
                                      save_texture=False, save_uv=False, save_normals=False, save_colors=False,
                                      save_cameras=False, save_markers=False, save_udim=False, save_alpha=False,
                                      save_comment=False,
                                      format=Metashape.ModelFormatPLY)
                else:
                    self.chunk.dense_cloud = None
                    for dense_cloud in self.chunk.dense_clouds:
                        if dense_cloud.key == key:
                            self.chunk.dense_cloud = dense_cloud
                    assert(self.chunk.dense_cloud is not None)
                    self.chunk.exportPoints(path=filename,
                                       source_data=Metashape.DenseCloudData, binary=True,
                                       save_normals=False, save_colors=False, save_classes=False, save_confidence=False,
                                       save_comment=False,
                                       format=Metashape.PointsFormatPLY)

            v1 = read_ply(tmp1.name)
            v2 = read_ply(tmp2.name)
Title: Re: align model to model.py without GUI
Post by: PolarNick on May 17, 2022, 05:27:11 PM
Yes, looks like the simplest way.