Forum

Author Topic: Cannot generate Occlusion Map through Python  (Read 7946 times)

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Cannot generate Occlusion Map through Python
« on: August 17, 2022, 11:58:11 AM »
Hi there! I'm currently testing the Python API for Metashape, and I am in a bit of a pickle regarding the generation of the Occlusion Map.

Everything goes fine with the diffuse map generation, but when getting at the Occlusion map step, I have this error : "Exception : Null model".

Here is my function:

Code: [Select]
def process(doc):
    chunk = doc.addChunk()
    import_images(chunk)
    chunk.matchPhotos(downscale=0, generic_preselection=True, reference_preselection=False, keypoint_limit=40000, keypoint_limit_per_mpx=1000, tiepoint_limit=4000,
                        filter_stationary_points=False, guided_matching=False)
    chunk.alignCameras()
    chunk.buildDepthMaps(downscale=4, filter_mode=Metashape.MildFiltering)
    chunk.buildDenseCloud()
    chunk.buildModel(surface_type=Metashape.Arbitrary, interpolation=Metashape.EnabledInterpolation,
    face_count=Metashape.HighFaceCount, source_data=Metashape.DenseCloudData)
    chunk.buildUV(mapping_mode=Metashape.GenericMapping)
    chunk.buildTexture(blending_mode=Metashape.MosaicBlending, texture_size=16384, texture_type=Metashape.Model.TextureType.DiffuseMap,fill_holes=True )
    chunk.buildTexture(texture_size=16384, texture_type=Metashape.Model.TextureType.OcclusionMap)
    doc.save()

Note : I noticed that, contrary to what it says on the Reference Manual, you actually have to specify "Metashape.Model.TextureType.[YourTexture]" for it to work.

I also tried to launch the code from another script, this one meant to be launched along the GUI, but I had the same exact error. In last resort, I also tried to launch the feature manually from the GUI and watch what it said in the console, but it doesn't show the actual code being run.

Does anybody have an idea :) ? Thank you so much!


PS : I also tried the codes mentioned here https://www.agisoft.com/forum/index.php?topic=13882.msg61297#msg61297, but I couldn't get them to work either. I have another error "Reference model has no texture of specified type..."

Popi
« Last Edit: August 17, 2022, 12:00:53 PM by maepopi »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15366
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #1 on: August 17, 2022, 03:58:31 PM »
Hello Popi,

You should run Occlusion texture map generation in the following way:
Code: [Select]
chunk.buildTexture(texture_type = Metashape.Model.TextureType.OcclusionMap, source_model = chunk.model.key, transfer_texture = False)It is important to define the source model and also set transfer_texture to False, as it is True by default.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #2 on: August 17, 2022, 04:33:53 PM »
Hi Alexey,

Thank you so much, it worked like a charm!

I have another question about the AO, if you're still around. I'm now testing the XML batch process, and I wanted to know whether there was a way to generate the AO there as well. I wrote this :
Code: [Select]
  <job name="BuildTexture" target="all">
    <texture_size>16384</texture_size>
<texture_type>occlusion</texture_type>
  </job>
</batchjobs>

But it just went on and calculated a diffuse instead of an AO ^^. Can you help me there as well?

Thank you again,

Popi

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15366
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #3 on: August 17, 2022, 05:10:01 PM »
Hello Popi,

Occlusion and Normal texture maps are not yet supported for generation via Batch Process.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #4 on: August 17, 2022, 05:27:11 PM »
Hey Alexey,

Oh OK that explains it. Do you have an approximate ETA for that?

Also, does the Cloud Processing allow for generating an AO?

Many thanks,

Best,

Popi
« Last Edit: August 17, 2022, 06:54:39 PM by DocPopi »

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #5 on: August 24, 2022, 06:07:54 PM »
Hello,

I'm upping that subject after having investigated a bit further. I can now send the project to be processed on the cloud, but there's a task that doesn't seem to be working : building the AO. Here is my code:

