Book Image

Computer Vision with Python 3

By : Saurabh Kapur
Book Image

Computer Vision with Python 3

By: Saurabh Kapur

Overview of this book

<p>This book is a thorough guide for developers who want to get started with building computer vision applications using Python 3. The book is divided into five sections: The Fundamentals of Image Processing, Applied Computer Vision, Making Applications Smarter,Extending your Capabilities using OpenCV, and Getting Hands on. Throughout this book, three image processing libraries Pillow, Scikit-Image, and OpenCV will be used to implement different computer vision algorithms.</p> <p>The book aims to equip readers to build Computer Vision applications that are capable of working in real-world scenarios effectively. Some of the applications that we will look at in the book are Optical Character Recognition, Object Tracking and building a Computer Vision as a Service platform that works over the internet.</p>
Table of Contents (17 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
7
Introduction to Computer Vision using OpenCV

Color tracking


In this section, we will try to understand how to track a color in a video using OpenCV. The following is the code for tracking yellow color in a video:

import cv2
import numpy as np

def detect(img):

         lower_range = np.array([40,150,150], dtype = "uint8")
         upper_range = np.array([70,255,255], dtype = "uint8")

         img = cv2.inRange(img,lower_range,upper_range)
         cv2.imshow("Range",img)

         m=cv2.moments(img)
         if (m["m00"] != 0):
                  x = int(m["m10"]/m["m00"])
                  y = int(m["m01"]/m["m00"])
         else:
                  x = 0
                  y = 0

         return (x, y)


cam = cv2.VideoCapture(0)

last_x = 0
last_y = 0

while (cam.isOpened()):

         ret, frame = cam.read()

         cur_x, cur_y = detect(frame)

         cv2.line(frame,(cur_x,cur_y),(last_x,last_y),(0,0,200),5);
         last_x = cur_x
         last_y = cur_y        
         cv2.imshow('frame',frame)

         if cv2.waitKey(1...