Share E-Book

Learning OpenCV 5 Computer Vision with Python Fourth Edition (Joseph Howse Joe Minichino)(Z-Library)

Author

Rating 4.0 AI initial rating

Log in to rate

Artificial Intelligence
Language English

Computer vision is a rapidly evolving science in the field of artificial intelligence encompassing diverse use cases and techniques. This book will not only help those who are getting started with computer vision but also experts in the domain. You'll be able to put theory into practice by building apps with OpenCV 5 and Python 3. You'll start by setting up OpenCV 5 with Python 3 on various platforms. Next you'll learn how to perform basic operations such as reading writing manipulating and displaying images videos and camera feeds. From taking you through image processing video analysis depth estimation and segmentation to helping you gain practice by building a GUI app this book ensures you'll have opportunities for hands-on activities. You'll tackle two popular challenges face detection and face recognition. You'll also learn about object classification and machine learning which will enable you to create and use object detectors and even track moving objects in real time. Later you'll develop your skills in augmented reality and real-world 3D navigation. Finally you'll cover ANNs and DNNs learning how to develop apps for recognizing handwritten digits and classifying a person's gender and age and you'll deploy your solutions to the Cloud. By the end of this book you'll have the skills you need to execute real-world computer vision projects

Format PDF
Size 36.7 MB
5
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
Contents 1. Learning OpenCV 5 Computer Vision with Python Fourth Edition Tackle tools techniques and algorithms for computer vision and machine learning 2. Join our book community on Discord 3. Technical requirements 4. What's new in OpenCV 5 5. Optimizing OpenCV for specific hardware 6. Choosing and using the right setup tools 7. Running the official sample code 8. Finding documentation help and updates 9. Finding this book s sample code 10. Summary 11. Join our book community on Discord 12. Technical requirements 13. Basic I O scripts 14. Project Cameo (face tracking and image manipulation) 15. Cameo an object-oriented design 16. Summary 17. Join our book community on Discord 18. Technical requirements 19. Converting images between different color models 20. Exploring the Fourier transform 21. Creating modules 22. Edge detection 23. Custom kernels getting convoluted 24. Modifying the application 25. Edge detection with Canny 26. Contour detection 27. Detecting lines circles and other shapes 28. Summary 29. Join our book community on Discord 30. Technical requirements 31. Conceptualizing Haar cascades 32. Getting Haar cascade data
Page 3
33. Using OpenCV to perform face detection 34. Performing face recognition 35. Swapping faces in infrared 36. Summary 37. Join our book community on Discord 38. Technical requirements 39. Understanding types of feature detection and matching 40. Detecting Harris corners 41. Detecting DoG features and extracting SIFT descriptors 42. Detecting Fast Hessian features and extracting SURF descriptors 43. Using ORB with FAST features and BRIEF descriptors 44. Filtering matches using K-Nearest Neighbors and the ratio test 45. Matching with FLANN 46. Finding homography with FLANN-based matches 47. A sample application tattoo forensics 48. Summary 49. Join our book community on Discord 50. Technical requirements 51. Understanding HOG descriptors 52. Understanding NMS 53. Understanding SVMs 54. Detecting people with HOG descriptors 55. Creating and training an object detector 56. Detecting cars 57. Summary 58. Join our book community on Discord 59. Technical requirements 60. Detecting moving objects with background subtraction 61. Tracking colorful objects using MeanShift and CamShift 62. Finding trends in motion using the Kalman filter 63. Tracking pedestrians 64. Summary 65. Join our book community on Discord 66. Technical requirements 67. Understanding 3D image tracking and augmented reality 68. Implementing the demo application 69. Improving the 3D tracking algorithm
Page 4
70. Summary 71. Join our book community on Discord 72. Technical requirements 73. Understanding ANNs 74. Training a basic ANN in OpenCV 75. Training an ANN classifier in multiple epochs 76. Recognizing handwritten digits with an ANN 77. Using DNNs from other frameworks in OpenCV 78. Detecting and classifying objects with third-party DNNs 79. Detecting and classifying faces with third-party DNNs 80. Using OpenCV with MediaPipe and Tensorflow to classify gestures and actions 81. Summary 82. Join our book community on Discord 83. What are Containers 84. Docker Basics 85. AWS Serverless (Fargate Lambda) and AWS SAM 86. Conclusion OceanofPDF.com
Page 5
Learning OpenCV 5 Computer Vision with Python, Fourth Edition: Tackle tools, techniques, and algorithms for computer vision and machine learning Welcome to Packt Early Access . We’re giving you an exclusive preview of this book before it goes on sale. It can take many months to write a book, but our authors have cutting-edge information to share with you today. Early Access gives you an insight into the latest developments by making chapter drafts available. The chapters may be a little rough around the edges right now, but our authors will update them over time. You can dip in and out of this book or follow along from start to finish; Early Access is designed to be flexible. We hope you enjoy getting to know more about the process of writing a Packt book. 1. Chapter 1: Setting Up OpenCV 2. Chapter 2: Handling Files, Cameras, and GUIs 3. Chapter 3: Processing Images with OpenCV 4. Chapter 4: Depth Estimation and Segmentation 5. Chapter 5: Detecting and Recognizing Faces 6. Chapter 6: Retrieving Images and Searching Using Image Descriptors 7. Chapter 7: Building Custom Object Detector 8. Chapter 8: Tracking Objects 9. Chapter 9: Camera Models and Augmented Reality 10. Chapter 10: 3D Reconstruction and Navigation 11. Chapter 11: Introduction to Neural Networks with OpenCV 12. Chapter 12: OpenCV Applications at Scale
Page 6
Join our book community on Discord https://discord.gg/djGjeMECxw You've picked up this book, so you may already have an idea of what OpenCV is. Maybe you have heard of capabilities that seem to come straight out of science fiction, such as training an artificial intelligence model to recognize anything that it sees through a camera. If this is your interest, you will not be disappointed! OpenCV stands for Open Source Computer Vision . It is a free computer vision library that allows you to manipulate images and videos to accomplish a variety of tasks, from displaying frames from a webcam to teaching a robot to recognize real-life objects. In this book, you will learn how to leverage the immense potential of OpenCV with the Python programming language. Python is an elegant language with a relatively shallow learning curve and very powerful features. This chapter is a quick guide to setting up Python 3, OpenCV 5, and other dependencies. As part of OpenCV, we will set up the opencv_contrib modules, which offer additional functionality that is maintained by the OpenCV community rather than the core development team. After the setup, we will also look at OpenCV's Python sample scripts and documentation.
Page 7
The following related libraries are covered in this chapter: NumPy : This library is a dependency of OpenCV's Python bindings. It provides numeric computing functionality, including efficient arrays. SciPy : This library is a scientific computing library that is closely related to NumPy. It is not required by OpenCV, but it is useful if you wish to manipulate data in OpenCV images. OpenNI 2 : This library is an optional dependency of OpenCV. It adds support for certain depth cameras, such as the Asus Xtion PRO. For this book's purposes, OpenNI 2 can be considered optional. It is used throughout Chapter 4 , Depth Estimation and Segmentation , but is not used in the other chapters or appendices. Optionally, if we build OpenCV with a custom configuration, we can include support for OpenNI 2, and we can even include OpenCV’s implementations of some patented algorithms, which OpenCV calls non- free . Notably, one of our code samples in Chapter 6 , Retrieving Images and Searching Using Image Descriptors , relies on a patented algorithm called SURF. However, the rest of the book does not use any patented algorithms, so this non-free content can be considered optional, and in any case, patent restrictions may make it unsuitable for redistribution in your future projects. This book focuses on OpenCV 5, the new major release of the OpenCV library. Additional information about OpenCV is available at https://opencv.org/ , and the official documentation is available at https://docs.opencv.org/master . We will cover the following topics in this chapter: What's new in OpenCV 5 Choosing and using the right setup tools Running samples Finding documentation, help, and updates Again, it is worth mentioning that this chapter includes optional instructions on how to build OpenCV from source code, with custom configurations that
Page 8
are relevant to a few projects in this book. However, configuring and customizing OpenCV is a large topic, and the configuration options sometimes change between minor releases (such as OpenCV 5.1 versus 5.0), so advanced users may wish to refer to additional tutorials online. One of the book’s authors, Joseph Howse, maintains a GitHub repository of additional OpenCV build recipes at https://github.com/JoeHowse/OpenCVBuildKit .
Page 9
Technical requirements This chapter assumes that you are using one of the following operating systems: Windows 10 or a later version macOS 10.15 (Catalina) or a later version Debian 10 (Buster) or a later version, or a derivative such as the following: Ubuntu 18.04 (Bionic Beaver) or a later version Linux Mint 19 or a later version Arch Linux, or a derivative such as Manjaro For editing Python scripts and other text files, this book's authors simply recommend that you should have a good text editor. Visual Studio ( VS ) Code is the most suitable editor because of its simplicity and almost all OS distributions support this editor. Other examples include the following: Notepad++ for Windows BBEdit (free version) for macOS GEdit for the GNOME desktop environment on Linux Kate for the KDE Plasma desktop environment on Linux We also recommend that you have a Git client installed since this will be the easiest way to obtain the book’s latest sample code, which is hosted on GitHub. Besides the operating system, there are no other prerequisites for this setup chapter.
Page 10
What's new in OpenCV 5 If you are an OpenCV veteran, you might want to know more about OpenCV 5's changes before you decide to install it. Here are some of the highlights: The C++ implementation of OpenCV has been updated to C++14. OpenCV's Python bindings wrap the C++ implementation, so as Python users, we may gain some performance advantages from this update, even though we are not using C++ directly. Support for Python 2 has been dropped because this Python version is obsolete and no longer maintained. If you have any old Python 2 projects, you should upgrade them to Python 3. For many algorithms, support for named parameters has been added in order to make the initialization code more concise and readable. For the pip package installer, the opencv-python and opencv- contrib-python packages are now maintained by the core OpenCV team rather than a third party. The opencv-contrib-python-nonfree package has been discontinued due to patent restrictions. The SIFT algorithm’s patent has expired and SIFT is now included in standard builds of OpenCV. Many new optimizations have been implemented. Existing OpenCV 4 projects can take advantage of many of these optimizations without further changes beyond updating the OpenCV version. OpenCV's DNN module, in particular, is better optimized. Also, the loading and caching of DNN models are better automated. Support for RISC-V processors has been added. A new module has been added for 3D tracking and navigation. This module includes some new functionality, as well as old functionality that was previously located in opencv_contrib . Likewise, a new module has been added for 2D tracking, including some functionality that was previously located in opencv_contrib . Whether or not you have used a previous version of OpenCV, this book will serve as a general guide to OpenCV 5, and some of the new features will
Page 11
receive special attention in subsequent chapters.
Page 12
Optimizing OpenCV for specific hardware Generally, the setup instructions in this chapter are compatible with any hardware architecture that is supported by OpenCV and the operating system. For example, the setup instructions that target Linux operating systems will work equally well on x64, x86, ARM, or RISC-V hardware (except that the optional OpenNI package is unavailable for RISC-V). That said, OpenCV supports many optional optimizations for specific hardware. By following the instructions in this chapter, you will get an OpenCV installation that uses a default set of optimizations for your hardware. The defaults are generally good, but for some applications, you could get even better performance with a more highly customized installation of OpenCV. Here are a few examples of use cases: For x64, x86, or ARM processors, OpenCV can optionally integrate with Intel Thread Building Blocks (TBB), a multithreading library. OpenCV can use TBB to speed up certain parallel algorithms, notably the Haar cascade classifiers that we will study in Chapter 5 , Detecting and Recognizing Faces . For NVIDIA graphics processors, OpenCV can optionally integrate with CUDA, another multiprocessing library. OpenCV can use CUDA to speed up various deep learning algorithms, as well as any kind of operation on very large blocks of data, such as very large images. For AMD graphics processors, OpenCV can optionally integrate with clBLAS and clFFT. These AMD libraries provide fine-tuned versions of certain OpenCL operations. OpenCL is another multiprocessing framework. Already, by default, OpenCV enables OpenCL optimizations on any compatible hardware, yet clBLAS and clFFT may make these optimizations perform even better on AMD hardware. Specifically, OpenCV can use clBLAS and clFFT to speed up two kinds of operations that are relevant to many signal processing applications: General Matrix Multiply ( GEMM ) and Fast Fourier
Page 13
Transform ( FFT ). For example, GEMM is the fundamental operation in the Kalman filter, which we will use for motion stabilization in Chapter 8 , Tracking Objects , and Chapter 9 , Camera Models and Augmented Reality . For examples of how to enable specific optimizations, see Joseph Howse’s repository of OpenCV build recipes at https://github.com/JoeHowse/OpenCVBuildKit . For now, let’s proceed using default optimizations, which are fine for the purpose of running this book’s sample projects on a wide range of hardware. However, bear in mind that many other optimization options are available to you for your future projects, where you may have specific performance requirements on specific hardware.
Page 14
Choosing and using the right setup tools We are free to choose various setup tools depending on our operating system and how much configuration we want to do. Regardless of the choice of operating system, Python offers some built-in tools that are useful for setting up a development environment. These tools include a package manager called pip and a virtual environment manager called venv . Some of this chapter's instructions will cover pip specifically, but if you would like to learn about venv , please refer to the official Python documentation at https://docs.python.org/3/library/venv.html . You should consider using venv if you plan to maintain a variety of Python projects that might have conflicting dependencies – for example, projects that depend on different versions of OpenCV. Each of venv 's virtual environments has its own set of installed libraries, and we can switch between these environments without reinstalling anything. Within a given virtual environment, libraries can be installed using pip or, in some cases, other tools. Let's take an overview of the setup tools available for Windows, macOS, Ubuntu, Arch Linux, and other Unix-like systems. Installation on Windows Windows does not come with Python preinstalled. However, an installation wizard is available for Python, and Python provides a package manager called pip , which lets us easily install ready-made builds of NumPy, SciPy, and OpenCV. Alternatively, we can build OpenCV from source in order to enable nonstandard features, such as support for depth cameras via OpenNI 2 or support for patented algorithms. OpenCV's build system uses CMake for configuring the system and Visual Studio for compilation.
Page 15
Before anything else, let's install Python. Go to https://www.python.org/getit/ and download and run the most recent installer for Python 3.10. You probably want an installer for 64-bit Python, though OpenCV can work with 32-bit Python too. Once Python has been installed, we can use pip to install NumPy and SciPy. Open the Command Prompt and run the following command: Now, we must decide whether we want a ready-made build of OpenCV (without support for depth cameras) or a custom build (with support for depth cameras). The next two subsections cover these alternatives. Using a ready-made OpenCV package OpenCV, including the opencv_contrib modules, can be installed as a pip package. This is as simple as running the following command: You might find that this pip package offers all the OpenCV features you currently want. On the other hand, if you intend to use depth cameras or patented algorithms, or if you just want to learn about the general process of making a custom build of OpenCV, you should not install the OpenCV pip package; you should proceed to the next subsection instead. Building OpenCV from source If you want support for depth cameras, you should also install OpenNI 2, which is available as a set of precompiled binaries with an installation wizard. Then, we must build OpenCV from source using CMake and Visual Studio. To obtain OpenNI 2, go to https://structure.io/openni and download the latest ZIP for Windows and your system's architecture (x64 or x86). Unzip it to get an installer file, such as OpenNI-Windows-x64-2.2.msi . Run the installer. After the installation is complete, log out and log back in (or, alternatively, reboot) so that changes to the environment variables can take effect.
Page 16
Now, let's set up Visual Studio. To build OpenCV 5, we need Visual Studio 2017 or a later version. If you do not already have a suitable version, go to https://visualstudio.microsoft.com/downloads/ and download and run one of the installers for one of the following: The free Visual Studio 2022 Community edition Any of the paid Visual Studio 2022 editions, which have a 30-day trial period During installation, ensure that any optional C++ components have been selected. After the installation finishes, reboot. For OpenCV 5, the build configuration process requires CMake 3 or a later version. Go to https://cmake.org/download/ , download the installer for the latest version of CMake for your architecture (x64 or x86), and run it. During installation, select either Add CMake to the system PATH for all users or Add CMake to the system PATH for the current user . At this stage, we have set up the dependencies and build environment for our custom build of OpenCV. Now, we need to obtain the OpenCV source code and configure and build it. We can do this by following these steps: 1. Go to https://opencv.org/releases/ and get the latest OpenCV download for Windows. It is a self-extracting ZIP. Run it and, when prompted, enter any destination folder, which we will refer to as <opencv_unzip_destination> . During extraction, a subfolder is created at <opencv_unzip_destination>\opencv . 2. Go to https://github.com/opencv/opencv_contrib/releases and download the latest ZIP of the opencv_contrib modules. Unzip this file to any destination folder, which we will refer to as <opencv_contrib_unzip_destination> . 3. Open the Command Prompt and run the following command to make another folder where our build will go: 1. Change the directory to the build folder: 1. Now, we are ready to configure our build with CMake's command-line interface. To understand all the options, we can read the code in
Page 17
<opencv_unzip_destination>\opencv\CMakeLists.txt . However, for this book's purposes, we only need to use the options that will give us a release build with Python bindings, opencv_contrib modules, non-free content, and depth camera support via OpenNI 2. Some options differ slightly, depending on the Visual Studio version and target architecture (x64 or x86). To create a 64-bit (x64) solution for Visual Studio 2022, run the following command (but replace <opencv_contrib_unzip_destination> and <opencv_unzip_destination> with the actual paths): Alternatively, to create a 32-bit (x86) solution for Visual Studio 2022, run the following command (but replace <opencv_contrib_unzip_destination> and <opencv_unzip_destination> with the actual paths): As the preceding command runs, it prints information about dependencies that are either found or missing. OpenCV has many optional dependencies, so do not panic (yet) about missing dependencies. However, if the build does not finish successfully, try installing the missing dependencies. (Many are available as prebuilt binaries.) Then, repeat this step. 1. CMake will have generated a Visual Studio solution file in <opencv_build_folder>/OpenCV.sln . Open it in Visual Studio. Ensure that the Release configuration (not the Debug configuration) is selected in the drop-down list in the toolbar near the top of the Visual Studio window. (OpenCV's Python bindings will probably fail to build in Debug configuration because most Python distributions do not contain debug libraries.) Go to the BUILD menu and select Build Solution . Watch the build messages in the Output pane at the bottom of the window, and wait for the build to finish. 2. By this stage, OpenCV has been built, but it hasn't been installed at a location where Python can find it. Before proceeding further, let's ensure that our Python environment does not already contain a conflicting build of OpenCV. Find and delete any OpenCV files in Python's DLLs folder and site_packages folder. For example, these files might match the following patterns: C:\Program Files\Python310\DLLs\opencv_*.dll , C:\Program
Page 18
Files\Python310\Lib\site-packages\opencv , and C:\Program Files\Python310\Lib\site-packages\cv2.pyd . 3. Finally, let's install our custom build of OpenCV. For a 64-bit build, copy the following files into Python’s site_packages folder: <build_folder>\lib\python3\Release\*.pyd <build_folder>\bin\Release\*.dll C:\Program Files\OpenNI2\Redist\OpenNI2.dll C:\Program Files\OpenNI2\Redist\OpenNI2 : Copy the whole folder so as to create a subfolder, site_packages\OpenNI2 4. Alternatively, for a 32-bit build, copy the following files into Python’s site_packages folder: <build_folder>\lib\python3\Release\*.pyd <build_folder>\bin\Release\*.dll C:\Program Files (x86)\OpenNI2\Redist\OpenNI2.dll C:\Program Files (x86)\OpenNI2\Redist\OpenNI2 : Copy the whole folder so as to create a subfolder, site_packages\OpenNI2 . Now, we have completed the OpenCV build process on Windows, and we have a custom build that is suitable for all of this book's Python projects. In the future, if you want to update to a new version of the OpenCV source code, repeat all the preceding steps, starting from downloading OpenCV. Installation on macOS macOS comes with a preinstalled Python distribution that has been customized by Apple for the system's internal needs. To develop our own projects, we should make a separate Python installation to ensure that we do not conflict with the system's Python needs. For macOS, there are several possible approaches for obtaining standard versions of Python 3, NumPy, SciPy, and OpenCV. All approaches ultimately require OpenCV to be compiled from source using the Xcode command-line tools. However, depending on the approach, this task is automated for us in various ways by third-party tools. We will look at this
Page 19
kind of approach using a package manager called Homebrew. A package manager can potentially do everything that CMake can, plus it helps us resolve dependencies and separate our development libraries from system libraries. MacPorts is another popular package manager for macOS. However, at the time of writing, MacPorts does not offer packages for OpenCV 5 or OpenNI 2, so we will not use it in this book. Before proceeding, let's make sure that the Xcode command-line tools are set up properly. Open a Terminal and run the following command: Agree to the license agreement and any other prompts. The installation should run to completion. Now, we have the compilers that Homebrew requires. Using Homebrew with ready-made packages Starting on a system where Xcode and its command-line tools are already set up, the following steps will give us an OpenCV installation via Homebrew: 1. Open a Terminal and run the following command to install Homebrew: 1. Homebrew does not automatically put its executables in PATH . To do so, create or edit the ~/.profile file and add the following line at the top of the code: Save the file and run this command to refresh PATH : Note that executables installed by Homebrew now take precedence over executables installed by the system. 1. For Homebrew's self-diagnostic report, run the following command: Follow any troubleshooting advice it gives. 1. Now, update Homebrew:
Page 20
1. Run the following command to install Python 3.10: 1. Now, we want to install OpenCV with the opencv_contrib modules. At the same time, we want to install dependencies such as NumPy. To accomplish this, run the following command: Homebrew does not provide an option to install OpenCV with OpenNI 2 support. Homebrew always installs OpenCV with the opencv_contrib modules, including non-free content such as the patented SURF algorithm, which we will cover in Chapter 6 , Retrieving Images and Searching Using Image Descriptors . If you intend to distribute software that depends on OpenCV's non-free content, you should do your own investigation of how the patent and licensing issues might apply in specific countries and to specific use cases. 1. Similarly, run the following command to install SciPy: Now, we have all we need to develop OpenCV projects with Python on macOS. Using Homebrew with your own custom packages Just in case you ever need to customize a package, Homebrew makes it easy to edit existing package definitions: The package definitions are actually scripts in the Ruby programming language. Tips on editing them can be found on the Homebrew wiki page at https://github.com/Homebrew/brew/blob/master/docs/Formula- Cookbook.md . A script may specify Make or CMake configuration flags, among other things. To see which CMake configuration flags are relevant to OpenCV, refer to https://github.com/opencv/opencv/blob/master/CMakeLists.txt in the official OpenCV repository on GitHub. After making edits to the Ruby script, save it.
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
← Back to List