首页 > 解决方案 > 如何让 MatLab 对象在 python 中通过 sys.argv[1]?

问题描述

我正在尝试让 NAO Humanoid Robot 识别物体。我在 MatLab 中使用卷积神经网络 (CNN) 对通过机器人捕获的对象进行分类。我编写了两个单独的 python 脚本:一个通过机器人拍照并将其保存为 .png 文件,第二个使机器人能够通过 SpeechProxy(来自 ALProxy 库)说出对象的标题。第一个脚本运行良好,可以根据需要与 MatLab 进行通信。我使用以下命令运行 python 脚本,读取作为结果创建的 .png,裁剪图像,然后对其进行分类:

system('python "TakePhoto.py path"'); %running the TakePhoto python script
im = imread('object.png path'); %reading the .png file
im = imresize(im,[224 224]); %resizing the .png to the desired dimensions
label = classify(net,im); %classifying the object capture
image(im); %outputting the image in the given dimensions
title(char(label)); %showing the label at the top of the output

我的 MatLab 脚本的这一部分工作正常,因为我只需运行脚本并将对象放在机器人的视图中即可对对象进行分类。我拥有的另一个脚本应该使机器人能够说出对象的标签,但我不确定如何让 python 脚本说出来自 MatLab 输出的标签。

下面的 Python 代码片段是机器人说出标签所需要的。

speechProxy.say("This object is a " + sys.argv[1])

现在的问题是从 MatLab 获取系统参数。我在 MathWorks 网站上搜索过,他们说应该按如下方式传递系统参数

system('python','python_script_path',argument) %assuming argument is an object of certain datatype

我已经用我各自的参数和 python 脚本/路径尝试了这种类型的语法,但我得到了一个错误。如果您参考我显示 MatLab 脚本的第一段代码,则传递参数的语法如下所示:

system('python "speakObject.py path"', label);

但这会导致错误。最终,我在获取 MatLab 脚本和第二个 Python 脚本进行通信时遇到了问题。如何将 .png 的标签分类作为系统参数传递给 python,以便机器人可以说出对象的身份?

标签: pythonmatlabnao-robot

解决方案


您可能想尝试类似的东西

strCommand = sprintf('python speakObject.py %s', label);
system(strCommand);

推荐阅读