Forum

Author Topic: Mixing RunScript command with other network processes  (Read 4233 times)

eastonmbio

  • Newbie
  • *
  • Posts: 13
    • View Profile
Mixing RunScript command with other network processes
« on: May 25, 2022, 11:54:39 AM »
We are having issues with attempting to integrate the RunScript command into the workflow, since it does not appear to mix well with tasks that are supported by the usual network processing python commands. I am able to perform camera alignment using this syntax without any issue, but when I try to add code to the end that opens the project, performs some required action, then saves the project, the code executes correctly, but on the original project, as if the cameras had never been aligned (i.e. there are no tie points). Can you assist with providing the correct syntax for integrating RunScript into the workflow?

We are using the following syntax to point to the correct script:

Code: [Select]
task = Metashape.Tasks.RunScript()
task.path = "Z:/scripts/PythonScripts/Test_scripts/Perform_action.py"
tasks.append(task)

The code pointed to by RunScript imports the project in the following way before performing the required action:

Code: [Select]

import Metashape as ms


app = ms.app
docpath = app.document.path
doc = ms.Document()
chunk = ms.app.document.chunk

chunk#.dosomething

doc.save()




Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #1 on: May 25, 2022, 01:35:31 PM »
Hello eastonmbio,

In the following thread I gave an example of Run Script task usage via network, that assumed possibility of project path variation:
https://www.agisoft.com/forum/index.php?topic=13390.msg59677#msg59677

Let me know, if it helps.
Best regards,
Alexey Pasumansky,
Agisoft LLC

eastonmbio

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #2 on: May 27, 2022, 02:00:46 AM »
Hi Alexey,

Thank you for your helpful response. I adjusted some of the syntax in the code and it worked.

For my own record, am I right in thinking that if my code opens the current document with the above syntax, if I use a script that also opens the current document as part of a network process after some other action has already been performed using network processing, it should open that document AFTER these actions have been performed, and not in its "old" state, even though there is no specific 'save' command inherent in network processing syntax? The path of the project does not vary between steps, it opens and saves the document without any change.

I believe the problem before was that RunScript was opening the original version of the .psx file, overwriting any previous network processing that had happened earler on in the script. I am not sure exactly the change in my syntax or workflow that made it work correctly, I'm just looking to understand how Metashape deals with these sorts of tasks.

Thank you.

matttob

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #3 on: June 07, 2022, 01:57:09 PM »
Hi eastonmbio,


I'm having a nightmare trying to run a python script as a network task, the worse part is that the script appears to run without errors but does not produce any results. I am trying to run a script that imports images see below .

Code: [Select]
import Metashape
from pathlib import Path
import os
# create list of image file names
# Enter path of highest tier of video files directory structure
video_files_highest_directory = r"C:\Users\Desktop\TEST"
# video_files_highest_directory += "/"
video_files_highest_directory=Path(video_files_highest_directory)
# Search in all sub directories for any files with .mpg extension and create list of full file paths for each video file
video_file_paths = [str(pp) for pp in video_files_highest_directory.glob("**/*.mpg")]
print('file =' + str(len(video_file_paths)))

for image_directories in video_file_paths:
    image_file_path = image_directories.split('\\')
    cropped_image_folder = ('/'.join(map(str, image_file_path[0:-1])))
    image_direcroty = ([ f.path for f in os.scandir(cropped_image_folder) if f.is_dir() ])
    image_direcroty = Path(image_direcroty[0])
    image_file_paths = [str(pp) for pp in image_direcroty.glob("**/*.jpg")]
    print((image_file_paths[0]))

    # doc = Metashape.Document()
    doc = Metashape.app.document   
    chunk = doc.addChunk()
    chunk.label = image_file_path[-1]

    chunk.addPhotos(image_file_paths)
    Metashape.app.update() 

This script runs fine when run locally but when I try to use network processing via  the code below

Code: [Select]
import Metashape

client = Metashape.NetworkClient()
task1 = Metashape.NetworkTask()
task1.name = 'RunScript'
task1.params['path'] = r"W:\MATT\CLUSTER_TEST_import_images_from_folders_metashape.py" #path to the script to be executed
# task1.params['args'] = "argument1 argument2" #string of the arguments with space as separator

path = r"W:\MATT\cluster_test_project.psx"
client.connect('192.168.44.250') #server ip
batch_id = client.createBatch(path, [task1])
client.setBatchPaused(batch_id, paused = False)

print("Job started...")


I can see the batch job appearing in my Agisoft Network Monitor and the job finishes with no errors but yet no images have been imported. I was wondering if you might be willing to share some more of your code that you have posted so far so I can try and follow completely what you are doing :)

Thanks very much in advance

Matt


Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15122
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #4 on: June 07, 2022, 02:29:54 PM »
Hello Matt,

