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 cv2import numpy as npcap = 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'):                breakcv2.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 ...

Get Python Robotics Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.