![pexelskatjayne551600 | Embarcadero RAD Studio Delphi C++Builder Blogs Computer vision](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/pexels-kat-jayne-551600-5202916-scaled.jpg?resize=1140%2C694&ssl=1)
In this article, you’ll learn why you should use Python for computer vision, how to use the Python Windows GUI Builder for GUI features and functionalities, how to use Python libraries to perform Computer Vision Tasks, the results, and much more.
Table of Contents
What is Computer Vision?
According to Ballard and Brown in 1982, Computer Vision is an interdisciplinary scientific field that deals with how computers can gain high-level understanding from digital images or videos. From the perspective of engineering, it seeks to understand and automate tasks that the human visual system can do.
At 19th CERN School of Computing, T. S. Huang states that Computer Vision has a dual goal. From the biological science point of view, Computer Vision aims to come up with computational models of the human visual system. From the engineering point of view, computer vision aims to build autonomous systems which could perform some of the tasks which the human visual system can perform (and even surpass it in many cases).
These two goals are intimately related. The properties and characteristics of the human visual system often give inspiration to engineers who are designing Computer Vision systems. Conversely, Computer Vision algorithms can offer insights into how the human visual system works.
Sub-domains of Computer Vision include scene reconstruction, object detection, event detection, video tracking, object recognition, 3D pose estimation, learning, indexing, motion estimation, visual servoing, 3D scene modeling, and image restoration (Morris, 2004).
Why use Python for Computer Vision?
- Easy to use
- Open source
- Python has become the language of scientific computing
- Easy for visualization and debugging
- It can be directly integrated with web frameworks (as well as GUIs)
Delphi adds Powerful GUI Features and Functionalities to Python
In this tutorial, we’ll build Windows Apps with extensive Computer Vision capabilities by integrating Python’s Computer Vision libraries with Embarcadero’s Delphi, using Python4Delphi (P4D).
A small disclaimer: we have used some publicly-available images for the fair-use purposes of education so we can teach you how face recognition works. The copyright of the images remains with the owner and we acknowledge the source and their ownership.
P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with Windows 10 looks and responsive controls for our Python Computer Vision applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.
We’re going to cover the following…
How to use OpenCV, Mahotas, Face Recognition, EasyOCR, and Keras Python libraries to perform Computer Vision tasks
All of them would be integrated with Python4Delphi to create Windows Apps with Computer Vision capabilities.
Prerequisites
Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.
Time to get Started!
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
![python4delphi run demo01 | Embarcadero RAD Studio Delphi C++Builder Blogs 5 Ways To Use Computer Vision In Your Windows Apps demo of code](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/04/0_runDemo1-1610538.png?resize=750%2C401&ssl=1)
How do I perform Computer Vision with OpenCV on Windows?
OpenCV (Open Source Computer Vision Library) is an open-source Computer Vision and Machine Learning software library. OpenCV was built to provide a common infrastructure for Computer Vision applications and to accelerate the use of machine perception in commercial products. OpenCV supports various programming languages including Python.
OpenCV has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art Computer Vision and Machine Learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high-resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc.
First, here is how you can get OpenCV to work with Python4Delphi to create GUI with Computer Vision and Machine Learning capabilities:
1 |
pip install opencv-python |
Note: This is an unofficial pre-built CPU-only OpenCV package for Python.
Don’t forget to put the path where your OpenCV installed, to the System Environment Variables:
System Environment Variable Examples
1 2 3 |
C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Lib/site-packages C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38/Scripts C:/Users/YOUR_USERNAME/AppData/Local/Programs/Python/Python38 |
The following is a code example of OpenCV to perform perspective transformation of an image (run this inside the lower Memo of Python4Delphi Demo01 GUI):
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 = cv2.imread("C:/Users/YOUR_USERNAME/got.jpg") pts1 = np.float32([[535,145],[625,145],[535,250],[625,250]]) pts2 = np.float32([[0,0],[400,0],[0,400],[400,400]]) M = cv2.getPerspectiveTransform(pts1,pts2) dst = cv2.warpPerspective(image,M,(400,400)) plt.subplot(121),plt.imshow(image),plt.title('Input') plt.subplot(122),plt.imshow(dst),plt.title('Output') plt.show() |
Here is the result in Python GUI
![OpenCV Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs OpenCV Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/1openCV-2891354.png?resize=750%2C346&ssl=1)
How do I perform Computer Vision with Mahotas on Windows?
Mahotas is a fast computer vision algorithms library (all implemented in C++ for speed) that operates over NumPy arrays. Mahotas supports Python 2.7 and 3.4+.
Currently, Mahotas has over 100 functions for image processing and computer vision and it keeps growing.
Here are some notable algorithms provided by Mahotas:
- Watershed
- Convex points calculations.
- Hit & miss, thinning.
- Zernike & Haralick, LBP, and TAS features.
- Speeded-Up Robust Features (SURF), a form of local features.
- Thresholding.
- Convolution.
- Sobel edge detection.
- Spline interpolation
- SLIC superpixels.
Are you looking for a powerful Computer Vision library and build a nice GUI for them? This section will show you how to get started!
First, here is how you can get Mahotas:
1 |
pip install mahotas |
The following is a code example of Mahotas to use the Ridler-Calvard threshold to transform an image (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Import using “mh” abbreviation which is common: import mahotas as mh from pylab import imshow, show # Load and show one of the demo images im = mh.demos.load('nuclear') imshow(im) show() # Automatically compute a threshold T_otsu = mh.thresholding.otsu(im) print(T_otsu) # Label the thresholded image (thresholding is done with numpy operations seeds,nr_regions = mh.label(im > T_otsu) print(seeds,nr_regions) # Call seeded watershed to expand the threshold labeled = mh.cwatershed(im.max() - im, seeds) print(labeled) |
Here is the Mahotas result in the Python GUI:
![Mahotas Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Mahotas Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/2mahotas-7319049.png?resize=750%2C438&ssl=1)
How do I perform Computer Vision with Face Recognition on Windows?
Face Recognition library-known as the world’s simplest face recognition library, has the capabilities to recognize and manipulate faces using Python or the command line.
Face Recognition is built using dlib’s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the Labeled Faces in the Wild benchmark.
This library also provides a simple face_recognition command-line tool that lets us do face recognition on a folder of images from the command line.
This section will guide you to combine Python4Delphi with the Face Recognition library, inside Delphi and C++Builder, from installing Face Recognition with pip until using it to recognize all faces in any given image!
First, here is how you can get Face Recognition:
1 |
pip install face-recognition |
Some of you might encounter some error when installing Face Recognition, caused by dlib (one of the Face Recognition requirements). Please refer to this link for the solutions.
Next, we will test the Face Recognition library to detect faces in this image:
![3faceRecognition0 | Embarcadero RAD Studio Delphi C++Builder Blogs 5 Ways To Use Computer Vision In Your Windows Apps recognizable faces](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/3faceRecognition0-5156092.jpg?resize=750%2C379&ssl=1)
Use the following code to recognize faces from any image, using Histogram of Oriented Gradients (HOG) based model (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from PIL import Image import face_recognition # Load the jpg file into a numpy array image = face_recognition.load_image_file("C:/Users/ASUS/people.jpg") # Find all the faces in the image using the default HOG-based model. # This method is fairly accurate, but not as accurate as the CNN model and not GPU accelerated. # See also: find_faces_in_picture_cnn.py face_locations = face_recognition.face_locations(image) print("I found {} face(s) in this photograph.".format(len(face_locations))) for face_location in face_locations: # Print the location of each face in this image top, right, bottom, left = face_location print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # You can access the actual face itself like this: face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) pil_image.show() |
Face Recognition Python4Delphi Results
![Face Recognition Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Face Recognition Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/3faceRecognition1-6329461-1024x551-8097279.jpg?resize=750%2C404&ssl=1)
![Face Recognition Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Face Recognition Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/3faceRecognition2-6500481.png?resize=750%2C404&ssl=1)
![Face Recognition Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Face Recognition Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/3faceRecognition3-4295053.png?resize=750%2C403&ssl=1)
![Face Recognition Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Face Recognition Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/3faceRecognition4-7123408.png?resize=750%2C405&ssl=1)
How do I perform Computer Vision with EasyOCR on Windows?
EasyOCR, as the name suggests, is a Python package that allows computer vision developers to effortlessly perform Optical Character Recognition. EasyOCR provides end-to-end, and ready-to-use OCR with 80+ supported languages and all popular writing scripts including Latin, Chinese, Arabic, Devanagari, Cyrillic, etc.
When it comes to OCR, using EasyOCR is by far the most straightforward way to apply Optical Character Recognition:
- The EasyOCR package can be installed with single pip command.
- The dependencies on the EasyOCR package are minimal, making it easy to configure your OCR development environment.
- Once EasyOCR is installed, only one import statement is required to import the package into your project.
- From there, all you need is two lines of code to perform OCR — one to initialize the Reader class and then another to OCR the image via the readtext function.
First, here is how you can get EasyOCR:
1 |
pip install -U scikit-learn |
Next, we will test the EasyOCR library to detect both Chinese and English characters in this image:
![4easyOCR0 | Embarcadero RAD Studio Delphi C++Builder Blogs 4easyocr0](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/4easyOCR0-4417710.jpg?resize=750%2C423&ssl=1)
The following is a basic usage of EasyOCR to detect both Chinese and English characters in the sample image above (run this inside the lower Memo of Python4Delphi Demo01 GUI):
1 2 3 4 5 6 7 |
import os os.system('cmd /k "chcp 936"') import easyocr reader = easyocr.Reader(['ch_sim','en']) result = reader.readtext('C:/Users/YOUR_USERNAME/chinese2.jpg') print(result) |
EasyOCR Optical Character Recognition Result
![EasyOCR Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs EasyOCR Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/4easyOCR1-9110941.png?resize=708%2C821&ssl=1)
Amazing isn’t it? You can make your computer recognize Chinese and English characters, as well as other 80+ supported languages.
How do I perform Computer Vision with Keras on Windows?
Keras is a high-level neural networks API for Python. Keras acts as an interface for the TensorFlow library. As a central part of the tightly connected TensorFlow 2.0 ecosystem, Keras is covering every step of the Machine Learning workflow, from data management to hyperparameter training to deployment solutions.
Do you want to use Keras to solve Computer Vision problems, and build a nice GUI for it? This section will show you a demo of the Keras image segmentation model trained from scratch on the Oxford Pets dataset.
First, here is how you can get Keras:
1 |
pip install keras |
Download the dataset, by running the following commands on your Windows cmd:
1 2 3 4 |
!curl -O https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz !curl -O https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz !tar -xf images.tar.gz !tar -xf annotations.tar.gz |
![5keras1downloadData1 | Embarcadero RAD Studio Delphi C++Builder Blogs 5 Ways To Use Computer Vision In Your Windows Apps command prompt](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/5keras1_downloadData1-3739603.png?resize=685%2C124&ssl=1)
![5keras2downloadData2 | Embarcadero RAD Studio Delphi C++Builder Blogs 5 Ways To Use Computer Vision In Your Windows Apps command prompt 2](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/5keras2_downloadData2-5903547.png?resize=685%2C125&ssl=1)
![5keras3downloadData3 | Embarcadero RAD Studio Delphi C++Builder Blogs 5keras3 downloaddata3](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/5keras3_downloadData3-5291799.png?resize=684%2C93&ssl=1)
The following is a code example of Keras to perform image segmentation with a U-Net-like architecture (run this inside the lower Memo of Python4Delphi Demo01 GUI). The code used in this section is authored by François Chollet.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# Prepare paths of input images and target segmentation masks import os input_dir = "C:/Users/ASUS/images/" target_dir = "C:/Users/ASUS/annotations/trimaps/" img_size = (160, 160) num_classes = 3 batch_size = 32 input_img_paths = sorted( [ os.path.join(input_dir, fname) for fname in os.listdir(input_dir) if fname.endswith(".jpg") ] ) target_img_paths = sorted( [ os.path.join(target_dir, fname) for fname in os.listdir(target_dir) if fname.endswith(".png") and not fname.startswith(".") ] ) print("Number of samples:", len(input_img_paths)) for input_path, target_path in zip(input_img_paths[:10], target_img_paths[:10]): print(input_path, "|", target_path) # What does one input image and corresponding segmentation mask look like? import matplotlib.pyplot as plt from IPython.display import Image, display from tensorflow.keras.preprocessing.image import load_img import PIL from PIL import ImageOps import glob import matplotlib.image as mpimg # Display input image #7 #display(Image(filename=input_img_paths[9])) gbr = mpimg.imread(input_img_paths[9]) imgplot = plt.imshow(gbr) plt.show() # Display auto-contrast version of corresponding target (per-pixel categories) img = PIL.ImageOps.autocontrast(load_img(target_img_paths[9])) plt.imshow(img) plt.show() |
The Input Image vs its Segmentation Mask Result in the Python4Delphi GUI
![Keras Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs 5 Ways To Use Computer Vision In Your Windows Apps Keras Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/5keras4-9315108.png?resize=750%2C363&ssl=1)
![Keras Demo with Python4Delphi in Windows | Embarcadero RAD Studio Delphi C++Builder Blogs Keras Demo with Python4Delphi in Windows](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2021/06/5keras5-9444154.png?resize=750%2C364&ssl=1)
Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition