39 Experiments with Raspberry Pi and Arduino
Camera Projects Book
Dogan Ibrahim LEARN DESIGN SHARE
IGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHAR SIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN
Camera Projects Book 39 Experiments with Raspberry Pi and Arduino
â—? Dogan Ibrahim
LEARN DESIGN SHARE
●
This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V. 78 York Street, London W1H 1DP, UK Phone: (+44) (0)20 7692 8344
●
All rights reserved. No part of this book may be reproduced in any material form, including
photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally to some other sue of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to reproduce any part of the publication should be addressed to the publishers.
●
Declaration
The author and publisher have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, or hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause.
●
Acknowledgements
The authors would like to express their thanks to Espressif Systems for giving permission to include ESP-EYE based pictures and data in this book. The authors also acknowledge the help and valuable suggestions given by Mr. Volker Bombien throughout the duration of the preparation of this book. The author also would like to thank to Dr Adrian Rosebrock of PyImageSearch team for giving permission to use the OpenCV installation instructions for the Raspberry Pi in this book.
●
British Library Cataloguing in Publication Data
●
ISBN 978-1-907920-77-6
A catalogue record for this book is available from the British Library
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
LEARN DESIGN SHARE
Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 1 • P ixels, Images, Image Files, and Camera Terminology . . . . . . . . . . . . 12 1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.2 Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.3 Pixel Colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.4 Image Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4.1 JPEG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.4.2 TIFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.4.3 GIF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4.4 BMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.4.5 PNG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5 Camera Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Chapter 2 • Raspberry Pi and Arduino Compatible Cameras . . . . . . . . . . . . . . . . . 19 2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 Raspberry Pi Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2.1 Standard Raspberry Pi Daytime Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2.2 Standard Raspberry Pi Infrared Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.2.3 Unistorm Night Vision Raspberry Pi Camera . . . . . . . . . . . . . . . . . . . . . . . . . . 22 2.2.4 Raspberry Pi Camera Accessories for CSI Bus Cameras . . . . . . . . . . . . . . . . . . 22 2.2.5 Webcam Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3 Arduino Uno Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3.1 ArduCAM OV2640 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3.2 ArduCAM OV5642 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.3.3 Arduino JPEG Camera Shield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.4 ArduCAM MT9D111 Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2.3.5 TTL Serial Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Chapter 3 • Raspberry Pi Camera Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.2 Installing the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.3 Project 1 - Still Camera Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
●5
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino 3.3.1 raspistill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.4 Project 2 – Building a Timelapse Camera – Who is in My Parking Place? . . . . . . . . 36 3.5 Project 3 - Video Camera Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 3.6 Using USB Camera With Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.7 Project 4 – Who is Ringing My Doorbell? – Bluetooth Solution . . . . . . . . . . . . . . . 51 3.8 Project 5 – Video Live Streaming on YouTube . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 3.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Chapter 4 • Simple Raspberry Pi Camera Projects using Python . . . . . . . . . . . . . . 61 4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.2 Python picamera Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.3 Using the Camera in Python Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.4 Project 1 – Capturing Multiple Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.5 Camera Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.5.1 Adding Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.5.2 Changing the Brightness and Contrast of Images . . . . . . . . . . . . . . . . . . . . . . . 64 4.5.3 Image Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.5.4 Exposure Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.5.5 Other Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.6 Taking Consecutive Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 4.7 Taking Images Continuously . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4.8 Project 2 – Timelapse Image Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.9 Project 3 – Capturing Pictures using a Button – Taking Selfie Pictures . . . . . . . . . . 68 4.10 Project 4 – Taking Selfie Pictures: Using a Buzzer . . . . . . . . . . . . . . . . . . . . . . . 72 4.11 Project 5 – Taking Selfie Pictures: Timed Photography with Buzzer . . . . . . . . . . . 75 4.12 Project 6 – Taking Selfie Pictures: Using a Display . . . . . . . . . . . . . . . . . . . . . . 77 4.13 Project 7 – Taking Selfie Pictures: Adding a Soft Keyboard to Desktop . . . . . . . . 84 4.14 Project 8 – Taking Selfie Pictures: Using Soft keyboard – Taking Multiple Pictures . 86 4.15 Project 9 – Designing a Photobox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.16 Project 10 – Activating the Photobox from the Desktop . . . . . . . . . . . . . . . . . . . 90 4.17 Project 11 – Capturing Video: Using a Button to Start/Stop Recording . . . . . . . . 93 4.18 Project 12 – Designing a Digital Photo Frame . . . . . . . . . . . . . . . . . . . . . . . . . . 95 4.19 Project 13 – Sending Email of a Captured Image . . . . . . . . . . . . . . . . . . . . . . . 96
●6
4.20 Project 14 – Who is Ringing my Doorbell? E-mail Solution . . . . . . . . . . . . . . . . . 99 4.21 Project 15 – Recording Video of Wildlife using PIR Sensor . . . . . . . . . . . . . . . . 102 4.22 Project 16 – Posting a Picture to Twitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 4.23 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 5 • Making a Motion Activation Spy Camera . . . . . . . . . . . . . . . . . . . . . . 114 5.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 5.2 Detection of Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 5.3 Project 1 – Motion Activated Spy Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 5.4 Project 2 – Motion Activated Spy Camera with LED . . . . . . . . . . . . . . . . . . . . . . 118 5.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Chapter 6 • Raspberry Pi Camera Flash Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.2 Project 1 - Unsynchronized Flash Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.3 Project 2 - Synchronized Flash Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Chapter 7 • Designing a Raspberry Pi Surveillance Camera System . . . . . . . . . . 128 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 7.2.1 The Motion Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 7.2.2 Configuring Motion Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 7.2.3 Starting the Motion Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 7.2.4 Customizing the Motion Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 7.2.5 Sending E-mail when a Motion is Detected . . . . . . . . . . . . . . . . . . . . . . . . . . 132 7.2.6 General Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 7.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Chapter 8 • The motionEyeOS Surveillance Software . . . . . . . . . . . . . . . . . . . . . . 135 8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 8.2 Installing the motionEyeOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 8.3 Initial Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.4 Advanced Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 8.5 Configuring for Motion Detection and Automatic Sending Email . . . . . . . . . . . . . 140 8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Chapter 9 • The OpenCV Software Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
●7
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino 9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.2 Copying the Raspberry Pi microSD Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 9.3 Install the OpenCV Software Package on Your Raspberry Pi . . . . . . . . . . . . . . . . 145 9.4 Using the OpenCV Software Package on Raspberry Pi . . . . . . . . . . . . . . . . . . . . 149 9.4.1 Connection using the 7 inch Raspberry Pi Display . . . . . . . . . . . . . . . . . . . . . 149 9.4.2 Connection using the Putty and vncserver . . . . . . . . . . . . . . . . . . . . . . . . . . 149 9.4.3 Connection using the Putty and Xming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 9.5 Basic OpenCV Image Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 9.5.1 imread(), imshow(), waitKey(), destroyAllWindows(), imwrite() . . . . . . . . . . . 153 9.5.2 Drawing Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 9.5.3 Image Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 9.6 Edge Detection – Canny Edge Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 9.7 Circle Detection – Hough Circle Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 9.8 Line Detection – Hough Line Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . 167 9.9 Arithmetic Operations on Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 9.9.1 Adding Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 9.9.2 Subtracting Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 9.10 Morphological Operations on Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 9.11 Simple Geometric Shape Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 9.12 Blurring/smoothing an Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 9.13 Detecting Colour in Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 9.14 Project 1 – Face and Eye Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 9.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Chapter 10 • Capturing Images from the Camera using OpenCV . . . . . . . . . . . . . 183 10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 10.2 Capturing Images From a Raspberry Pi Camera using OpenCV . . . . . . . . . . . . . 183 10.2.1 Project 1 – Capturing Several Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 10.2.2 Project 2 – Timelapse Image Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 10.2.3 Project 3 – Timelapse Image Capture at Specified Times . . . . . . . . . . . . . . . 187 10.3 Video Recording with a Raspberry Pi Camera using OpenCV . . . . . . . . . . . . . . . 188 10.4 Using USB Camera with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 10.4.1 Project 4 – Capturing Multiple Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
●8
10.4.2 Capturing Video Frames with OpenCV using USB Camera . . . . . . . . . . . . . . . 191 10.4.3 Project 5 – Recording Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 10.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 CHAPTER 11 • Using the SimpleCV in Camera Projects . . . . . . . . . . . . . . . . . . . . 195 11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 11.2 Barcodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 11.2.1 Code 39 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 11.2.2 Code 128 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 11.2.3 Universal Product Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 11.3 Project 1 – Barcode Password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 11.4 Project 2 – Motion Detection with Buzzer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 11.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 Chapter 12 • Using Camera with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 12.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 12.3 Project 2 – Arduino Uno SD Card interface: Reading/Writing to the Card . . . . . . 208 12.4 The Arducam Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 12.4.1 Arducam Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 12.4.2 Multi Camera Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 12.4.3 Project 3 – Capturing Image with the Arducam and Arduino . . . . . . . . . . . . . 215 12.4.4 Project 4 – Timelapse Photography with the Arduino . . . . . . . . . . . . . . . . . . 226 12.4.5 Project 5 – Image Capture with an External Shutter Button . . . . . . . . . . . . . 231 12.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Chapter 13 • Automatic Number Plate Recognition . . . . . . . . . . . . . . . . . . . . . . . 238 13.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 13.2 Vehicle Number Plates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 13.3 The OpenALPR Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 13.4 Using With the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 13.4.1 Project 1 - Recognizing Number Plate from a File . . . . . . . . . . . . . . . . . . . . . 238 13.4.2 Project 2 - Recognizing from the Camera . . . . . . . . . . . . . . . . . . . . . . . . . . 240 13.5 Using OpenALPR with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 13.5.1 Project 3 – Recognizing Number Plates with Python . . . . . . . . . . . . . . . . . . . 242 13.5.2 Project 4 – Daddy is Home, Put the Kettle ON! . . . . . . . . . . . . . . . . . . . . . . 244
●9
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino 13.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Appendix A • Raspberry Pi 3 Pin Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Appendix B • The ESP-EYE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 B.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 B.2 Face Detection and Recognition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 B.3 Adding a New Face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 B.4 Delete an Existing Face . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
● 10
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Chapter 1 • P ixels, Images, Image Files, and Camera Terminology 1.1 Overview Before going into the details of cameras and their uses in Raspberry Pi and Arduino projects, it is worthwhile to look at the general image concepts briefly. In this Chapter we shall be learning about the pixels, images, image files, and colours. 1.2 Pixels We can lay an image on a grid with squares. Pixels are then the basic building blocks of the image where a pixel occupies one square of the grid. This grid is also called the bitmap. An image with 1024x768 pixels is a grid with 1024 columns and 768 rows, and therefore it contains 1024 x 768 = 786,432 pixels. It is important to realize that knowing the pixel count in an image does not indicate the physical dimensions of that image. This is because we have to know the size of a pixel before we can find out the physical size of the overall image. The physical size of an image can be estimated from knowledge of the Pixels-PerInch (PPI), that is the number of pixels in one inch of the image. If for example the PPI is 100 and the image has 1024 x 768 pixels, then the physical size of the image is 10.24 inches x 7.68 inches. The number of pixels making up an image is also known as the resolution of that image. The resolutions of large images are usually denoted in megapixels. In printing technology, the resolution is denoted by Dots-Per-Inch (DPI) which shows the number of dots in one inch of the hard copy printed output. i.e. it is the physical dot density of an image when it is reproduced on paper. Different printers achieve different resolutions. For example the resolution of an inkjet printer is 300-700 DPI. The resolution of laser printers is around 600-2500 DPI. The DPI value of a printer should be considerably higher than the PPI value of an image in order to produce similar quality output. An image becomes clearer as the PPI count (or the resolution) increases. Figure 1.1 shows an image with different resolutions. In this example, although the image is very poor at a resolution of 10x10, it is much clearer at a resolution of 100x100.
Figure 1.1 An image at different resolutions (www.wikipedia.org/wiki/Image_resolution) 1.3 Pixel Colours The colour represented by a pixel depends on the number of bits used for the colour information. Monochrome images are made up of black and white images and they use only two bits. With 8 bits we can represent up to 256 colours, with 16 bits 65,536 colours (Highcolor), and with 24 bits we can represent 16,777,216 colours (Truecolor). Colour of a pixel is represented using a combination of the Red, Green, and Blue (RGB) colours.
â—? 12
Chapter 1 • Pixels, Images, Image Files, and Camera Terminology
In a 24 bit system, 8 bits are used to represent Red, 8 bits to represent Green, and 8 bits to represent Blue. The colour values can take values from 0 to 255. Some example colour combinations are given below: Red Green Blue White colour: 255 255 255 Black colour: 0 0 0 Red colour: 255 0 0 Yellow colour: 255 255 0 Pink colour: 255 0 255 In colour printing, subtractive colours Cyan, Magenta, and Yellow (CMY) are used. For example, mixing Cyan and Yellow gives the Green colour, mixing Magenta and Yellow gives the Red, mixing all three colours give the Black colour and so on. 1.4 Image Files There are many image files in use today. In this section we shall be looking at some of the commonly used and known image files properties. Coloured image files are very large files. For example, a 640 x 480 pixel image file with 24 bit colour information takes 640x480x24 = 7,372,800 bits, or 921,600 bytes, which is almost one megabyte. Storing such large files can take too much storage space. Also, processing or sending large files through the Internet can take long time. Because the sizes of the coloured image files are very large, these files are usually compressed to save storage space. In general there are two types of compression algorithms in use today: Lossless compression, and Lossy compression. Lossless compression preserve a perfect copy of the image and attempt to reduce the file size. Lossy compression creates much smaller file sizes but the image is not a perfect copy of the original. The features of some of the commonly used image files are given below. 1.4.1 JPEG JPEG (Joint Photographic Experts Group) is one of the most commonly used image file formats which is based on lossy compression. The filename extensions of these files are .JPG or .JPEG. Most cameras and other digital imaging systems support the JPEG format which produces 8 bit grayscale or 24 bit colour images. Although JPEG reduces the file size, the quality of the image is reduced because of lossy compression. The degree of compression and hence the quality of the produced image can be changed by many application programs. Figure 1.2 shows an image in JPEG format where the file size of this image is 32KB.
â—? 13
Chapter 2 • Raspberry Pi and Arduino Compatible Cameras
Chapter 2 • Raspberry Pi and Arduino Compatible Cameras 2.1 Overview In this Chapter we shall be looking at the basic features of various Raspberry Pi and Arduino Uno compatible cameras. 2.2 Raspberry Pi Cameras Raspberry Pi computers accept CSI (Camera Serial Interface) bus compatible cameras. CSI is capable of very high data rates. CSI bus connector (Figure 2.1) is a surface mount ZIF 15 socket which connects to a camera through a ribbon cable. Additionally, it is also possible to connect USB bus compatible cameras (e.g. webcam) to a Raspberry Pi USB port.
Figure 2.1 CIS bus connector There are basically 4 models of the official CSI bus compatible Raspberry Pi cameras. The features of these cameras and the ones manufactured by other companies are described briefly in the following sections. 2.2.1 Standard Raspberry Pi Daytime Cameras This was the first Raspberry Pi camera version (v1), announced in 2013 by the Raspberry Pi Foundation. This camera (Figure 2.2) has 5 megapixel OmniVision OV5647 type sensor, giving a resolution of 2592 x 1944 pixels, and is designed for daytime use. In 2016, the Raspberry Pi Foundation announced version 2 (v2) of the CSI camera (Figure 2.3), having 8 megapixels and slightly wider horizontal and vertical fields of view, and 3280 x 2464 pixels. This new camera uses the Sony IMX219.
● 19
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Figure 2.2 Raspberry Pi Standard v1 camera
Figure 2.3 Raspberry Pi Standard v2 camera Table 2.1 gives a comparison of the V1 and V2 cameras.
● 20
Camera v1
Camera v2
Resolution (Still)
5 megapixels
8 megapixels
Video modes
1080p30, 720p60,
1080p30, 720p60,
640x480p60/90
640x480p60/90
Sensor
OmniVision OV5647
Sony IMX219
Sensor resolution
2592 x 1944
3280 x 2464
Pixel size
1.4µm x 1.4µm
1.12µm x 1.12µm
Sensor image area
3.76mm x 2.74mm
3.68mm x 2.76mm
Focal length
3.60mm
3.04mm
Focus
Fixed focus
Fixed focus
Horizontal field of view
53.50 degrees
62.2 degrees
Vertical field of view
41.41 degrees
48.8 degrees
Shutter
Rolling shutter
Rolling shutter
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Chapter 3 • Raspberry Pi Camera Projects 3.1 Overview In the previous Chapters we have seen the basic features of the Raspberry Pi and Arduino computers. We have also seen the basic camera terminology and the types of Raspberry Pi and Arduino cameras available in the market place. In this Chapter we shall be developing various camera projects using the Raspberry Pi. The early sections of the Chapter describe how to install and then use the camera from the Raspberry Pi command line. In later sections we shall develop more interesting camera based projects using the Python programming language. 3.2 Installing the Camera The steps to install the camera on your Raspberry Pi are as follows: • Make sure that your Raspberry Pi is switched off • Locate the camera port, labelled as CSI or CAMERA • Pull up the plastic cable holder slider of the camera port gently by holding from both ends. The cable holder slider will open • Insert the camera ribbon cable (with the connector side facing away from the USB sockets) into the cable holder • Push down the plastic cable holder slider so that it locks and holds the cable • Apply power to your Raspberry Pi
Figure 3.1 Camera installed on Raspberry Pi
● 28
Chapter 3 • Raspberry Pi Camera Projects
You should now be able to test your camera interface and carry out camera operations to capture still images and video frames. This is described in the next sections using the Raspberry Pi command line. There are four camera commands available from the command interface. These are: raspistill and raspiyuv are used to capture images. raspistill uses the Image Encode component, and raspiyuv does not use an encoder but sends the output directly from the camera to a file. raspivid and raspividyuv are used for capturing video. raspivid uses the Video Encode component, and raspividyuv outputs directly to a file. Detailed information on camera commands is available at the following web sites: https://www.raspberrypi.org/app/uploads/2013/07/RaspiCam-Documentation.pdf and https://www.raspberrypi.org/documentation/raspbian/applications/camera.md Before using the camera commands from the Raspberry Pi command line, you have to start a terminal emulation software such as Putty so that you can enter the commands. If you have direct connection to your Raspberry Pi using a monitor then there is no need to use Putty and you can enter the commands directly through your keyboard. Also, we have to enable the camera interface on our Raspberry Pi. The steps are: • Login to your Raspberry Pi remotely (e.g. using Putty) • Enter command sudo raspi-config to display the Configuration Tool (Figure 3.2) pi@raspberrypi:~ $ sudo raspi-config
Figure 3.2 Raspberry Pi Configuration Tool • Navigate down the menu to Interfacing Options and press Enter
● 29
Chapter 4 • Simple Raspberry Pi Camera Projects using Python
Chapter 4 • Simple Raspberry Pi Camera Projects using Python 4.1 Overview In the last Chapter we have seen how to control the camera from the Raspberry Pi command line. In this Chapter we shall be developing camera projects using the Raspberry Pi hardware with the Python programming language. 4.2 Python picamera Library picamera is a Pyhton package that provides programming interface to the Raspberry Pi camera module. picamera is by default pre-installed on your raspberry Pi, but its latest version can be installed as follows: pi@raspberrypi:~ $ sudo apt-get install python-picamera
4.3 Using the Camera in Python Programs Using the Raspberry Pi camera in Python programs is easy. Function start_preview is used to preview an image. Function capture is then used to capture an image where the name of the image file is supplied as an argument to the function. Function stop_preview stops the preview. Notice that the start_preview only works if a monitor is directly connected to the Raspberry Pi. It will not work in the command mode or in the remote access (SSH) mode of access. In the example interactive Python code given below, an image is captured by the camera and is stored in file myfile.jpg under folder /home/pi/mypics. The image is captured by default with the resolution of 640x480 (remember to type python to enter the Python interactive mode). You should use statement camera.close() at the end to release all resources allocated to the camera.: >>> from picamera import PiCamera >>> camera = PiCamera() >>> camera.start_preview() >>> camera.capture("/home/pi/mypics/myfile.jpg") >>> camera.stop_preview() >>> camera.close()
We can change the resolution for example to 1024x768 with the command:
camera.resolution = (1024, 768)
4.4 Project 1 – Capturing Multiple Pictures Description: In this project we will capture 5 images with time intervals of 5 seconds and store all the image files in folder /home/pi/mypics. The images will be named Pic1.jpg, Pic2.jpg, Pic3.jpg, Pic4.jpg, and Pic5.jpg.
● 61
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Aim: The aim of this project is to show how multiple images can be captured and stored in a folder. Program Listing: Figure 4.1 shows the program listing (program: multipics.py). At the beginning of the program, modules time and picamera are included in the program. A for loop is then formed that iterates 5 times. Inside this loop, messages are displayed and images are captured and stored in files every 5 seconds. Figure 4.2 shows the messages displayed on the screen. You should run the program by entering the following command: pi@raspberrypi:~ $ python multipics.py #-------------------------------------------------------------------# #
CAPTURING MULTIPLE PICTURES
#
===========================
# # In this program 5 pictures are captured with 10 seconds intervals # and these pictures are stored in folder /home/pi/mypics with the # names Myimg1.jpg, Myimg2.jpg, Myimg3.jpg, Myimg4.jpg, Myimg5.jpg. # # Author: Dogan Ibrahim # File
: multipics.py
# Date
: January, 2019
#-------------------------------------------------------------import time from picamera import PiCamera camera = PiCamera() camera.start_preview() for c in range(5): time.sleep(5) print("Capturing image: %d of %d" %(c+1,5)) camera.capture('/home/pi/mypics/Pic%s.jpg' %(c+1)) camera.stop_preview() camera.close()
Figure 4.1 Program listing
Figure 4.2 Messages displayed on the screen
â—? 62
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Chapter 5 • Making a Motion Activation Spy Camera 5.1 Overview In last Chapter we have seen the design of several projects using the standard Raspberry Pi camera. In this Chapter we shall be designing a motion activated spy camera that takes pictures automatically when movement is detected inside a room. 5.2 Detection of Motion Motion, or the presence of a person is usually detected using PIR (Passive Infrared) sensor modules (see Figure 5.1), which basically consists of a pyroelectric sensor (see also section 4.21). The operation of the sensor is based on detecting the heat emitted by the body. Therefore, when a human or an animal gets into the range of the sensor, the heat emitted by the bodies in the form of infrared energy activates the sensor. The sensor module consists of a specially designed cover named the Fresnel lens which focuses the infrared signals onto the sensor. PIR sensor modules are 3 terminal devices with the pins: +5V, GND, and OUT. Two small potentiometers are provided on most modules (e.g. model HC-SR501) to adjust the sensitivity (usually up to 7 metres) and the delay time (usually 0.3 seconds to 5 minutes). The delay time sets the time for which the output stays high after a movement has been detected. Some modules include a jumper for setting the trigger mode of the module. In non-repeatable trigger mode (or single trigger mode), the output goes low after the delay time. In repeatable trigger mode, the output stays high as long as the object is in range.
Figure 5.1 PIR sensor module 5.3 Project 1 – Motion Activated Spy Camera Description: In this project a motion activated spy camera is designed. The project detects movement in a room and then automatically takes pictures every 15 seconds. The program can be configured so that it starts automatically after the re-start of the Raspberry Pi. Power to the system can be provided from a portable USB power bank since there is no need to connect the system to a PC, to a monitor, or to a display during its normal operation. Aim: The aim of this project is to design a motion activated spy camera using a PIR sensor module.
â—? 114
Chapter 5 • Making a Motion Activation Spy Camera
Block Diagram: The block diagram of the project is shown in Figure 5.2. In this project the HC-SR501 PIR sensor module is used. This module has the following specifications: • 4.5V to 20V operation • 50µA quiescent current • 3.3V TTL output (when HIGH) • 3s to 5 minutes delay time • 3 m to 7 m sensitivity range • Two trigger modes • Less than 120 degrees sensitivity range within 7 metres • -15º to 70º centigrade operation • 1 minute initialization time (the application system should wait one minute before reading the sensor output to avoid any false readings)
Figure 5.2 Block diagram of the project Circuit Diagram: Figure 5.3 shows the circuit PCB layout of the sensor module. The jumper at the left sets the trigger mode, the potentiometer at the bottom left sets the sensitivity, while the potentiometer at the bottom right sets the delay time (see Figure 5.1). The detection profile of the sensor module is a 110 degree cone with a range of 3 m to 7 m. Turning the sensitivity potentiometer clockwise decreases the sensitivity. When fully right, the sensitivity is 3 metres. Similarly, turning the sensitivity potentiometer anti-clockwise increases the sensitivity. When fully left, the sensitivity is 7 metres. Turning the delay potentiometer clockwise increases the delay time. When fully right, the delay time is approximately 5 minutes. Similarly, turning the delay potentiometer anti-clockwise decreases the delay time. When fully left, the delay time is approximately 3 seconds. It is recommended to turn both potentiometers fully CCW to start with.
● 115
Chapter 6 • Raspberry Pi Camera Flash Light
Chapter 6 • Raspberry Pi Camera Flash Light 6.1 Overview Although the Raspberry Pi camera is a high resolution camera, it is difficult to take pictures in low light conditions since it lacks a flash light. Most smartphones have built-in camera flash lights in the form of LEDs. These LEDs are synchronized with the camera shutter so that they only come ON when the shutter button is released. In this Chapter we shall be designing both normal and synchronized flash lights for our Raspberry Pi camera. Interested readers may find on the internet that there are several models of flash lights are available commercially. One such product is available from the following website: https://uk.pi-supply.com/products/bright-pi-brightwhite-ir-camera-light-raspberry-pi 6.2 Project 1 - Unsynchronized Flash Light Description: Perhaps the easiest flash light for your Raspberry Pi camera is to use a high brightness white LED and to turn ON the LED just before taking a picture. As an example project, the super bright white LED from SparkFun Electronics (model no: YSLR547W2C-A13 or a similar one) can be used as a flash light. This LED has the following specifications (note that for comparison, the luminous intensity of a standard 5mm Red LED is 100 mcd): • Forward current: 20mA • Reverse current: 10µA • Power dissipation: 105mW • Forward voltage: 3.2V • Luminous intensity: 8000 – 10000 mcd Aim: The aim of this project is to design a non-synchronizing flash light for the Raspberry Pi camera. Block Diagram: The block diagram of the project is shown in Figure 6.1. The driver circuit consists of a transistor switch as shown in the circuit diagram.
Figure 6.1 Block diagram of the project
● 121
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Circuit Diagram: Because the forward voltage of the LED is 3.2V, it makes sense to drive the LED from +5V through a for example 2N2222 NPN transistor circuit as shown in Figure 6.2 where it is assumed that the LED is connected to port pin GPIO 17. Assuming 3.2V LED forward voltage, and transistor VCEsat = 0.4V (check the transistor data sheet), the load resistance is given by:
RL = (5V – 3.2V-0.4) / 20mA = 70 Ohm
Assuming a minimum hFE of 75, VBEsat = 0.6V, the base current is: IB = IC / hFE = 20 / 75 = 0.26mA. We can take IB = 0.5mA to make sure that the transistor is in hard saturation. The base resistance is then given by: RB = (3.3V – 0.6) / 0.5mA = 5.4K, we can take 4.7K as the nearest physical resistor
Figure 6.2 Circuit diagram of the project We can now write a program to turn ON our LED just before taking a picture. The required Python program (program: camflash.py) is shown in Figure 6.3. A picture is taken when the button is pressed. In this program, the LED is turned ON just one second before and after a picture is taken. #-------------------------------------------------------------# #
CAMERA WITH LED FLASH LIGHT
#
===========================
# # In this program the camera is connected to the Raspberry Pi. # In addition, a high brightness white LED is connected to GPIO # 17 through a transistor switch. Also, a button is connected to # GPIO 3. A picture is taken when the button is pressed.The LED # flash light turns ON for 1 second just before and after taking # a picture. # # Author: Dogan Ibrahim
● 122
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Chapter 7 • Designing a Raspberry Pi Surveillance Camera System 7.1 Overview In the last Chapter we have seen how to design a flash light for our camera module. In this Chapter we shall be designing a surveillance camera system. This system detects the changes in captured frames and informs if there is a change. Any movement or disturbance between successive frames is an indication of the existence of a possible intruder in the view area. 7.2.1 The Motion Library In this Chapter we shall be using the motion library (surveillance software library) for our surveillance system. This library gives us the following features: • Watching live video feed on a web browser (the IP address of your Raspberry Pi is required) • Recording and saving the video whenever motion is detected in the view area creating timelapse videos • Taking snapshots and saving them on disk when motion has been detected The steps in installing and using this library are given below. • Install the motion library. pi@raspberrypi:~ $ sudo apt-get install motion
• Enable the motion daemon so that it will always run. Edit file /etc/default/motion using the nano editor and enter the line: start_motion_daemon=yes. Enter Cntrl+X followed by Y to save and exit nano. pi@raspberrypi:~ $ sudo nano /etc/default/motion
7.2.2 Configuring Motion Software Now we have to configure the motion software before using it. The steps are as follows: • Make a backup copy of the motion configuration file just in case: pi@raspberrypi:~ $ sudo cp /etc/motion/motion.conf /etc/motion/motion. conf.bak
• Edit the configuration file using the nano editor: pi@raspberrypi:~ $ sudo nano /etc/motion/motion.conf
● 128
Chapter 7 • Designing a Raspberry Pi Surveillance Camera System
• Find the options below and edit them as shown (check the motion software documentation for further information about these and other options): Uncomment target_dir and change to /home/pi/motion Define the logfile: logfile /home/pi/motion/mypics/logfile.log Set daemon to on Set output_pictures to best Set quality to 80 Set text_changes to on Set ffmpeg_output_movies to off Set webcontrol_port to 8082 Set webcontrol_localhost to off Set width and height to 640 and 480 respectively Set stream_localhost to off Set framerate to 20 Set locate_motion_mode to preview Set event_gap to 10 Set pre_capture to 1 Set post_capture to 1 • Create folder motion: pi@raspberrypi:~ $ mkdir /home/pi/motion
• Activate the official camera driver (note that it is lower case letter 'l' below and not number'1'): pi@raspberrypi:~ $ sudo modprobe bcm2835-v4l2
• You can insert the following line at the end of file /etc/rc.local so that the camera driver is loaded automatically after a reboot: pi@raspberrypi:~ $ sudo nano /etc/rc.local
• and insert sudo modprobe bcm2835-v4l2 at the end of the file. Enter Cntrl+X followed by Y to exit the nano editor • Make motion to be the owner of the folder where the captured files will be stored: pi@raspberrypi:~ $ sudo chown motion:motion /home/pi/motion pi@raspberrypi:~ $ sudo chown motion:motion /home/pi/motion/logfile.log
• Restart your Raspberry Pi
● 129
Chapter 8 • The motionEyeOS Surveillance Software
Chapter 8 • The motionEyeOS Surveillance Software 8.1 Overview In this Chapter we shall be installing and using the motionEyeOS on our Raspberry Pi 3. Some of the motionEyeOS features are: • Multiple camera support including USB cameras • Take still images • Set a working schedule • Motion detection with email notification • Upload files to Google Drive or Dropbox • Several formats for movies • Timelapse movie support • Ethernet or WiFi support • Video streaming • Files can be accessed through built-in FTP server (or SFTP server) 8.2 Installing the motionEyeOS motionEyeOS is installed on a new formatted microSD card and then this card is inserted into your raspberry Pi board. In this Chapter we shall be using a 16GB microSD card, although you can also use a higher capacity card if you wish. The steps to install the motionEyeOS are given below: • Install the SD card formatter software from the following site (see Figure 8.1): https://www.sdcard.org/downloads/formatter_4/
Figure 8.1 Install the SD card formatter software
● 135
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
• Insert the microSD card into your PC and start the SD card formatter application (you may have to use an adapter to insert the microSD card into a standard size SD card slot on your PC). Select the card and format with the Overwrite format option (see Figure 8.2)
Figure 8.2 Format your microSD card • Choose the right image file for your device from the MotionEyeOS releases page and save the file in a folder: https://github.com/ccrisan/motioneyeos/releases If you are using the Raspberry Pi 3, at the time of writing this book the image file to choose was: motioneyeos-raspberrypi3-20190119.img.xz • You now need to extract and load the image file onto the microSD card. Notice that you cannot simply extract and copy the image file onto an SD card since the operating system needs to create a boot block on the card. It is recommended to use the Etcher program for this task. You can install the Etcher program on your PC from the following site: https://www.balena.io/etcher/ • Start the Etcher program (Figure 8.3). Click Select image and browse to select the image file you saved earlier.
● 136
Chapter 9 • The OpenCV Software Package
Chapter 9 • The OpenCV Software Package 9.1 Overview In the last Chapter we have seen how to install and use the motionEyeOS on our Raspberry Pi 3. In this Chapter we shall see how to install the OpenCV software package on our Raspberry Pi 3. OpenCV is a highly popular image processing software that can be used to capture and manipulate images on our Raspberry Pi. 9.2 Copying the Raspberry Pi microSD Card If your Raspberry Pi operating system is on an 8GB microSD card, you may want to copy it to a larger card before installing and using the OpenCV software package since this package requires a lot of space. The microSD card used by the author was only 8GB. The steps to image copy all the files and the operating system to a larger capacity microSD card are given below. Here, the files are copied to a 16GB microSD card: • Remove power from your Raspberry Pi and remove the microSD card • Insert the microSD card into your PC (you may have to use an adapter as most PCs accept standard size SD cards) • Run program Win32DiskImager on your PC • Make sure that the microSD card is selected at the top right hand side (F:\ in this example. Give a name to the image file to be saved on your PC. In this example the image file is named C:/MyRPi as shown in Figure 9.1. • Click Read to copy the files to C:\MyRPi. Wait until the copy process completes and close the Win32DiskImager program. • Remove the microSD card from your PC and insert the new 16GB microSD card into your PC • Re-run the Win32DiskImager program, enter the image filename as C:/MyRPi and click Write to copy the image to the new microSD card. Wait until the copy process completes. • Remove the new microSD card from your PC and insert into your Raspberry Pi. Apply power to your Pi and it should start as before.
● 143
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Figure 9.1 Use the Win32DiskImager program • You will find that just copying the files to a bigger microSD card does not increase the usable disk space automatically. If your operating system was installed without using the NOOBS then you do the following to increase the usable space on your microSD card: -- Run raspi-config to display the configuration menu and select Advanced Options (Figure 9.2) pi@raspberrypi:~ $ sudo raspi-config
Figure 9.2 The configuration menu -- Select Expand Filesystem to expand the filesystem on the microSD card -- Exit from the configuration menu • If your operating system was installed using NOOBS then it is much more complicated to expand the usable space on your new microSD card. The following link should give you the step by step instructions to expand the filesystem on your microSD card (you are warned that these are low level disk operations and doing something wrong may make your microSD card unusable): http://dracoy.com/2016/06/26/expanding-partition-onsd-card-for-raspberry-pi-with-noobs-pre-installed/
● 144
Chapter 10 • Capturing Images from the Camera using OpenCV
Chapter 10 • Capturing Images from the Camera using OpenCV 10.1 Overview Last Chapter we have seen how to install the OpenCV image processing software package on our Raspberry Pi. In this Chapter we shall be learning how to capture still images and video movies from a camera using OpenCV. Full details on picamera can be obtained from the following link: https://media.readthedocs.org/pdf/picamera/latest/picamera.pdf 10.2 Capturing Images From a Raspberry Pi Camera using OpenCV In this Chapter we shall be using the picamera library to capture images from our Raspberry Pi camera. Firstly, we have to install the picamera package: (cv)pi@raspberrypi:~ $ pip install "picamera[array]"
Figure 10.1 shows a program (program: cam.py) which is used to capture an image from the camera. The image is stored in folder /home/pi/mypics with the name img.jpg. Notice that two seconds camera warm-up time is used in the program. #---------------------------------------------------# #
CAPTURE AN IMAGE ================
# # This program captures an image using the Raspberry Pi # camera and OpenCV. The image is stored in folder # /home/pi/mypics withh filename img.jpg # # File:
cam.py
# Date:
February, 2019
# Author: Dogan Ibrahim #----------------------------------------------------import cv2 from picamera import PiCamera import time camera = PiCamera() camera.resolution = (1024, 768) camera.start_preview() print("Ready to capture an image") time.sleep(2) camera.capture('/home/pi/mypics/img.jpg') print("Image has been captured...") camera.stop_preview() camera.close()
Figure 10.1 Program listing ● 183
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
We can resize the image during capturing it by using option resize of capture. e.g.:
camera.capture('/home/pi/mypics/img.jpg, resize=(320,240))
Some important camera settings supported by OpenCV are given below:
shutter_speed: sets or retrieves the shutter speed in microseconds exposure_speed: retrieves the camera exposure speed exposure_mode: sets or retrieves the camera exposure mode.
Some valid values are: off, auto, night, nightpreview, backlight, spotlight, sports, snow, beach etc (default is auto)
iso: sets or retrieves the camera ISO setting awb_mode: sets or retrieves the camera auto-while-balance mode.
Valid values are: ff, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandescent, flash, horio zon (default is auto) awb_gains: sets or retrieves the camera auto-while-balance gain annotate_text: insert text to the top middle part of the image annotate_text_size: sets the size of the text. Default is 32, valid values are 6 to 160 brightness: sets or retrieves the camera brightness. Valid values are 0 to 100 (default is 50) contrast: sets or retrieves the camera contrast. Valid values are -100 to 100 (default is 0) flash_mode: sets or retrieves the flash mode. Valid values are: off, auto, on, redeye, fillin, torch (default is off) hflip: sets or retrieves whether camera's output is horizontally clipped image_effect: sets or retrieves the image effect applied to the image. Some of the valid values are: none, negative, solarize, sketch, denoise, emboss, etc (default is none) rotation: sets or retrieves camera's rotation. Valid values are: 0, 90, 180, 270 (default is 0) sharpness: sets or retrieves the sharpness setting. Valid values are -100 to 100 (default is 0) vflip: sets or retrieves whether camera's output is vertically flipped
â—? 184
CHAPTER 11 • Using the SimpleCV in Camera Projects
CHAPTER 11 • Using the SimpleCV in Camera Projects 11.1 Overview In this Chapter we shall be developing camera based projects using the Raspberry Pi with a camera, using the popular SimpleCV image processing software. SimpleCV is similar to OpenCV but is easier to use and also is not as powerful as the OpenCV. The first project in this Chapter is the barcode reader. In this project the barcode is used as a password to activate a relay, for example to open a door. The second project is on motion detection where a buzzer is activated if motion is detected in a room. Before going into the details of the barcode project, it is worthwhile to look at the basic principles of different types of barcodes. 11.2 Barcodes The barcode was invented in 1950s in the USA by Joseph Woodland and Bernard Silver. Barcodes are currently used in supermarkets and shops as labels attached to or printed on the products in order to easily and quickly identify them. There are many types of barcodes in use. Some popular barcode types are described briefly below: 11.2.1 Code 39 This was one of the first codes developed in 1974. This code has the capability to encode all of the 128 ASCII character set. The width of the code can be extended, limited only by the size of the label. This code has the disadvantage that it is not compact and therefore can take more space than other codes. 11.2.2 Code 128 This code is compact and is commonly used in product identification applications. Because it is very compact, Code 128 replaces Code 39 in most applications. This is a variable length code. 11.2.3 Universal Product Code The Universal Product Code (or UPC for short) are found nearly on all grocery products as a means of identifying the products. UPC is a 12-digit omnidirectional number, assigned by the Global Standards Organization (GS1). An example UPC is shown in Figure 11.1. These barcodes can easily be printed on a standard high quality printer. In Figure 11.1 (code = 725272730706), the first 6 digits (725272) is the manufacturer's code enabling up to one million items to be uniquely coded, next 5 digits (73070) are the item numbers, the last digit (6) is the check digit. The check digit is calculated as follows (for the example in Figure 11.1): • Add together all the digits in odd positions (i.e. digits 1,3,5,7,9,11). In this example we have: 7+5+7+7+0+0 = 26 • Multiply the number by 3. i.e. 3 x 26 = 78
● 195
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
• Add together all the digits in even positions (i.e. digits 2,4,6,8,10). In this example we have: 2+2+2+3+7 = 16 • Add the result to the value found earlier. i.e. 78 + 16 = 94 • Consider the number found above i.e. 94, such that when a number is added to this number we get a multiple of 10. The nearest multiple of 10 to 90 is 100, therefore the check digit is 100-90 = 6 which is the last digit in Figure 11.1
Figure 11.1 Example UPC barcode It is important to make sure that every item in a store has a unique UPC number. Even different sizes of the same item must have a different UPC code. 11.3 Project 1 – Barcode Password Description: This is a barcode based password project. A secret barcode number is stored by the project. When the correct barcode is put in-front of the camera the system recognizes this code as the correct code and activates a relay. The relay then for example opens a door or causes some other actions to be taken. Aim: The aim of this project is to show how barcodes can be read with the Raspberry Pi and a camera. Block Diagram: Figure 11.2 shows the block diagram of the project.
Figure 11.2 Block diagram of the project Circuit Diagram: The circuit diagram of the project is shown in Figure 11.3. In this project a standard Raspberry Pi camera is used and is connected to the CSI bus. A relay is connected to GPIO pin 2 of the Raspberry Pi.
● 196
Chapter 12 • Using Camera with Arduino
Chapter 12 • Using Camera with Arduino 12.1 Overview In this Chapter we shall be learning how to capture images using a camera and the Arduino Uno microcontroller development board. In actual fact, the Arduino Uno is not a suitable processor for image processing for the following reasons: • The memory of Arduino Uno is very limited • Arduino Uno processor has not got enough processing power to handle image processing operations • Arduino Uno does not have an operating system like Linux, Windows, MAC etc. Because of this it is not possible to install the OpenCV image processing package on Arduino. Because of the above limitations of the Arduino Uno, we shall only capture files using a suitable compatible camera module. An external SD card will be used to save the captured images. The images can be displayed or processed by inserting the SD card into a powerful computer such as a Windows system, Raspberry Pi or any other processor. Because we will be using an SD card to save the captured images, it is worthwhile to learn how to interface an SD card to the Arduino Uno and how to create files on the SD card to read and write data. This is explained in the next project. 12.2 Project 1 – Arduino Uno SD Card Interface: Writing to the Card Description: In this project we will interface a microSD card to our Arduino Uno and see how we can create a new file on the microSD card, and learn how write text to this file. In this project the text My Computer will be saved in a file called MyFile.txt on the SD card. Aim: The aim of this project is to show how a microSD card can be interfaced to the Arduino Uno and how a file can be created and text stored in this file. Block Diagram: Figure 12.1 shows the block diagram of the project.
Figure 12.1 Block diagram of the project
● 203
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Circuit Diagram: In this project a microSD card is used as shown in Figure 12.2 (you can use a standard or a mini SD card if you wish, the circuit diagram and the program are the same. The only thing to be careful of is that the card adapter voltage levels should be compatible with the Arduino signal levels).
Figure 12.2 microSD card SD cards use the SPI interface and they can easily be connected to a microcontroller using adapter readers. The adapter reader used in this project is shown in Figure 12.3. The circuit diagram of the project is shown in Figure 12.4. The following pins are used to connect the SD card adapter to the Arduino Uno (notice that the Arduino SPI port pins are MOSI (11), MISO (12), SCK (13): microSD card Pin Arduino Uno Pin Vcc 5V CS 4 MOSI 11 MISO 12 SCK 13 GND GND
Figure 12.3 microSD card adapter reader used in the project The microSD card adapter reader used in the project has the following specifications: • Power supply is 4.5V ~ 5.5V, with on-board 3.3V voltage regulator • Standard SPI interface • 4 M2 screws positioning holes for easy installation
● 204
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Chapter 13 • Automatic Number Plate Recognition 13.1 Overview In this Chapter we shall be seeing how to recognize a vehicle number plate (ANPR) using the OpenALPR software library with the Raspberry Pi. Examples are given for both the command line interface and also by using the Python language. Although it is also possible to do number plate recognition using OpenCV, this method is highly theoretical and complex and is beyond the scope of this book. 13.2 Vehicle Number Plates In general, vehicle number plates in different countries have different shapes. Figure 13.1 shows example vehicle number plates in some countries. The beauty of the OpenALPR library is that it can be configured to recognize the number plates of different countries.
Figure 13.1 Some vehicle number plates 13.3 The OpenALPR Library The OpenALPR library is an open source vehicle number plate recognition software library that can be used in connection with several compilers, such as C#, C++, Java, Python etc to recognize the vehicle number plates. Instead of using neural network algorithms, OpenALPR library uses computer vision techniques and is based on OpenCV and Tesseract OCR libraries. 13.4 Using With the Command Line The OpenALPR library must be installed before it can be used. The following command installs the library: pi@raspberrypi:~ $ sudo apt install openalpr openalpr-daemon openalpr-utils libopenalpr-dev
13.4.1 Project 1 - Recognizing Number Plate from a File Description: In this project we will see how to use the OpenALPR library by recognizing the number plate shown by the jpeg image in Figure 13.2, named Plate.jpg, and copied to our default directory. This is an example number plate for a vehicle in Germany.
Figure 13.2 Example number plate (plate.jpg)
â—? 238
Chapter 13 • Automatic Number Plate Recognition
Aim: the aim of this project is to show how a number plate can be recognized from an image file. Block Diagram: The block diagram of the project is shown in Figure 13.3.
Figure 13.3 Block diagram of the project Commands: Enter the following command to recognize the number plate. In the example below, -c option selects the country where eu is for Europe: pi@raspberrypi:~ $ alpr –c eu Plate.jpg
Which displays the text shown in Figure 13.4. Notice that by default 10 outputs are given with different levels of confidence. The result at the top of the list is given with over 88% confidence and we have to take this value which is the correct number plate:
Figure 13.4 Output of the number plate recognition OpenALPR has several options. The commonly used options are: -c specifies the country code (e.g. us for USA, gb for Great Britain, eu for Europe, etc) -n maximum number of possible number plates to return (default is 10) -p attempt to match a number plate against a plate pattern (e.g. ca for California) -j output recognition result in JSON format (default is off) -h displays usage information
● 239
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Index A Adding text 63 Adding images 169 Alpr 239 Annotate 63 Arducam 25, 213 Arducam interface 214 Arducam library 217 Arduino camera 203 Arithmetic operations 169 Aspect ratio 18 Automatic number plate recognition 238 Avconv 38, 51 B Barcodes 195 Barcode password 196 Black level calibration 17 Bluetooth 51 Blurring 176 BMP 15 Brightness 64 Buzzer 72 C Canny 164 Camera cable 23 Camera cable joiner 23 Camera display 77 Camera flash 121 Camera mount 23 Camera saturation 65 Camera settings 63 Camera sharpness 65 Camera shield 26 Camera tripot 23 Camera v1 20 Camera v2 20 Camera with Arduino 202 Capturing video 93 Circle 156 Circle detection 165 Consecutive images 65 Contrast 64
â—? 252
Crontab 39 Crontab generator 40 D Day time camera 19 Depth of field 17 Detecting colour 177 Detecting motion 114 destroyAllWindows 153 Digital photo frame 95 Display 77 Docker 58 Doorbell 51 Dots per inch 12 Dpi 12 Drawing circle 156 Drawing ellipse 156 Drawing line 155 Drawing rectangle 156 Dt-blob 124 E Edge detection 164 Ellipse 156 Email 96, 140 ESP-EYE 249 Exposure modes 65 External shutter 231 Eye detection 180 F Face detection 180, 250 Ffmpeg 59, 68 Field of view 18 File transfer 55 Flash 121 Focal length 17 Frames per second 17 Fswebcam 43 G GaussianBlur 176 Geometric recognition 174 GIFF 15
H Haar Cascades 180 HC-SR501 103 Hough circle transform 165 Hough line transform 167
OpenCV image operations 152 OpenCV with USB 189 OpenCV video recording 188 OV2640 25 Ov5647 22
I Image effects 65 Image file 13 Imread 153 Imshow 153 Imwrite 153 Infrared camera 21 Inter_Area 159 Inter_linear 159
P Photobox 88 Picamera 61 Pin_config 125 Pin_define 125 PIR sensor 103, 114 Pixel 12 Pixel colour 12 PNG 16 Power pack 107 Putty 29
J JPEG 13 L Line 155 Line detection 167 Live streaming 55 Long range WiFi 107 Lossless compression 13 Lossy compression 13 M MedianBlur 176 Morphological operations 173 Motion activated camera 114 Motion detection 140 Motion detection with buzzer 200 Sending motion email 140 motionEyeOS 135 Motion library 128 Motion server 130 N Night vision Number plate
22 238
O Omxplayer 38 OpenALPR 242 OpenCV 143
Q Quality level
3
R Raspberry Pi display 78 Raspberry Pi pins 248 Raspistill 29 Raspivid 29, 104 Raspiyuv 29 Recording video 50, 192 Recording wildlife 102 Rectangle 156 Relay 53 Resize 159 Rolling shutter 18 S Scheduling 39 Screen blanking 94 SD formatter 135 Sdptool 52 Shape recognition 175 Selfie 68, 72, 77, 84, 86 Sending email 96, 99 Seven inch display 78 Shutter speed 17 SimpleCV 195 SmartPi 78
â—? 253
Camera Projects Book - 39 Experiments with Raspberry Pi and Arduino
Smoothing 176 Soft keyboard 84 Spy camera 114 Subtracting images 171 Surveillance camera 128 Synchronized flash 124 T Terminal 85 TIFF 14 Timed photogtaphy 75 Timelapse 36, 67, 186, 187 Timelapse with Arduino 226 TTL camera 27 Twitter 107 Twitting photo 108 Twitting picture 107 Twitting text 108 U Unsynchronized flash USB camera
121 43
V Vncserver 30 Vncviewer 30 W waitKey 153 Webcam 24 White balance 17 WiFi 107 Wildlife 102 Window_autosize 154 Window_normal 154 Winscp 38
X X11 forwarding 150 Xming 150 Y Youtube 56
â—? 254
39 Experiments with Raspberry Pi and Arduino
Camera Projects Book This book is about Raspberry Pi 3 and Arduino camera projects. The book explains in simple terms and with tested and working example projects, how to configure and use a Raspberry Pi camera and USB based webcam in camera-based projects using a Raspberry Pi. Prof. Dr. Dogan Ibrahim has a BSc. in Electronic Engineering, an MSc. in Automatic Control Engineering, and a Ph.D. in Digital Signal Processing. He worked in many industrial organisations before returning to academia. Prof. Ibrahim is the author of over 60 technical books and over 200 technical articles on microcontrollers, microprocessors, and related fields. He is a Chartered Electrical Engineer and a Fellow of the Institution of Engineering Technology.
Example projects are given to capture images, create timelapse photography, record video, use the camera and Raspberry Pi in security and surveillance applications, post images to Twitter, record wildlife, stream live video to YouTube, use a night camera, send pictures to smartphones, face and eye detection, colour and shape recognition, number plate recognition, barcode recognition and many more. Installation and use of popular image processing libraries and software including OpenCV, SimpleCV, and OpenALPR are explained in detail using a Raspberry Pi. The book also explains in detail how to use a camera on an Arduino development board to capture images and then save them on a microSD card. All projects given in this book have been fully tested and are working. Program listings for all Raspberry Pi and Arduino projects used in this book are available for download on the Elektor website.
ISBN 978-1-907920-77-6
Elektor International Media BV
www.elektor.com
LEARN DESIGN SHARE
● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEA IGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D HARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN N ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEA SIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● D