|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "b83c3e74", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import cv2 \n", |
| 11 | + "import numpy as np\n", |
| 12 | + "\n", |
| 13 | + "#capturing video through webcam\n", |
| 14 | + "cap=cv2.VideoCapture(0)\n", |
| 15 | + "\n", |
| 16 | + "while(1):\n", |
| 17 | + " _, img = cap.read()\n", |
| 18 | + " \n", |
| 19 | + "#converting frame(img i.e BGR) to HSV (hue-saturation-value)\n", |
| 20 | + "hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)\n", |
| 21 | + "\n", |
| 22 | + "#definig the range of red color\n", |
| 23 | + "red_lower=np.array([136,87,111],np.uint8)\n", |
| 24 | + "red_upper=np.array([180,255,255],np.uint8)\n", |
| 25 | + "\n", |
| 26 | + "#defining the Range of Blue color\n", |
| 27 | + "blue_lower=np.array([99,115,150],np.uint8)\n", |
| 28 | + "blue_upper=np.array([110,255,255],np.uint8)\n", |
| 29 | + "\n", |
| 30 | + "#defining the Range of yellow color\n", |
| 31 | + "yellow_lower=np.array([22,60,200],np.uint8)\n", |
| 32 | + "yellow_upper=np.array([60,255,255],np.uint8)\n", |
| 33 | + "\n", |
| 34 | + "#finding the range of red,blue and yellow color in the image\n", |
| 35 | + "red=cv2.inRange(hsv, red_lower, red_upper)\n", |
| 36 | + "blue=cv2.inRange(hsv,blue_lower,blue_upper)\n", |
| 37 | + "yellow=cv2.inRange(hsv,yellow_lower,yellow_upper)\n", |
| 38 | + "\n", |
| 39 | + "#Morphological transformation, Dilation \t\n", |
| 40 | + "kernal = np.ones((5 ,5), \"uint8\")\n", |
| 41 | + "\n", |
| 42 | + "red=cv2.dilate(red, kernal)\n", |
| 43 | + "res=cv2.bitwise_and(img, img, mask = red)\n", |
| 44 | + "\n", |
| 45 | + "blue=cv2.dilate(blue,kernal)\n", |
| 46 | + "res1=cv2.bitwise_and(img, img, mask = blue)\n", |
| 47 | + "\n", |
| 48 | + "yellow=cv2.dilate(yellow,kernal)\n", |
| 49 | + "res2=cv2.bitwise_and(img, img, mask = yellow) \n", |
| 50 | + "\n", |
| 51 | + "#Tracking the Red Color\n", |
| 52 | + "(_,contours,hierarchy)=cv2.findContours(red,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", |
| 53 | + "\n", |
| 54 | + "for pic, contour in enumerate(contours):\n", |
| 55 | + " area = cv2.contourArea(contour)\n", |
| 56 | + " if(area>300):\n", |
| 57 | + " x,y,w,h = cv2.boundingRect(contour)\t\n", |
| 58 | + " img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)\n", |
| 59 | + " cv2.putText(img,\"RED color\",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))\n", |
| 60 | + "\n", |
| 61 | + "#Tracking the Blue Color\n", |
| 62 | + "(_,contours,hierarchy)=cv2.findContours(blue,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", |
| 63 | + "for pic, contour in enumerate(contours):\n", |
| 64 | + " area = cv2.contourArea(contour)\n", |
| 65 | + " if(area>300):\n", |
| 66 | + " x,y,w,h = cv2.boundingRect(contour)\t\n", |
| 67 | + " img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)\n", |
| 68 | + " cv2.putText(img,\"Blue color\",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,0,0))\n", |
| 69 | + "\n", |
| 70 | + "#Tracking the yellow Color\n", |
| 71 | + "(_,contours,hierarchy)=cv2.findContours(yellow,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", |
| 72 | + "for pic, contour in enumerate(contours):\n", |
| 73 | + " area = cv2.contourArea(contour)\n", |
| 74 | + " if(area>300):\n", |
| 75 | + " x,y,w,h = cv2.boundingRect(contour)\t\n", |
| 76 | + " img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)\n", |
| 77 | + " cv2.putText(img,\"yellow color\",(x,y),cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,0)) \n", |
| 78 | + " \n", |
| 79 | + " \n", |
| 80 | + " #cv2.imshow(\"Redcolour\",red)\n", |
| 81 | + " cv2.imshow(\"Color Tracking\",img)\n", |
| 82 | + " #cv2.imshow(\"red\",res) \t\n", |
| 83 | + " if cv2.waitKey(10) & 0xFF == ord('q'):\n", |
| 84 | + " cap.release()\n", |
| 85 | + " cv2.destroyAllWindows()\n", |
| 86 | + " break \n", |
| 87 | + " " |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "metadata": { |
| 92 | + "kernelspec": { |
| 93 | + "display_name": "Python 3", |
| 94 | + "language": "python", |
| 95 | + "name": "python3" |
| 96 | + }, |
| 97 | + "language_info": { |
| 98 | + "codemirror_mode": { |
| 99 | + "name": "ipython", |
| 100 | + "version": 3 |
| 101 | + }, |
| 102 | + "file_extension": ".py", |
| 103 | + "mimetype": "text/x-python", |
| 104 | + "name": "python", |
| 105 | + "nbconvert_exporter": "python", |
| 106 | + "pygments_lexer": "ipython3", |
| 107 | + "version": "3.8.8" |
| 108 | + } |
| 109 | + }, |
| 110 | + "nbformat": 4, |
| 111 | + "nbformat_minor": 5 |
| 112 | +} |
0 commit comments