Book Image

Python Robotics Projects

By : Prof. Diwakar Vaish
Book Image

Python Robotics Projects

By: Prof. Diwakar Vaish

Overview of this book

Robotics is a fast-growing industry. Multiple surveys state that investment in the field has increased tenfold in the last 6 years, and is set to become a $100-billion sector by 2020. Robots are prevalent throughout all industries, and they are all set to be a part of our domestic lives. This book starts with the installation and basic steps in configuring a robotic controller. You'll then move on to setting up your environment to use Python with the robotic controller. You'll dive deep into building simple robotic projects, such as a pet-feeding robot, and more complicated projects, such as machine learning enabled home automation system (Jarvis), vision processing based robots and a self-driven robotic vehicle using Python. By the end of this book, you'll know how to build smart robots using Python.
Table of Contents (24 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Image recognition


Now, to go ahead and start doing vision processing, let's connect the camera to Raspberry Pi. Once you have done that, you need to write the following code:

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

while True:
        _, image = cap.read()
        cv2.imshow("Frame", image)
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)


        lowerGreen = np.array([80,50,50])
        upperGreen = np.array([130,255,255])

        mask = cv2.inRange(hsv, lowerGreen, upperGreen)
        res = cv2.bitwise_and(image, image, mask=mask)
        cv2.imshow('mask',mask)
        cv2.imshow('result',res)
        key = cv2.waitKey(1) & 0xFF


        if key == ord('q'):
                break
cv2.destroyAllWindows()
cap.release()

Before you actually compile this code, let me tell you what exactly we are doing:

import numpy as np

In the preceding line, we are importing the library numpy as np, or in other words, we have imported the library, and every time we need to call is...