Computer Vision News - September 2016
This is the initialization scenario in most cases. Why most and not all? Because when Matlab finds the Python interpreter both in the path environment variable and in the Windows registry, it automatically loads it. In this case, for changing the Python version you will have to change your path environment variable . After successfully loading the Python interpreter, you can call Python commands via the py. Model (e.g. py.list([1.0,2.0,3.0,4.0])). More examples follow below. B. Calling Python Module Now, let’s see how we can call our Python code and model. For this demonstration, we will assume our model is saved in ThePythonCode.py file and it has a testFunc function, see code snippets below: Computer Vision News Trick 25 Dear reader, How do you like Computer Vision News? Did you enjoy reading it? Give us feedback here: It will take you only 2 minutes to fill and it will help us give the computer vision community the great magazine it deserves! Give us feedback, please (click here) FEEDBACK Trick The Python code: The testFunc function get a list1 and define a new list (list2), print the type of those list and return the concatenation of those two list. For concatenating a ‘list’ sent from Matlab and a list define in Python a type conversion is needed. ThePythonCode.py def testFunc(list1): list2 = [1.0,2.0,3.0,4.0] print(type(list1)) print(type(list2)) return list2+list(list2) The Matlab code: We start by adding the local directory (''), empty path, to the Python environment variable. As the location of the ThePythonCode.py script has to be in the Python path even if it is in the current directory. After the path has been set, we call the testFunc Python function located in ThePythonCode.py file. Note, the different types between a ‘pure’ Python list and a ‘list’ sent from Matlab which is array.array. Array objects are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. For more on data type conversion see here . >> if count(py.sys.path,'') == 0 insert(py.sys.path,int32(0),''); end >> my_list = [5.0, 6.0, 7.0, 8.0] >> my_list2 = py.ThePythonCode.testFunc(my_list) <class 'array.array'> <class 'list'> ans = Python list with no properties. [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0] C. Return Values Returns values, much like send values, should get special care according to their type. For example the ‘my_list2’ above is a Python list.
Made with FlippingBook
RkJQdWJsaXNoZXIy NTc3NzU=