Problem / Question
Hello ![]()
I only found a documentation of using PartFinder with cpp Interface. Is there a documentation about the functions to access the results of PartFinder when using python?
Thanks!
Hello ![]()
I only found a documentation of using PartFinder with cpp Interface. Is there a documentation about the functions to access the results of PartFinder when using python?
Thanks!
Hi,
as for the PartFinder entry in the guide, there are indeed only C++ examples.
We do have Python examples, although not explicitly for PartFinder. However, since using PartFinder boils down to using the NxLib, which is demonstrated in the Python section, I suggest you have a look at the Python examples in the guide and infer for PartFinder.
Should you still struggle with Python, we are happy to assist!
Best,
Bernhard
Thanks for the reply,
sorry I still don’t get e.g. how to load model parameters of a generated model into partfinder and how to access the results in the end with python.
Providing a short example code would help heaps, thanks ![]()
Sure, here is a quick example how to load a generated model into PartFinder and obtain the results:
from nxlib import NxLib, NxLibCommand
from nxlib.constants import *
with NxLib():
try:
with NxLibCommand(CMD_OPEN) as cmd:
cmd.parameters()[ITM_CAMERAS] = "<your camera>"
cmd.execute()
with NxLibCommand(CMD_CAPTURE) as cmd:
cmd.execute()
with NxLibCommand(CMD_COMPUTE_DISPARITY_MAP) as cmd:
cmd.execute()
with NxLibCommand(CMD_PART_FINDER) as cmd:
cmd.parameters()[ITM_FUNCTION] = VAL_LOAD_MODEL
cmd.parameters()[ITM_MODEL_PATH] = "<generated model path>"
cmd.execute()
cmd.parameters()[ITM_FUNCTION] = VAL_FIND
cmd.parameters()[ITM_MODEL_ID] = cmd.result()[ITM_MODEL_ID].as_int()
cmd.execute()
objects = cmd.result()[ITM_OBJECTS]
for i in range(objects.count()):
print(f"Object {i}:")
print(f"Coverage: {objects[i][ITM_COVERAGE].as_double()}")
print(f"Score: {objects[i][ITM_SCORE].as_double()}")
print(f"Texture: {objects[i][ITM_TEXTURE].as_double()}")
print(f"ViewPointIndex: {objects[i][ITM_VIEWPOINT_INDEX].as_int()}")
print()
except Exception as e:
print(e)
Let me know if this was helpful!
Thank you already for your code.
I still have a problem with the cmd.execute() part of
“NxLibCommand(CMD_PART_FINDER) as cmd:
cmd.parameters()[ITM_FUNCTION] = VAL_LOAD_MODEL
cmd.parameters()[ITM_MODEL_PATH] = “”
cmd.execute()”
As error there’s written:
An NxLibError occured: Error Text: NxLib error 17 (NxLibExecutionFailed) for item /Execute/Temporary
…\Python\Python39\lib\site-packages\nxlib\command.py", line 131, in execute
self.assert_successful()
…\Python\Python39\lib\site-packages\nxlib\command.py", line 153, in assert_successful
Sorry, I didn’t close the NxView App completely, that’s why the error occured.
Nevertheless, I’m not quiet sure how to access the parameters like “Score” from the objects.
If I’d implement print(f"Score: {objects[i][ITM_SCORE].as_double()}") instead of the constant ITM_COVERAGE, I get another error.
How can I figure out which constants belong to what in general?
And what’s a good way to get an insight in how the parameters / json files belonging to Part finder are changing and the cmd.result()?
Ah, I wrote ITM_COVERAGE for everything. Sorry for that. Please have a look at the example again.
The example script opens its own NxLib instance. So whatever problem you faced, NxView should not have been the problem.
When you see that the assert_successful throws, it means the NxLib command returned an error. I have updated the example to use try and catch . Run your code again and take a look what the error says.
To get an insight into the PartFinder command, have a look at the manual. The Python interface is just a wrapper that calls the NxLib, so everything specified in the manual applies to Python as well. Navigating to Result/Objects, it says:
Outputs all found parts for the Find and Confirm functions.
with “Format: Array”. Thus, we know we can iterate over cmd.result()[ITM_OBJECTS] and access the nodes that are specified in the manual:
If you have an IDE, you can hover over ITM_COVERAGE, for example, and go to its definition. Your IDE should open constants.py where you can see all constants defined for the NxLib.
Hope this helps!