Are you looking for a powerful machine learning library? Try OpenCV library for Python. You can run it with Python for Delphi (P4D). P4D is a free and simple with which you can run Python scripts as well as create new Python modules and types in Delphi.
Delphi itself offers various third party libraries for interfacing with OpenCV. If you have an existing Python application and you need OpenCV you could use the Python OpenCV library or you could send the data to Delphi and use Delphi-OpenCV there. Alternately, you could do all of your work with OpenCV in Pythin and then display it in the Delphi Windows GUI app. You can use Python4Delphi a number of different ways such as:
OpenCV is an open-source library for computer vision and machine learning that supports various programming languages including Python. With this library, you can do a lot of difficult operations, such as image processing, video analysis, feature detection, machine learning, computational photography, object detection.
You have read the quick overview of OpenCV library. Go
here, download this library and use the full power of computer vision and machine learning in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.
- Create a Windows GUI around you existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed sensitive Python apps with functions from Delphi for more speed.
1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
K-Nearest Neighbour
In this example, we will consider solving the problem of finding nearest neighbors using OpenCV library. First, let’s randomly create 20 red points (family 0) and 20 green points (family 1). Then add 5 blue points. Using function train(), we will train the neural network. Function findNearest() returns k nearest neighbours (in our example k=3) for each blue point. It also calculates the distance to each found neighbor and determines the family of points from which more neighbors are found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import cv2 import numpy as np import matplotlib.pyplot as plt # Feature set containing (x,y) values of 20 training data trainData = np.random.randint(0,100,(20,2)).astype(np.float32) # Labels each one either Red or Green with numbers 0 and 1 responses = np.random.randint(0,2,(20,1)).astype(np.float32) # Take Red points and plot them red = trainData[responses.ravel()==0] plt.scatter(red[:,0],red[:,1],50,'r','s') # Take Green points and plot them green = trainData[responses.ravel()==1] plt.scatter(green[:,0],green[:,1],50,'g','^') # 5 new points newpoints = np.random.randint(0,100,(5,2)).astype(np.float32) plt.scatter(newpoints[:,0],newpoints[:,1],50,'b','o') knn = cv2.ml.KNearest_create() knn.train(trainData,cv2.ml.ROW_SAMPLE,responses) ret, results,neighbours,dist = knn.findNearest(newpoints, 3) print("result: ", results,"n") print("neighbours: ", neighbours,"n") plt.show() |
Perspective Transformation of an Image
To perform perspective transformation with an image use warpPerspective() function. The parameters of this function are the original image, the transformation matrix, and the size of the output image. Use getPerspectiveTransform() function to get the transformation matrix. You need to pass four points of the input image and the corresponding four points of the output image to this function. It is important, that three of the four points should not be on the same straight line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import cv2 import numpy as np import matplotlib.pyplot as plt image_path = "E:faces.JPEG" img = cv2.imread(image_path) pts1 = np.float32([[900,100],[1200,100],[900,400],[1200,400]]) pts2 = np.float32([[0,0],[400,0],[0,400],[400,400]]) M = cv2.getPerspectiveTransform(pts1,pts2) dst = cv2.warpPerspective(img,M,(400,400)) plt.subplot(121),plt.imshow(img),plt.title('Input') plt.subplot(122),plt.imshow(dst),plt.title('Output') plt.show() |
Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition