Computer Vision News - September 2016
Every month, Computer Vision News shows you a nice trick that will make your work easier. This time, our engineers will tell you how to call Python code from Matlab . Both Matlab and Python have efficient and powerful tool-boxes for computer vision and it is not rare to have a scenario where you have a computer vision project in Matlab but you need to incorporate already written Python code in it. Since version 2014b, Matlab comes with built-in support for calling Python code. However, there are some bits and bytes that need to be tuned in order to get it to work correctly. In addition, some of the settings change from one Python version to another. This month's trick comes to aid you in this process. After all, this is what our trick of the month is for, isn’t it? A. Initialization The initialization consists of two steps: setting and loading the Python interpreter. Setting is done by the pyversion command and loading is done automatically when you type the first py.command. The Matlab’s pyversion command is a bit tricky as it has two uses: both for setting and for getting the version of the Python interpreter. In addition, once the Python interpreter is loaded you can’t change it in the current session. Only restarting the Matlab will allow you to load a different version of Python, for example, in case you want to switch between version 2.x and 3.x. Let's see the pyversion command in action: Details The code 24 Computer Vision News Trick Call Python code fromMatlab Trick pyversion argument telling Matlab which Python version to use. First time we run the pyversion command: The ‘isloaded’ field is 0 indicating that Python is not loaded yet. >> pyversion 'C:/Anaconda3/python.exe' >> pyversion version: '3.5' executable: 'C:\Anaconda3\python.exe' library: 'C:\Anaconda3\python35.dll' home: 'C:\Anaconda3' isloaded: 0 Python will be loaded only after you execute the first Python command. For example after running this command which define a Python list. >> myList = py.list([1.0,2.0,3.0,4.0]) Calling the pyversion again, after running a Python command, Now the is ‘isloaded’ field is ‘1’ >> pyversion version: '3.5' executable: 'C:\Anaconda3\python.exe' library: 'C:\Anaconda3\python35.dll' home: 'C:\Anaconda3' isloaded: 1 As mentioned, once Python was loaded, it cannot be changed. Only restarting Matlab would allow you to do so. Error using pyversion Python is loaded. The version cannot be changed.
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=