Code: [Select]
def build_task_list(tasks, chunk):
    network_tasks = []
    '''Match photos'''
    task = Metashape.Tasks.MatchPhotos()
    task.downscale = 0
    task.keypoint_limit = 40000
    task.tiepoint_limit = 4000
    task.generic_preselection = True
    task.reference_preselection = True
    tasks.append(task)

    '''AlignCameras'''
    task = Metashape.Tasks.AlignCameras()
    tasks.append(task)

    '''BuildDepthMaps'''
    task = Metashape.Tasks.BuildDepthMaps()
    task.downscale = 1
    task.filter_mode = Metashape.MildFiltering
    tasks.append(task)

    '''BuildModel'''
    task = Metashape.Tasks.BuildModel()
    task.surface_type = Metashape.Arbitrary
    task.interpolation = Metashape.EnabledInterpolation
    task.face_count = Metashape.HighFaceCount
    task.source_data = Metashape.DepthMapsData
    tasks.append(task)

    '''BuildDiffuse'''
    task = Metashape.Tasks.BuildTexture()
    task.texture_type = Metashape.Model.TextureType.DiffuseMap
    task.blending_mode = Metashape.MosaicBlending
    task.texture_size = 16384
    task.fill_holes = True
    task.ghosting_filter = True
    tasks.append(task)

    '''BuildAO'''
    task = Metashape.Tasks.BuildTexture()
    task.texture_type = Metashape.Model.TextureType.OcclusionMap
    task.texture_size = 16384
    task.source_model = chunk.model.key
    task.transfer_texture = False
    tasks.append(task)

    for t in tasks:
   
            network_tasks.append(t.toNetworkTask(chunk))
       

    return network_tasks


def main():

    output_path = path.join('C:\\', 'GITLAB_PROJECTS', '3dscanpipeline', '02_Production', '05_Scripts', 'Tests', 'hotdog_test.psx')

    tasks = []
    doc = Metashape.Document()
    doc.save(output_path)
    chunk = doc.addChunk()
    import_images(chunk)
    network_tasks = build_task_list(tasks, chunk)
    doc.save()

     # Go retrieve the projects on the cloud
    client = Metashape.CloudClient()
    client.username = **************
    client.password = ***************
    # projects = client.getProjectList()

    client.uploadProject(doc)
    client.processProject(doc,network_tasks)



main()


The code seems to crash at the line where I'm defining the AO source model parameter, with the "chunk.model.key". It returns an error implying that there is no model (null object). I thought that maybe the project had to be processed before being able to create the AO, so I tried uploading the project, then processing it, then downloading again and feeding it the AO task, but it still didn't work.

Can you help me again?

Many thanks,

Best,

Popi


PS: Also, do you have an ETA about the possibility to process an AO through XML batch?

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15366
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #6 on: August 24, 2022, 06:26:44 PM »
Hello Popi,

I think that chunk.model.key is None in the newly created project, as there are no models available yet.

If you try to set the value to 0 instead, providing that it is really a new project and only one mesh model will be generated in the chunk.
Best regards,
Alexey Pasumansky,
Agisoft LLC

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #7 on: August 25, 2022, 06:03:24 PM »
Hi Alexey,

It worked indeed, thank you ! However the result processed on the cloud differs greatly from when it is processed on the local machine (lots of holes). I suppose I didn't put the exact same parameters in the script. I will give that a look  and if the problem persists, I will make another post to avoid confusing threads.

Thank you a lot anyway!

Best,

Popi

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15366
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #8 on: August 25, 2022, 06:09:52 PM »
Hello Popi,

You can try to perform the task locally and check, if you are getting similar results to the Cloud version, so you can debug it faster:

Code: [Select]
chunk = Metashape.app.document.chunk
task = Metashape.Tasks.BuildTexture()
task.texture_type = Metashape.Model.TextureType.OcclusionMap
task.texture_size = 16384
task.source_model = chunk.model.key
task.transfer_texture = False
task.apply(chunk)
Best regards,
Alexey Pasumansky,
Agisoft LLC

DocPopi

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Cannot generate Occlusion Map through Python
« Reply #9 on: August 26, 2022, 11:17:27 AM »
Hello Alexey,

I will try that indeed, and let you know!

Thank you again,

Best,