Raspberry Pi Computer Vision Programming – Second Edition Programming – Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
Raspberry Pi Computer Vision Pajankar Ashwin Edition Second – Programming Vision Computer Pi Raspberry Programming – Second Edition Raspberry Pi is one of the popular on Raspberry Pi, before covering major Raspberry Pi single-board computers of our generation. techniques and algorithms in image All the major image processing and computer processing, manipulation, and computer vision algorithms and operations can be vision. By sequentially working through the implemented easily with OpenCV on steps in each chapter, you'll understand Computer Vision Raspberry Pi. This updated second edition is essential OpenCV features. Later sections packed with cutting-edge examples and new will take you through creating graphical user topics, and covers the latest versions of key interface (GUI) apps with GPIO and OpenCV. technologies such as Python 3, Raspberry Pi, You'll also learn how to use the new computer Programming and OpenCV. This book will equip you with vision library, Mahotas, to perform various the skills required to successfully design and image processing operations. Finally, you'll implement your own OpenCV, Raspberry Pi, explore the Jupyter notebook and how to and Python-based computer vision projects. set up a Windows computer and Ubuntu for computer vision. Second Edition At the start, you'll learn the basics of Python 3, and the fundamentals of single-board By the end of this book, you'll be able to computers and NumPy. Next, you'll discover confi dently build and deploy computer how to install OpenCV 4 for Python 3 vision apps. Design and implement computer vision applications with Raspberry Pi, OpenCV, and Python 3 Things you will learn: • Set up a Raspberry Pi for computer • Implement low-pass and high-pass vision applications fi lters and understand their applications in image processing • Perform basic image processing with libraries such as NumPy, Matplotlib, • Cover advanced techniques such as and OpenCV histogram equalization and morphological transformations • Demonstrate arithmetical, logical, and other operations on images • Create GUI apps with Python 3 and OpenCV • Work with a USB webcam and the Raspberry Pi Camera Module • Perform machine learning with K-means clustering and image quantization www.packt.com www.packt.com Ashwin Pajankar Raspberry Pi Computer Vision Programming Second Edition Design and implement computer vision applications with Raspberry Pi, OpenCV, and Python 3 Ashwin Pajankar BIRMINGHAM—MUMBAI Raspberry Pi Computer Vision Programming Second Edition Copyright © 2020 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every efort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Sunith Shetty Acquisition Editor: Reshma Raman Senior Editor: Ayaan Hoda Content Development Editor: Nazia Shaikh Technical Editor: Utkarsha S. Kadam Copy Editor: Safs Editing Project Coordinator: Aishwarya Mohan Proofreader: Safs Editing Indexer: Rekha Nair Production Designer: Joshua Misquitta First published: May 2015 Second edition: June 2020 Production reference: 1260620 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80020-721-9 www.packt.com Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals • Improve your learning with Skill Plans built especially for you • Get a free eBook or video every month • Fully searchable for easy access to vital information • Copy and paste, print, and bookmark content Did you know that Packt ofers eBook versions of every book published, with PDF and ePub fles available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and ofers on Packt books and eBooks. Contributors About the author Ashwin Pajankar is a polymath. He is a science popularizer, a programmer, a maker, an author, and a YouTuber. He graduated from IIIT Hyderabad with an MTech in computer science and engineering. He has a keen interest in the promotion of science, technology, engineering, and mathematics (STEM) education. About the reviewers Lentin Joseph is an author, roboticist, and robotics entrepreneur from India. He runs a robotics sofware company called Qbotics Labs in Kochi/Kerala. He has 10 years of experience in the robotics domain, primarily working with the Robot Operating System (ROS), OpenCV, and PCL. He has authored several books on ROS, namely Learning Robotics Using Python First Edition and Second Edition, Mastering ROS for Robotics Programming First Edition and Second Edition, ROS Robotics Projects First Edition and Second Edition, and Robot Operating System for Absolute Beginners. He has masters in Robotics and Automation from Amrita Vishwa Vidapeetham University in India and also worked at the Robotics Institute, CMU, USA. He is also a TEDx speaker. Arush Kakkar is an author, entrepreneur, and a computer vision and deep learning researcher. He is currently the CEO of Agrex.ai, a company started by him. It's a video analytics company utilizing Artifcial Intelligence to analyze human behavior. Te applications include analyzing customer behavior in retail stores, security threats in sensitive installations, thermal vision processing, face recognition, and improving operational efciency. He has authored two books Raspberry Pi By Example and Raspberry Pi: Amazing Products from Scratch both published by Packt Publishing. Arush has extensive experience in using Raspberry Pi to build products that use computer vision to accomplish certain tasks. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors. packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specifc hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Introduction to Computer Vision and the Raspberry Pi Understanding computer vision 2 Setting up Raspbian on a OpenCV 3 Raspberry Pi 14 Downloading the necessary software 20 Single-board computers 4 Preparing the microSD card manually 23 The Beagleboard family 6 Booting up the Raspberry Pi for the ASUS Tinkerboard 6 frst time 26 NVIDIA Jetson 7 Connecting various RPi board models Intel boards 7 to the internet 34 Raspberry Pi 8 Updating the RPi 37 Raspberry Pi models 8 Summary 38 OSes for Raspberry Pi 13 2 Preparing the Raspberry Pi for Computer Vision Remotely logging into the RPi Installing OpenCV on an RPi with SSH 40 board 46 Remote desktop access 44 Heatsinks and overclocking RPi 4B 47 Summary 49 ii Table of Contents 3 Introduction to Python Programming Technical requirements 52 The basics of NumPy 62 Understanding Python 3 52 Matplotlib 65 Python on RPi and Raspberry Pi OS 53 RPi GPIO programming with Python 3 IDEs on Raspberry Pi OS 54 Python 3 71 Working with Python 3 in interactive LED programming with GPIO 72 mode 59 Push-button programming with GPIO 80 The basics of Python 3 programming 59 Summary 83 The SciPy ecosystem 62 4 Getting Started with Computer Vision Technical requirements 86 Webcam video recording 103 Exploring image datasets 86 Capturing images with the webcam using Python and OpenCV 103 Working with images using Live videos with the webcam using OpenCV 86 Python and OpenCV 104 Using matplotlib to visualize Webcam resolution 105 images 89 FPS of the webcam 106 Drawing geometric shapes with Saving webcam videos 107 OpenCV and NumPy 92 Playing back the video with OpenCV 109 Working with a GUI 95 The Pi camera module 110 Event handling and a primitive Capturing images and videos with the paint application 96 raspistill and raspivid utilities 112 Working with a USB webcam 99 Using picamera with Python 3 113 Capturing images with the webcam 100 Using the RPi camera module and Timelapse photography 100 Python 3 to record videos 116 Summary 116 Table of Contents iii 5 Basics of Image Processing Technical requirements 118 Blending and transitioning Retrieving image properties 118 images 126 Basic operations on images 120 Multiplying images by a Splitting the image into channels 121 constant and one another 130 Adding a