如何运行 MATLAB 代码以从 PHP 中识别孤立的口语单词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15956000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run MATLAB code for isolated spoken words recognition from PHP?
提问by mamdouh alramadan
As the title indicates, I have MATLAB code for isolated spoken words recognition, and I want to be able to integrate this project with another one made with PHPfor some purpose.
正如标题所示,我有 MATLAB 代码用于识别孤立的口语单词,我希望能够将此项目与出于某种目的使用PHP制作的另一个项目集成。
I have not used to deal with such problem before. In other words, it's the first time for me when I need to integrate PHP and MATLAB, so I really don't know where to start and how.
我以前没有处理过这样的问题。换句话说,我是第一次需要整合PHP和MATLAB,所以我真的不知道从哪里开始,如何开始。
I have read a couple of articles, but I couldn't make it valid.
我已经阅读了几篇文章,但我无法使其有效。
I have PHP 5.4.9, MATLAB R2012A and Windows 7. The MATLAB project files can be seen on GitHub.
我有 PHP 5.4.9、MATLAB R2012A 和 Windows 7。MATLAB 项目文件可以在 GitHub 上看到。
回答by Amro
You have a few options here:
您在这里有几个选择:
If MATLAB installed on the server where the PHP application would be deployed (not your current development environment), you can invoke it directly just like any other program (
matlab -r "..."
) using whatever is the equivalent ofEXECUTE
command in PHP. Here are some resources (make sure to also checkout the linked questions as well):- How to call MATLAB from command-line and print to stdout before exiting
- Running a cmd file without GUI popping up
- Pass Parameters _ Shell Script - Octave Script
Others have commented on how to pass input/output between PHP and your MATLAB script. For example, you could design your MATLAB function to receive the path of WAV file as input, process it and save any resulting image to disk:
function myFunc(filename) [y,Fs] = audioread(filename); img = my_process_func(y, FS); imwrite(img, 'out.png'); end
Which is invoked from PHP as:
% Of course you have to make sure "myFunc" is available on the MATLAB path. % Think: "addpath(..)" or just "cd(..)" into the directory first matlab -wait -nodisplay -r "myFunc('audio.wav'); quit;"
You could then read the output image in the PHP application.
If not, what deployment-related toolboxes do you have available? MATLAB Compilerand related toolboxes like MATLAB Builder NEand MATLAB Builder JA.
Those will compile your program into an executable/.NET Assembly/JAR file respectively, and all of them require the freely available MCR Runtimeto be installed. In other words, the executables do not need to have a full MATLAB installation on the target machine, only the MCR runtime.
You would run the executable in the same manner as before.
Another product is the MATLAB Coder, which converts your MATLAB code into C++ program. When compiled, it can run without any external requirement.
A new product by MathWorks is MATLAB Production Server. Personally I know nothing about it :)
Yet another option is to use TCP/IP to communicate between PHP and MATLAB. A server would be run on the MATLAB side, using socket programming written as C MEX-file or a Java class. See:
The client being your PHP application. The idea is to have MATLAB listening for connections, reading whatever input is given by a client,
eval
it, and return the result. This is more involved than the other options, as you have to deal with serialization and other things like concurrency. The advantage is that MATLAB can be run on a separate server, even on multiple servers on the cloud (see this post).
如果 MATLAB 安装在将部署 PHP 应用程序的服务器上(不是您当前的开发环境),您可以像任何其他程序 (
matlab -r "..."
) 一样使用EXECUTE
PHP 中的等效命令直接调用它。以下是一些资源(请务必同时查看链接的问题):其他人评论了如何在 PHP 和您的 MATLAB 脚本之间传递输入/输出。例如,您可以设计您的 MATLAB 函数以接收 WAV 文件的路径作为输入,对其进行处理并将任何结果图像保存到磁盘:
function myFunc(filename) [y,Fs] = audioread(filename); img = my_process_func(y, FS); imwrite(img, 'out.png'); end
这是从 PHP 调用的:
% Of course you have to make sure "myFunc" is available on the MATLAB path. % Think: "addpath(..)" or just "cd(..)" into the directory first matlab -wait -nodisplay -r "myFunc('audio.wav'); quit;"
然后您可以在 PHP 应用程序中读取输出图像。
如果没有,您有哪些与部署相关的工具箱可用?MATLAB Compiler和相关工具箱,如MATLAB Builder NE和MATLAB Builder JA。
这些将分别将您的程序编译为可执行/.NET 程序集/JAR 文件,并且所有这些都需要安装免费提供的MCR 运行时。换句话说,可执行文件不需要在目标机器上安装完整的 MATLAB,只需安装 MCR 运行时。
您将以与以前相同的方式运行可执行文件。
另一个产品是MATLAB Coder,它将您的 MATLAB 代码转换为 C++ 程序。编译后,它可以在没有任何外部要求的情况下运行。
MathWorks 的新产品是MATLAB Production Server。我个人对此一无所知:)
另一种选择是使用 TCP/IP 在 PHP 和 MATLAB 之间进行通信。服务器将在 MATLAB 端运行,使用编写为 C MEX 文件或 Java 类的套接字编程。看:
客户端是您的 PHP 应用程序。这个想法是让 MATLAB 监听连接,读取客户端给出的任何输入
eval
,然后返回结果。这比其他选项更复杂,因为您必须处理序列化和其他诸如并发之类的事情。优点是 MATLAB 可以在单独的服务器上运行,甚至可以在云上的多台服务器上运行(请参阅此帖子)。
So first, decide what approach best suits your project, then it would be easier to answer specific questions... Just always consult the documentation first, MATLAB toolboxes are very well documented and usually include many examples. Here are a couple more resources specific to MATLAB Compiler products family:
因此,首先,决定哪种方法最适合您的项目,然后回答具体问题会更容易……只要始终先查阅文档,MATLAB 工具箱都有很好的文档记录,通常包含许多示例。以下是更多特定于 MATLAB Compiler 产品系列的资源:
- Webinar: Application Deployment with MATLAB
- PDF File: MATLAB Application Deployment - Web Example Guide
Note that they concentrate on ASP.NET and Java JSP/servlet applications. In your case, the PHP application would communicate with a middle tier running a web service built using one of the above two options (or simply design a CGI-like site running plain executables built using the MATLAB Compiler as explained earlier).
请注意,它们专注于 ASP.NET 和 Java JSP/servlet 应用程序。在您的情况下,PHP 应用程序将与运行使用上述两个选项之一构建的 Web 服务的中间层通信(或者简单地设计一个类似 CGI 的站点,运行使用 MATLAB 编译器构建的普通可执行文件,如前所述)。
回答by Chris K
To help the OP with running system commands from a PHP webpage, my post hereis relevant (copied below).
为了帮助 OP 从 PHP 网页运行系统命令,我在这里的帖子是相关的(复制如下)。
We do exactly this all the time. I call them voodoo pages. Here's some working code:
我们一直都在这样做。我称它们为巫毒页面。这是一些工作代码:
<?php
$command="uptime"; $output; $retval; $errors="";
exec ($command, &$output, &$retval);
echo $output[0] . "\n";
unset($output);
?>
And the output to the webpage served:
并输出到所提供的网页:
13:40:19 up 22 days, 23:14, 0 users, load average: 0.04, 0.02, 0.00
And the additional note I added in the comments below:
Relative vs absolute paths may be a pain... $command
might need to be /usr/bin/uptime
or another could be /usr/bin/ls /home/chris/ftp
. Normally, scripts' working directory is where they live in the file system. MATLAB is a windows program, yes? My experience is you will need absolute paths for the program and any files passed as arguments, example: $command="c:\\matlab\\matlab.exe c:\\www\\somefile.wav"
And then single quotes required for silly NTFS names, TAB command line completion works well for examples. Or use proper 8.3 name with the ~ in it.
以及我在下面的评论中添加的附加说明:相对路径与绝对路径可能会很痛苦...$command
可能需要/usr/bin/uptime
或另一个可能是/usr/bin/ls /home/chris/ftp
. 通常,脚本的工作目录是它们在文件系统中的位置。MATLAB 是一个 windows 程序,是吗?我的经验是,您将需要程序的绝对路径和作为参数传递的任何文件,例如:$command="c:\\matlab\\matlab.exe c:\\www\\somefile.wav"
然后是愚蠢的 NTFS 名称所需的单引号,TAB 命令行完成对于示例来说效果很好。或者使用正确的 8.3 名称,其中包含 ~。
回答by tawfekov
My answer would be in two parts:
我的回答将分为两部分:
How do I run a MATLAB script from the terminal? I will give some example about running a MATLAB script from the terminal:
matlab -nojvm -nodesktop -r "run <the-script>.m" matlab -nojvm -nodesktop -r "<the-script>" matlab -nojvm -nodesktop -r "run <the/path>/<the-script>.m"
matlab
in Windows must be in your environment path. How-to.If you need to compile your script to Java:
java -jar yourjarfile.jar
How do I execute the terminal command from PHP? I think the previous answers are good, and there isn't any need to repeat them.
如何从终端运行 MATLAB 脚本?我将给出一些有关从终端运行 MATLAB 脚本的示例:
matlab -nojvm -nodesktop -r "run <the-script>.m" matlab -nojvm -nodesktop -r "<the-script>" matlab -nojvm -nodesktop -r "run <the/path>/<the-script>.m"
matlab
在 Windows 中必须在您的环境路径中。操作方法。如果您需要将脚本编译为 Java:
java -jar yourjarfile.jar
如何从 PHP 执行终端命令?我觉得前面的回答很好,没有必要重复。
More notes:
更多注意事项:
回答by tawfekov
One quick hack would be to compile your MATLAB code into an executable filethen use PHP's shell_exec()
.
一个快速的技巧是将您的 MATLAB 代码编译成一个可执行文件,然后使用 PHP 的shell_exec()
.
The difficult part would be adapting your MATLAB code (sorry, I didn't read it) in such a way that:
困难的部分是以这样的方式调整您的 MATLAB 代码(抱歉,我没有阅读它):
- It will receive its input in command-line-interface style (as
char
strings); - It will output its results as text to standard output (file id #1 in MATLAB).
- 它将以命令行界面样式(作为
char
字符串)接收其输入; - 它将其结果作为文本输出到标准输出(MATLAB 中的文件 id #1)。
Then all it takes is to parse the MATLAB output back into PHP...
然后所需要的就是将 MATLAB 输出解析回 PHP ...