Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenCV 4 with Python Blueprints

You're reading from   OpenCV 4 with Python Blueprints Build creative computer vision projects with the latest version of OpenCV 4 and Python 3

Arrow left icon
Product type Paperback
Published in Mar 2020
Publisher Packt
ISBN-13 9781789801811
Length 366 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (4):
Arrow left icon
Michael Beyeler (USD) Michael Beyeler (USD)
Author Profile Icon Michael Beyeler (USD)
Michael Beyeler (USD)
Dr. Menua Gevorgyan Dr. Menua Gevorgyan
Author Profile Icon Dr. Menua Gevorgyan
Dr. Menua Gevorgyan
Michael Beyeler Michael Beyeler
Author Profile Icon Michael Beyeler
Michael Beyeler
Arsen Mamikonyan Arsen Mamikonyan
Author Profile Icon Arsen Mamikonyan
Arsen Mamikonyan
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Fun with Filters 2. Hand Gesture Recognition Using a Kinect Depth Sensor FREE CHAPTER 3. Finding Objects via Feature Matching and Perspective Transforms 4. 3D Scene Reconstruction Using Structure from Motion 5. Using Computational Photography with OpenCV 6. Tracking Visually Salient Objects 7. Learning to Recognize Traffic Signs 8. Learning to Recognize Facial Emotions 9. Learning to Classify and Localize Objects 10. Learning to Detect and Track Objects 11. Profiling and Accelerating Your Apps 12. Setting Up a Docker Container 13. Other Books You May Enjoy

Tracking hand gestures in real time

Hand gestures are analyzed by the recognize function; this is where the real magic takes place. This function handles the entire process flow, from the raw grayscale image to a recognized hand gesture. It returns the number of fingers and the illustration frame. It implements the following procedure:

  1. It extracts the user's hand region by analyzing the depth map (img_gray), and returns a hand region mask (segment), like this:
def recognize(img_gray: np.ndarray) -> Tuple[int,np.ndarray]:
# segment arm region
segment = segment_arm(img_gray)
  1. It performs contour analysis on the hand region mask (segment). Then, it returns the largest contour found in the image (contour) and any convexity defects (defects), as follows:
# find the hull of the segmented area, and based on that find the
# convexity defects
contour, defects = find_hull_defects(segment)
  1. Based on the contour found and the convexity defects, it detects the number of extended fingers (num_fingers) in the image. Then, it creates an illustration image (img_draw) using (segment) image as a template, and annotates it with contour and defect points, like this:
img_draw = cv2.cvtColor(segment, cv2.COLOR_GRAY2RGB)
num_fingers, img_draw = detect_num_fingers(contour,
defects, img_draw)
  1. Finally, the estimated number of extended fingers (num_fingers), as well as the annotated output image (img_draw), are returned, as follows:
return num_fingers, img_draw

In the next section, let's learn how to accomplish hand region segmentation, which we used at the beginning of the procedure.

You have been reading a chapter from
OpenCV 4 with Python Blueprints - Second Edition
Published in: Mar 2020
Publisher: Packt
ISBN-13: 9781789801811
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at R$50/month. Cancel anytime