mrkrd / matlab_wrapper

Easy to use MATLAB wrapper for Python
GNU General Public License v3.0
78 stars 23 forks source link

Is that suitable to call some self-write matlab function in a real-time python environment? #16

Closed Mvrjustid closed 8 years ago

Mvrjustid commented 8 years ago

I need to write a real-time optical flow demo, and since I've already realized the optical flow algorithm I need in Matlab, I'm wondering if I can call the function in python to make the real-time demo. And, the problem is I found the interface you created need to create a command window every time you call the Matlab function, which would cost too much time for a real-time task. Is there any possibility I can finish my task? Thank you.

mrkrd commented 8 years ago

And, the problem is I found the interface you created need to create a command window every time you call the Matlab function, which would cost too much time for a real-time task.

I don't understand what does “create a command window” mean in this context.

Could you explain it or show a line in matplab_wrapper where a command window is created?

Cheers, Marek

Mvrjustid commented 8 years ago

Sorry for coming late. Actually, my question is simple, that is, can I perform a camera based real-time demo on python while calling a complex function (which could process several frames per second) from matlab? Other words, I am afraid that calling a matlab function is too slow for a real-time demo on python...

Thanks

mrkrd commented 8 years ago

Sorry for coming late.

Actually, my question is simple, that is, can I perform a camera based real-time demo on python while calling a complex function (which could process several frames per second) from matlab?

Other words, I am afraid that calling a matlab function is too slow for a real-time demo on python...

Do I understand the video pathway correctly?

camera → python → matlab_wrapper → matlab

I could work, but it depends on the desired frame rate and the size of the images. The “only” overhead you have from matlab_wrapper is the cost of copying in memory the data from Matlab matrices to Numpy arrays. So if your Matlab video processing function is fast, it could work.

You will have to try it.

Mvrjustid commented 8 years ago

You really help a lot, I am very appreciate that. I should take some time to do these right now, :+1: You're a nice person, thank you!

Mvrjustid commented 8 years ago

Sorry to bother again, but I still got the _'Traceback (most recent call last): File "D:/Desk/PythonLearning/Practice/eval_m_file.py", line 23, in main() File "D:/Desk/PythonLearning/Practice/eval_m_file.py", line 16, in main matlab.eval('my_script(3)') File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 184, in eval RuntimeError: Error from MATLAB MATLAB:UndefinedFunction: Undefined function 'myscript' for input arguments of type 'double'.' problem...even if the my_script.m file and the eval_m_file.py are both at the same path, and the current path of Python and Matlab are also in the same path... Please see to this ...

thanks.

mrkrd commented 8 years ago

Sorry to bother again, but I still got the _'Traceback (most recent call last): File "D:/Desk/PythonLearning/Practice/eval_m_file.py", line 23, in main() File "D:/Desk/PythonLearning/Practice/eval_m_file.py", line 16, in main matlab.eval('my_script(3)') File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 184, in eval RuntimeError: Error from MATLAB MATLAB:UndefinedFunction: Undefined function 'myscript' for input arguments of type 'double'.'

problem...even if the my_script.m file and the eval_m_file.py are both at the same path, and the current path of Python and Matlab are also in the same path... Please see to this ...

The error message means that Matlab is unable to find my_script.m, which has to be in the Matlab's path [1] and is independent from the relative location to eval_m_file.py.

You can either place my_script.m somewhere where Matlab can find it or modify Matlab's path to point out my_script's location, and it should work.

Cheers, Marek

[1] https://mathworks.com/help/matlab/matlab_env/what-is-the-matlab-search-path.html

Mvrjustid commented 8 years ago

Dear Marek, Thank you for assisting me these days : )

But, I met a other wired problem here : _Traceback (most recent call last): File "D:/Desk/PythonLearning/Practice/opt_flow.py", line 82, in flow = matlab.get('imflow') File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 202, in get File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 241, in error_check RuntimeError: MATLAB function <_FuncPtr object at 0x00000000035A5118> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x0000000003515948>) with arguments: (<matlab_wrapper.matlab_session.LPEngine object at 0x0000000003502BC8>, 'imflow')

And the respective code sentence is: matlab.put('im1', prevgray) matlab.put('im2', gray) matlab.eval('demoflow(im1,im2)') flow = matlab.get('imflow')

And the Matlab function I stated as: function imflow = demoflow(im1,im2)

Then, what is happening here...? I really need your help

Thanks !

mrkrd commented 8 years ago

Dear Marek, Thank you for assisting me these days : )

But, I met a other wired problem here : _Traceback (most recent call last): File "D:/Desk/PythonLearning/Practice/opt_flow.py", line 82, in flow = matlab.get('imflow') File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 202, in get File "build\bdist.win-amd64\egg\matlab_wrapper\matlab_session.py", line 241, in error_check RuntimeError: MATLAB function <_FuncPtr object at 0x00000000035A5118> failed (<matlab_wrapper.matlab_session.LP_mxArray object at 0x0000000003515948>) with arguments: (<matlab_wrapper.matlab_session.LPEngine object at 0x0000000003502BC8>, 'imflow')

And the respective code sentence is: matlab.put('im1', prevgray) matlab.put('im2', gray) matlab.eval('demoflow(im1,im2)') flow = matlab.get('imflow')

And the Matlab function I stated as: function imflow = demoflow(im1,im2)

Then, what is happening here...? I really need your help

I guess that imflow cannot be found in the Matlab's global namespace. Where's the output of demoflow going?

Mvrjustid commented 8 years ago

Sorry, what that mean?

mrkrd commented 8 years ago

Sorry, what that mean?

Where's is the output of the function assigned to? To which variable?

Mvrjustid commented 8 years ago

Alright, you may figure it out seeing the main content of the function (I filtered some unnecessary sentences): function imflow = demoflow(im1,im2) addpath('mex'); im1 = imresize(im1,0.5,'bicubic'); im2 = imresize(im2,0.5,'bicubic'); [vx,vy] = Coarse2FineTwoFrames(im1,im2,para); clear flow; flow(:,:,1) = vx; flow(:,:,2) = vy; imflow = flowToColor(flow); So, the problem will from...?

Thanks,

mrkrd commented 8 years ago

Alright, you may figure it out seeing the main content of the function (I filtered some unnecessary sentences): function imflow = demoflow(im1,im2) addpath('mex'); im1 = imresize(im1,0.5,'bicubic'); im2 = imresize(im2,0.5,'bicubic'); [vx,vy] = Coarse2FineTwoFrames(im1,im2,para); clear flow; flow(:,:,1) = vx; flow(:,:,2) = vy; imflow = flowToColor(flow); So, the problem will from...?

I guess that the code above is not relevant for the error.

Generally a function takes some input (im1 and im2 in this case) and returns some output. What is the output of demoflow assigned to in following line?

    matlab.eval('demoflow(im1,im2)')

Does imflow exist in this Matlab namespace?

    flow = matlab.get('imflow')

Please run the above in Matlab first and make sure that there's no error.

Mvrjustid commented 8 years ago

Cheers!!!!!!! I killed it !!!!!! The output variable in matlab always be the name --------> 'ans' !!!!!!!!

After nearly a month's work, I finally successfully run a python-matlab project on my laptop using your bridge! Which also proves that this python-matlab bridge you created really helps, even in some very complicated environment!

Thank you so much, mrkrd ! hope we can keep in touch and maybe have a meet in the future.

mrkrd commented 8 years ago

Congratulations!