I think that you need to save the created project first:
Code: [Select]
    doc = Metashape.Document()
    doc.save(path)
    #doc = Metashape.app.document   
    chunk = doc.addChunk()
and re-save it when the script is finished:
Code: [Select]
doc.save()
Additionally I suggest to pay attention to the paths: for input images and for the saved project, I recommend to input it in respect of the root path, unless you have a single node and it wouldn't be confused by absolute local paths.
Best regards,
Alexey Pasumansky,
Agisoft LLC

matttob

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #5 on: June 07, 2022, 02:59:39 PM »
Hi Alexy,

Thanks very much for your fast reply :)

I have tried the changes you suggest with regard to saving the project. But I still seem to be getting the same results. Would it be possible to unpack :

"Additionally I suggest to pay attention to the paths: for input images and for the saved project, I recommend to input it in respect of the root path, unless you have a single node and it wouldn't be confused by absolute local paths."

A little further.

 I should have also said I am running this all on windows machines but I am a pretty inexperienced windows user


Matt

matttob

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #6 on: June 07, 2022, 03:12:31 PM »
I think you are right though that my problem may well be path related as I changed my code to

Code: [Select]
doc.save("/cluster_test_project.psx")
to try to reference the root directory and I get two errors from two different nodes:

"Can't open file. The system cannot fine the file specified (2): C:/cluster_test_project.psx"






« Last Edit: June 07, 2022, 06:52:55 PM by matttob »

matttob

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #7 on: June 10, 2022, 03:54:56 PM »
HI Alexey is it possible to post a simple example that would show me how to run a python script only using the Metashape.Tasks.RunScript()  command.

Whatever I try I just cannot get it to work to perform any operations even if I do not receive any path errors in the network monitor.

Should it be possible to run Metashape.Tasks.RunScript()  alone without it being part of a wider list of network tasks?

Thanks very much
« Last Edit: June 10, 2022, 04:49:39 PM by matttob »

eastonmbio

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Mixing RunScript command with other network processes
« Reply #8 on: June 13, 2022, 03:05:23 AM »
Hi Matt,

If you are using a version of Metashape before 1.7.4, I would highly recommend donwloading a newer version. I have found that for myself, 1.7.6 (still downloadable now) produces more than satisfactory results. The syntax differs somewhat from what you posted which is why I ask the question. In 1.7.4 and later, some of the code structure was altered reflective of variation in pathing and it seems to deal with tasks much better now. I will share below an example of some code that duplicates the chunk via network processing.

Code for sending the job to the network is:

Code: [Select]
import Metashape

app = Metashape.app
docpath = app.document.path
doc = Metashape.Document()
chunk = Metashape.app.document.chunk


network_server = 'my_server' #The same name as the host name in the network monitor

Metashape.app.settings.network_path = 'Z:/'  #The drive that is mapped to the network

client = Metashape.NetworkClient()

doc.open(docpath, read_only=False, ignore_lock=True)
# save latest changes
doc.save()

tasks = []  # create task list

chunk = doc.chunk


task = Metashape.Tasks.RunScript()
task.path = "Z:/scripts/EcoRRAP/PythonScripts/Test_scripts/Resize_Dupe.py"
tasks.append(task)



# convert task list to network tasks
network_tasks = []
for task in tasks:
    if task.target == Metashape.Tasks.DocumentTarget:
        network_tasks.append(task.toNetworkTask(doc))
    else:
        network_tasks.append(task.toNetworkTask(chunk))

client = Metashape.NetworkClient()
client.connect(app.settings.network_host)  # server ip
batch_id = client.createBatch(docpath, network_tasks)
client.resumeBatch(batch_id)

Metashape.app.messageBox("Tasks have been sent do the network. Please reopen this project without saving and it will display the progress of the jobs you have just sent.")


The script that this opens is as follows:

Code: [Select]
# Set duplicate to False if you don't want to back up the sparse cloud

############
duplicate = True
############


import Metashape as ms
import math
import sys


app = ms.app
docpath = app.document.path
doc = ms.Document()
chunk = ms.app.document.chunk

doc.open(docpath, read_only=False, ignore_lock=True)
chunk = ms.app.document.chunk



chunk = doc.chunks[0]


# duplicate chunk to preserve source
if duplicate is True:
    chunk_label = chunk.label  # create reference to source chunk
    chunk.copy()  # now duplicate the source chunk
    chunks = ms.app.document.chunks  # update reference to chunks
    # set reference to the duplicated chunks since we will be working with this chunk:
    dupeChunk = ms.app.document.chunks[len(ms.app.document.chunks)-1]
    if dupeChunk in chunks:
        dupeChunk.label = str(chunk_label) + " -PreCleanUnFiltered"  # rename dupe chunk


doc.save()

Hope this helps.