首页 > 解决方案 > 为什么我不能在 C# 中使用 ProcessStartInfo() 将参数传递给 python 文件

问题描述

我正在实现一个在 TensorFlow 上运行的图像分类应用程序。我能够使用 Python 成功获得所有结果,现在正在使用我熟悉的 C# 实现 GUI。我在这里使用示例。我的问题是我不能像在 Python 中那样传递参数,我在 python 文件旁边键入它们。

这就是我设置标题信息的方式

string cmd = @"c:\flowers\label_image.py";
string args = @"--input_layer=Placeholder --output_layer=final_result --image=c:/flower_photos/daisy/21652746_cc379e0eea_m.jpg";
            start.FileName = @"C:\Users\pubud\AppData\Local\Programs\Python\Python36\python.exe";                
start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);

当上面执行时,我得到

"c:\flowers\label_image.py" "--input_layer=Placeholder --output_layer=final_result --image=c:/flower_photos/daisy/21652746_cc379e0eea_m.jpg"
2019-01-05 08:55:25.369320: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
  File "c:\flowers\label_image.py", line 127, in <module>
    input_operation = graph.get_operation_by_name(input_name)
  File "C:\Users\pubud\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3618, in get_operation_by_name
    return self.as_graph_element(name, allow_tensor=False, allow_operation=True)
  File "C:\Users\pubud\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3490, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "C:\Users\pubud\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3544, in _as_graph_element_locked
    (repr(name), types_str))
ValueError: Name 'import/Placeholder --output_layer=final_result --image=c:/flower_photos/daisy/21652746_cc379e0eea_m.jpg' appears to refer to a Tensor, not a Operation.

这里的问题是 label_image.py 不理解参数并尝试运行默认值。

但是,如果我删除参数并调用 label_image.py 并将默认值硬编码到 py 文件中,它的工作正常。

start.Arguments = string.Format("\"{0}\"", cmd);

为什么这不理解我的论点?

标签: pythontensorflow

解决方案


以下提供的格式系统税导致错误。(它将“”传递给参数字符串)

start.Arguments = string.Format("\"{0}\" \"{1}\"", cmd, args);

所以改为如下:

start.Arguments = string.Format(@"{0} {1}", cmd, args);

它现在正在工作。


推荐阅读