LabVIEW-Python Interface A guide to use LabVIEW interface Python Suparat Yeamkuan
Table of Contents 1. LabVIEW-Python Interface 2. LabVIEW and Python Environment 3. Python Functions in LabVIEW 4. Python Programming use with LabVIEW 5. Call Python Functions with LabVIEW 6. Installation Python Package with pip 7. Scientific Computing with Numpy 8. Machine Learning with sklearn 9. Image Processing with OpenCV
LabVIEW-Python Interface Studies on educational applications and academic projects have grown since the release of the Free LabVIEW 2020 Community Version for non-commercial and academic use. Python, on the other hand, is a programming language with a fast-growing user base. The Python Node is a new feature in LabVIEW that allows you to call a Python script with easiness.
LabVIEW and Python Environment Which LabVIEW version? We recommend installing the \"Free LabVIEW 2020 Community Version\" (32- or 64-bit for both), which includes Python node features. Which Python version? We recommend any stable version of python, either Python 2.7, or Python 3.5 or later (32- or 64-bit for both). Installation Notes If you're using LabVIEW 32 bit, please utilize the same OS operation for both LabVIEW and Python. Python, on the other hand, requires a 32-bit operating system.
Python Functions in LabVIEW In LabVIEW, the python function may be found under the Connectivity Python menu, as shown in the figure below. There are three python functions listed in the table below. Palette Object Description Close Python Session Open Python Session Closes a Python session. Python Node Opens a Python session with a specific version of Python. The Python session is necessary for configuring multiple Python Nodes to run in a specific version of Python and in the same process. Calls a Python function directly.
Python Programming use with LabVIEW In terms of the Python - LabVIEW interface, the Python programming site requires Python functions, which are blocks of code that only run when they are called. You can send arguments into a function, and the function can return data as a result. In Python a function is defined using the “def” keyword follow figure below:
Call Python Functions with LabVIEW LabVIEW: 01_Call_Python_Function_with_LabVIEW.vi Python: 01_Max_Min.py def Max_Min(LabVIEW_array): min_value = min(LabVIEW_array) max_value = max(LabVIEW_array) return [min_value, max_value] Result:
Installation Python Package with pip pip is a Python-based package management system for installing and managing applications. It links to the Python Package Index, which is an online repository of public packages. Install Numpy to Python 3.9 in 32-bit system Check Numpy installation complete
Install Matplotlib to Python 3.9 in 32-bit system Check Matplotlib installation complete
Install Sklearn to Python 3.9 in 32-bit system Check Sklearn installation complete
Scientific Computing with Numpy Numpy is a Python package for scientific computing that is widely used. It includes a multidimensional array object, as well as variants such as masks and matrices, that may be used for a variety of arithmetic operations. Many other common Python programs, like as pandas and matplotlib, are compatible with Numpy and utilize it. LabVIEW: 02_Numpy_Histogram_with_LabVIEW.vi Python: 02_Numpy_Histogram.py import numpy as np from matplotlib import pyplot as plt def np_Histogram(LabVIEW_Array): a = np.array(LabVIEW_Array) np.histogram(a,bins = [0,20,40,60,80,100]) hist,bins = np.histogram(a,bins = [0,20,40,60,80,100]) plt.hist(a, bins = [0,20,40,60,80,100]) plt.title(\"histogram\") plt.show() return hist, bins
Result: During running Result: After running
Machine Learning with sklearn Sklearn is a Python machine learning and data modeling package that is open-source. It includes support vector machines, random forests, gradient boosting, k-means, and DBSCAN, among other classification, regression, and clustering methods, and is designed to work with the Python libraries such as NumPy an etc. LabVIEW: 03_Sklearn_Linear_Regression_with_LabVIEW.vi Python: 03_Sklearn_Linear_Regression.py import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, linear_model from sklearn.metrics import mean_squared_error, r2_score def linear_regression(): # Load the diabetes dataset diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True) # Use only one feature diabetes_X = diabetes_X[:, np.newaxis, 2] # Split the data into training/testing sets diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:]
Python: 03_Sklearn_Linear_Regression.py (Cont’d) # Split the targets into training/testing sets diabetes_y_train = diabetes_y[:-20] diabetes_y_test = diabetes_y[-20:] # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(diabetes_X_train, diabetes_y_train) # Make predictions using the testing set diabetes_y_pred = regr.predict(diabetes_X_test) # Plot outputs plt.scatter(diabetes_X_test, diabetes_y_test, color='black') plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth =3) plt.xticks(()) plt.yticks(()) plt.show() # Return values X_test = diabetes_X_test.ravel().tolist() Y_test = diabetes_y_test.ravel().tolist() Y_pred = diabetes_y_pred.ravel().tolist() return X_test, Y_test, Y_pred
Result: During running Result: After running
Image Processing with OpenCV OpenCV is a cross-platform library that allows us to create a computer vision application. It primarily focuses on image processing, video recording, and analysis, which includes characteristics like as face and object identification. LabVIEW: 04_OpenCV_with_LabVIEW.vi Python: 02_Numpy_Histogram.py import numpy as np import cv2 def gray_image(path): image = cv2.imread(path) RGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image_arr = np.array(gray) g = image_arr.ravel().tolist() h, w = image.shape[:2] cv2.imshow('Color image', image) cv2.waitKey(0) cv2.destroyAllWindows() return g, w, h
Result: During running Result: After running
Reference [1] Integrating Python Code in LabVIEW, https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019WHGSA2&l=en-TH [2] Python Functions, https://zone.ni.com/reference/en-XX/help/371361R-01/glang/python_pal/ [3] Can a Python Node to Call a Function That Includes Python Packages? https://knowledge.ni.com/KnowledgeArticleDetails?id=kA03q000000ww92CAA&l=en -TH [4] NumPy Tutorial https://www.tutorialspoint.com/numpy/numpy_histogram_using_matplotlib.htm [5] Linear Regression Example https://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html
Search
Read the Text Version
- 1 - 18
Pages: