在C#或Unity中执行Python脚本 [英] Executing Python Script in C# or Unity

查看:1682
本文介绍了在C#或Unity中执行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中使用Keras设计了一个神经网络,用于计算诸如降噪功能之类的东西.到目前为止,它的运行情况还不错,现在我想使用此网络来清理我的Unity项目中的数据.

I used Keras in Python to design a neural network calculating something like a noise-reducing-function. It works pretty good so far, and now I want to use this network to clean the data inside a Unity-Project of mine.

我不会想到这可能会如此困难.我在资产商店中只能找到一个python解释器,它不支持外部python库. IronPython也不是一种选择,因为我需要包括Keras软件包. 我在GitHub上找到了KerasSharp项目,但是没有关于如何加载已经训练好的网络的文档,并且在一开始就不能对其进行训练.此外,由于提交历史和未解决的问题,似乎不再有人在该项目上工作.由于延迟,通过网络API访问脚本可能也不是一个好选择.我需要每一帧的计算.

I would not have thought that this could be so difficult. I could only find one python interpreter in the asset store, which does not support external python librarys. IronPython is not an option either, because I need to include the Keras Packages. I found a KerasSharp Project on GitHub, but there is no documentation on how to load an already trained network, and training it at the beginning is not an option. Furthermore it seems like there is no one working on the project anymore, due to the commit history and unanswered questions. Accessing the script via network APIs is probably not an good option either, due to the latency. I need the calculation for every frame.

所以我的问题是:有什么方法可以在C#或Unity中加载Keras/Tensorflow模型

So my question is: Is there any way I can load a Keras/Tensorflow model in C# or Unity

我可以以某种方式访问​​使用Keras模型计算降噪功能的python脚本吗?

Can I somehow access the python script which is calculating the noise-reducing-function using the Keras model?

推荐答案

如果您的情况允许您在Unity之后启动python脚本,则可以尝试将python脚本作为子进程启动,如下所述:

If your situation allows for you to start the python script after Unity, you can try starting the python script as a subprocess as described here:

http://answers.unity.com/answers/14156/view.html

如果您不需要在Unity之前运行其他进程 一,您可以通过Process和 然后将stdin/out重定向到流并通过这些流进行通信.

If you do not require the other process to be running before the Unity one, you could have your Unity project launch that via Process and then redirect stdin/out to streams and communicate through these.

示例:

Process otherProcess = new Process ();
otherProcess.StartInfo.FileName = path;
otherProcess.StartInfo.CreateNoWindow = true;
otherProcess.StartInfo.UseShellExecute = false;
otherProcess.StartInfo.RedirectStandardInput = true;
otherProcess.StartInfo.RedirectStandardOutput = true;

// Now communicate via streams
//     otherProcess.StandardOutput
// and
//     otherProcess.StandardInput

也有可能通过名称或pid捕获正在运行的进程,并且 然后设置转发即可,但我尚未对此进行测试, 看起来确实值得怀疑.

It is also possible that grabbing a running process by name or pid and then setting the forwarding would work, but I've not tested this and it does seem rather doubtful.

此设置要求您的python脚本能够从标准输入中接收数据,并在标准输出上输出其结果.

This setup would require that your python script be able to take in data from standard in, and output its results over standard out.

这篇关于在C#或Unity中执行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