Learn To Build A Python GUI For Solving Complex Tasks With Powerful OpenCV Library In A Delphi Windows App
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.
Use Delphi and C++Builder and Python4Delphi to run Python scripts in Python GUI apps for Windows. First, run Demo1 project for executing Python script in Python for Delphi. Then load script in text field and press Execute button to see the result. Go to GitHub to download Demo1 source.
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |
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.
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.
# 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()
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.
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()
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() |
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.