How can I run MATLAB code for isolated spoken words recognition from PHP?
Asked Answered
E

4

18

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 PHP for some purpose.

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.

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.

Entoblast answered 11/4, 2013 at 18:19 Comment(1)
In case you don't want to install MATLAB on the server, you can use SaturnAPI, which provides a REST interface for you to make HTTP calls and transfer data back and forth. saturnapi.comKammerer
I
19

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 of EXECUTE command in PHP. Here are some resources (make sure to also checkout the linked questions as well):

    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 Compiler and related toolboxes like MATLAB Builder NE and 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 Runtime to 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).

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:

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).

Iredale answered 19/4, 2013 at 7:59 Comment(2)
+1 for such a great description. What I was trying, is to use matlab builder JA. so compiling as a jar file was my first option to start with. but as usual I'm not having a full control on the deployment server. aw, using java with php should not be a problem. I just need to consider some security issues. Thank you very much on your answer. I'm going to accept it. but please if you have any other information you might like to share with me about Matlab->JAR->PHP ( Voice input -- string and image output ) I will greatly appreciate it. Thanks.Entoblast
@mamdouhalramadan: there is currently another open question about PHP/MATLAB integration that might be of interest to you. Specifically Sam Roberts has some great advice about how to reduce the MCR startup overhead..Iredale
N
4

To help the OP with running system commands from a PHP webpage, my post here is relevant (copied below).

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.

Norven answered 15/4, 2013 at 23:12 Comment(3)
Thank you, but still my question is set for bounty because I need more detailed answer. Really appreciate your help. but if you can take a look at the matlab code you will see that it can't be done this way,Entoblast
Matlab I do not know. If you can accomplish what you seek via terminal command (like the below answers), then I thought my answer would help with the easier part of the problem.Norven
it's not about having it via terminal commands. Yes, eventually this might be a good way to do it. but what I'm seeking after is much more than executing a program, cuz I need to interact with with the matlab code. I can use the java builder and/or any other feature from matlab to have a full control but I need to know what are all the steps in there to be done as it's not a common issue I'm afraid that I should make a twist to make it. but what is that twist?Entoblast
B
4

My answer would be in two parts:

  1. 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
    
  2. 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.

More notes:

  1. Watch for your security. You might be XSS'ed easily.
  2. Abstract your code and improve it to save parameters and output to the database. Run your code in Parallels or queue manager. You might create a REST service.
  3. Unit test.
  4. Use Linux. It's much more powerful.
Bourgeoisie answered 16/4, 2013 at 7:8 Comment(0)
H
3

One quick hack would be to compile your MATLAB code into an executable file then use PHP's shell_exec().

The difficult part would be adapting your MATLAB code (sorry, I didn't read it) in such a way that:

  1. It will receive its input in command-line-interface style (as char strings);
  2. It will output its results as text to standard output (file id #1 in MATLAB).

Then all it takes is to parse the MATLAB output back into PHP...

Haff answered 11/4, 2013 at 22:26 Comment(4)
thank you for your response, the problem is that the input is a .wav format and the output is an image and string... would that be valid?Entoblast
If I would try the executable Matlab way, I would pass the path+filename.wav as argument to the Matlab executable, and the program would output 2 strings (separated by new-line, for example): one would be the string that you need, and the other would be the path+filename.png.Haff
One more thing --- here's another Stackoverflow entry dealing with passing arguments to Matlab executables: #3336005Haff
Watch out for security problems here. Make sure to escape any arguments that are not hard-coded into the application if the PHP aplication is open to untrusted users.Alpine

© 2022 - 2024 — McMap. All rights reserved.