├── 1. Image Numpy Basics ├── 00-puppy.jpg ├── 1_Image_Basics.ipynb └── 2_Numpy_and_Image_Assessment.ipynb ├── 2. Image Basics with OpenCV ├── 00-puppy.jpg ├── 1_ImageBasics_OpenCV.ipynb ├── 2_ImageBasics_OpenCV_OpeningImage.ipynb ├── 2_ImageBasics_OpenCV_OpeningImage.py ├── 3_ImageBasics_OpenCV_DrawingOnImage.ipynb ├── 4_ImageBasics_OpenCV_DrawingWithMouse.py ├── 4_ImageBasics_OpenCV_DrawingWithMouse_Multiple_Events.py ├── 4_ImageBasics_OpenCV_DrawingWithMouse_X_Drag.py ├── 5_ImageBasics_OpenCV_Assessment_Part1.ipynb ├── 5_ImageBasics_OpenCV_Assessment_Part2.py ├── Assessment_ImageBasics_OpenCV.gif ├── Puppy_fixed_image.jpg ├── WritingOnImage_Mouse.gif ├── WritingOnImage_Mouse_Drag.gif ├── WritingOnImage_Mouse_Multiple.gif └── dog_backpack.png ├── 3. Image Processing ├── 1_ImageProcessing_ColorSpaces.ipynb ├── 2_ImageProcessing_Blending_Pasting.ipynb ├── 3_ImageProcessing_Thresholding.ipynb ├── 4_ImageProcessing_BlurringSmoothing.ipynb ├── 5_ImageProcessing_MorphologicalOperators.ipynb ├── 6_ImageProcessing_Gradients.ipynb ├── 7_ImageProcessing_Histograms_Equalization.ipynb ├── 8_ImageProcessing_Assessment.ipynb ├── bricks.jpg ├── crossword.jpg ├── giraffes.jpg ├── gorilla.jpg ├── horse.jpg ├── rainbow.jpg ├── sammy.jpg ├── sammy_noise.jpg └── sudoku.jpg ├── 4. Videos with Python and Open CV ├── 1_Video_OpenCV_ConenctingtoCamera.ipynb ├── 2_Video_OpenCV_UsingVideoFiles.ipynb ├── 3_Video_OpenCV_DrawingonVideo.ipynb └── 4_Video_OpenCV_Assessment.ipynb ├── 5. Object Detection with OpenCV and Python ├── 1_ObjectDetection_OpenCV_Introduction_Template_Matching.ipynb ├── 2_ObjectDetection_OpenCV_CornerDetection.ipynb ├── 3_ObjectDetection_OpenCV_EdgeDetection.ipynb ├── 4_ObjectDetection_OpenCV_GridDetection.ipynb ├── 5_ObjectDetection_OpenCV_ContourDetection.ipynb ├── 6_ObjectDetection_OpenCV_FeatureMatching.ipynb ├── 7_ObjectDetection_OpenCV_WaterShedAlgorithm.ipynb ├── 8_ObjectDetection_OpenCV_CustomSeeds_WatershedAlgorithm.py ├── 9_ObjectDetection_OpenCV_FaceDetection_CameraCapture.py ├── 9_ObjectDetection_OpenCV_FaceDetection_CameraCapture_All3.py ├── 9_ObjectDetection_OpenCV_FaceDetection_Image.ipynb ├── Denis_Mukwege.jpg ├── Nadia_Murad.jpg ├── dot_grid.png ├── flat_chessboard.png ├── haarcascade_eye.xml ├── haarcascade_frontalface_default.xml ├── many_cereals.jpg ├── pennies.jpg ├── real_chessboard.jpg ├── reeses_puffs.png ├── road_image.jpg ├── sammy.jpg └── solvay_conference.jpg ├── 6. Object Tracking ├── 1_ObjectTracking_Introduction.ipynb ├── 2_ObjectTracking_GunnerFarneBack.py ├── 3_ObjectTracking_Introduction_LucasKanade.py ├── 4_ObjectTracking_MeanShift_CamShift_1_Theory.ipynb ├── 4_ObjectTracking_MeanShift_CamShift_2_CamShift.py └── 4_ObjectTracking_MeanShift_CamShift_2_MeanShift.py ├── 7. Deep Learning CNN ├── 1_DeepLearning_ConvolutionNeuralNetworks.ipynb ├── 2_DeepLearning_ConvolutionNeuralNetworks_CIFAR10.ipynb └── 3_DeepLearning_ConvolutionNeuralNetworks_CATSDOGS.ipynb ├── 8. Captsone ├── Capstone_Part_1_VariablesBackgroundFunction.py ├── Capstone_Part_2_Segmentation.py ├── Capstone_Part_3_Counting_ConvexHull.py ├── Capstone_Part_4_Final.py └── Readme.md ├── README.md └── images ├── FaceDetection1.gif ├── FaceDetection2.gif ├── FaceDetection3.gif ├── Readme.md └── Watershed1.gif /1. Image Numpy Basics/00-puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/1. Image Numpy Basics/00-puppy.jpg -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/00-puppy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/00-puppy.jpg -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/2_ImageBasics_OpenCV_OpeningImage.py: -------------------------------------------------------------------------------- 1 | #cv2.imshow works in Jupyter notebook but not in Colab 2 | import cv2 3 | 4 | img = cv2.imread('00-puppy.jpg') 5 | 6 | while True: 7 | cv2.imshow('Puppy',img) 8 | if cv2.waitKey(1) & 0xFF == 27: # 0xFF is a hexadecimal constant which is 1111111 9 | break 10 | 11 | # if we waited at least 1ms AND we have pressed Esc (27) / ord(q) - Q key 12 | 13 | cv2.destroyAllWindows() 14 | -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/4_ImageBasics_OpenCV_DrawingWithMouse.py: -------------------------------------------------------------------------------- 1 | # Here we will use callbacks to conenct iamges to event functions with OpenCV (allows us to directly interact with images) 2 | 3 | # Connecting Callback functions 4 | # Adding Functionality through Event Choices 5 | # Dragging mouse for functionality 6 | # connect function to drawing 7 | 8 | import cv2 9 | import numpy as np 10 | 11 | ############### 12 | ## FUNCTION ## 13 | ############### 14 | 15 | def draw_circle(event,x,y,flags,param): #these parameters are called back automatically with MousecallBack 16 | #pass #automatically filled 17 | if event == cv2.EVENT_LBUTTONDOWN: 18 | cv2.circle(img,(x,y),50,(0,255,0),-1) 19 | 20 | cv2.namedWindow(winname='My_Drawing') 21 | cv2.setMouseCallback('My_Drawing',draw_circle) 22 | 23 | ################################ 24 | ## SHOWING IMAGE WITH OPENCV ### 25 | ################################ 26 | 27 | img = np.zeros((512,512,3),np.int8) #int8 gives a bit gray background 28 | 29 | while True: 30 | cv2.imshow('My_Drawing',img) 31 | if cv2.waitKey(20) & 0xFF == 27: 32 | break 33 | 34 | cv2.destroyAllWindows() 35 | -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/4_ImageBasics_OpenCV_DrawingWithMouse_Multiple_Events.py: -------------------------------------------------------------------------------- 1 | # Here we will use callbacks to conenct images to event functions with OpenCV (allows us to directly interact with images) 2 | 3 | # Connecting Callback functions 4 | # Adding Functionality through Event Choices 5 | # Dragging mouse for functionality 6 | # connect function to drawing 7 | 8 | import cv2 9 | import numpy as np 10 | 11 | ############### 12 | ## FUNCTION ## 13 | ############### 14 | 15 | def draw_circle(event,x,y,flags,param): #these parameters are called back automatically with MousecallBack 16 | #pass #automatically filled 17 | if event == cv2.EVENT_LBUTTONDOWN: 18 | cv2.circle(img,(x,y),50,(0,255,0),-1) 19 | #for mulitple events 20 | elif event == cv2.EVENT_RBUTTONDOWN: 21 | cv2.circle(img,(x,y),50,(255,0,0),-1) 22 | 23 | cv2.namedWindow(winname='My_Drawing') 24 | cv2.setMouseCallback('My_Drawing',draw_circle) 25 | 26 | ################################ 27 | ## SHOWING IMAGE WITH OPENCV ### 28 | ################################ 29 | 30 | img = np.zeros((512,512,3)) #int8 gives a bit gray background 31 | 32 | while True: 33 | cv2.imshow('My_Drawing',img) 34 | if cv2.waitKey(20) & 0xFF == 27: 35 | break 36 | 37 | cv2.destroyAllWindows() 38 | -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/4_ImageBasics_OpenCV_DrawingWithMouse_X_Drag.py: -------------------------------------------------------------------------------- 1 | # Here we will use callbacks to conenct iamges to event functions with OpenCV (allows us to directly interact with images) 2 | 3 | # Connecting Callback functions 4 | # Adding Functionality through Event Choices 5 | # Dragging mouse for functionality 6 | # connect function to drawing 7 | 8 | import cv2 9 | import numpy as np 10 | 11 | ############### 12 | ## VARIABLES ## 13 | ############### 14 | 15 | drawing = False #Will be True if the mouse has been pressed down 16 | ix = -1 17 | iy = -1 18 | 19 | ############### 20 | ## FUNCTION ## 21 | ############### 22 | 23 | def draw_rectangle(event,x,y,flags,params): #these parameters are called back automatically with MousecallBack 24 | global ix,iy,drawing 25 | if event == cv2.EVENT_LBUTTONDOWN: #for starting drawing rectangle 26 | drawing = True 27 | ix,iy = (x,y) 28 | elif event == cv2.EVENT_MOUSEMOVE: #for dragging to keep the shape of rectangle 29 | if drawing == True: 30 | cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) #ix,iy starting point and dragging point, x,y are where current position is 31 | 32 | elif event == cv2.EVENT_LBUTTONUP: #to finish drawing final rectangle 33 | drawing = False 34 | cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) 35 | 36 | ################################ 37 | ## SHOWING IMAGE WITH OPENCV ### 38 | ################################ 39 | 40 | img = np.zeros((512,512,3)) #int8 gives a bit gray background 41 | 42 | cv2.namedWindow(winname='My_Drawing') 43 | cv2.setMouseCallback('My_Drawing',draw_rectangle) 44 | 45 | while True: 46 | cv2.imshow('My_Drawing',img) 47 | if cv2.waitKey(1) & 0xFF == 27: 48 | break 49 | 50 | cv2.destroyAllWindows() 51 | -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/5_ImageBasics_OpenCV_Assessment_Part2.py: -------------------------------------------------------------------------------- 1 | #TASK: (NOTE: YOU WILL NEED TO RUN THIS AS A SCRIPT). 2 | # 3 | # Create a script that opens the picture and allows you to draw empty red circles whever you click the RIGHT MOUSE BUTTON DOWN. 4 | 5 | #import libraries 6 | import cv2 7 | import numpy as np 8 | 9 | ############### 10 | ## FUNCTION ## 11 | ############### 12 | 13 | def draw_circle(event,x,y,flags,param): 14 | if event == cv2.EVENT_RBUTTONDOWN: 15 | cv2.circle(fix_img,(x,y),50,(0,0,255),thickness=10) 16 | 17 | # Connects the mouse button to our callback function 18 | cv2.namedWindow(winname='Dog_backpack') 19 | cv2.setMouseCallback('Dog_backpack',draw_circle) 20 | 21 | 22 | #Runs forever until we break with Esc key on keyboard 23 | # Shows the image window 24 | # EXPLANATION FOR THIS LINE OF CODE: 25 | # https://stackoverflow.com/questions/35372700/whats-0xff-for-in-cv2-waitkey1/39201163 26 | 27 | 28 | ################################ 29 | ## SHOWING IMAGE WITH OPENCV ### 30 | ################################ 31 | 32 | img = cv2.imread('dog_backpack.png') 33 | fix_img = cv2.resize(img,(0,0),img,0.5,0.5) 34 | 35 | while True: 36 | cv2.imshow('Dog_backpack',fix_img) 37 | if cv2.waitKey(20) & 0xFF == 27: 38 | break 39 | 40 | # Once script is done, its usually good practice to call this line 41 | # It closes all windows (just in case you have multiple windows called) 42 | cv2.destroyAllWindows() 43 | -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/Assessment_ImageBasics_OpenCV.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/Assessment_ImageBasics_OpenCV.gif -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/Puppy_fixed_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/Puppy_fixed_image.jpg -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/WritingOnImage_Mouse.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/WritingOnImage_Mouse.gif -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/WritingOnImage_Mouse_Drag.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/WritingOnImage_Mouse_Drag.gif -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/WritingOnImage_Mouse_Multiple.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/WritingOnImage_Mouse_Multiple.gif -------------------------------------------------------------------------------- /2. Image Basics with OpenCV/dog_backpack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/2. Image Basics with OpenCV/dog_backpack.png -------------------------------------------------------------------------------- /3. Image Processing/bricks.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/bricks.jpg -------------------------------------------------------------------------------- /3. Image Processing/crossword.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/crossword.jpg -------------------------------------------------------------------------------- /3. Image Processing/giraffes.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/giraffes.jpg -------------------------------------------------------------------------------- /3. Image Processing/gorilla.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/gorilla.jpg -------------------------------------------------------------------------------- /3. Image Processing/horse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/horse.jpg -------------------------------------------------------------------------------- /3. Image Processing/rainbow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/rainbow.jpg -------------------------------------------------------------------------------- /3. Image Processing/sammy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/sammy.jpg -------------------------------------------------------------------------------- /3. Image Processing/sammy_noise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/sammy_noise.jpg -------------------------------------------------------------------------------- /3. Image Processing/sudoku.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/3. Image Processing/sudoku.jpg -------------------------------------------------------------------------------- /4. Videos with Python and Open CV/1_Video_OpenCV_ConenctingtoCamera.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.3" 21 | }, 22 | "colab": { 23 | "name": "1_Video_OpenCV_ConenctingtoCamera.ipynb", 24 | "provenance": [], 25 | "toc_visible": true, 26 | "include_colab_link": true 27 | } 28 | }, 29 | "cells": [ 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "id": "view-in-github", 34 | "colab_type": "text" 35 | }, 36 | "source": [ 37 | "\"Open" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": { 43 | "id": "l6xKhnZ0XOLW" 44 | }, 45 | "source": [ 46 | "#### Video Basics with Python and OpenCV (Connecting to Camera)\n", 47 | "\n", 48 | "In this program we will execute following things:\n", 49 | " - Use OpenCV to connect to webcam\n", 50 | " - Write a video stream to a file" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "metadata": { 56 | "id": "iAI96II7XOLW" 57 | }, 58 | "source": [ 59 | "# close all other programs that are accessing opencv to connect to camera and only use 1 kernel in usew\n", 60 | "\n", 61 | "import cv2\n", 62 | "\n", 63 | "cap = cv2.VideoCapture(0) #0 default camera\n", 64 | "\n", 65 | "#width and hieght of actual capture\n", 66 | "\n", 67 | "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #return like 1080.0\n", 68 | "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", 69 | "\n", 70 | "while True:\n", 71 | " \n", 72 | " ret,frame =cap.read() #tuple unpacking\n", 73 | " \n", 74 | " gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #covnert iamge to grayscale\n", 75 | " \n", 76 | " cv2.imshow('frame',gray)\n", 77 | " \n", 78 | " if cv2.waitKey(1) & 0xFF == 27: #ESC key\n", 79 | " break\n", 80 | " \n", 81 | "cap.release()\n", 82 | "cv2.destroyAllWindows()\n", 83 | " " 84 | ], 85 | "execution_count": null, 86 | "outputs": [] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "metadata": { 91 | "id": "YddSSRxJXOLY" 92 | }, 93 | "source": [ 94 | "# for raw frame\n", 95 | "\n", 96 | "import cv2\n", 97 | "\n", 98 | "cap = cv2.VideoCapture(0) #0 default camera\n", 99 | "\n", 100 | "#width and hieght of actual capture\n", 101 | "\n", 102 | "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #return like 1080.0\n", 103 | "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", 104 | "\n", 105 | "while True:\n", 106 | " \n", 107 | " ret,frame =cap.read() #tuple unpacking\n", 108 | " \n", 109 | " #gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #covnert iamge to grayscale\n", 110 | " \n", 111 | " cv2.imshow('frame',frame)\n", 112 | " \n", 113 | " if cv2.waitKey(1) & 0xFF == 27: #ESC key\n", 114 | " break\n", 115 | " \n", 116 | "cap.release()\n", 117 | "cv2.destroyAllWindows()\n", 118 | " " 119 | ], 120 | "execution_count": null, 121 | "outputs": [] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "metadata": { 126 | "id": "Lq1nm2qqXOLY" 127 | }, 128 | "source": [ 129 | "#WRITE VIDEO STREAM TO FILE\n", 130 | "\n", 131 | "# for raw frame and writing video to file\n", 132 | "\n", 133 | "import cv2\n", 134 | "\n", 135 | "cap = cv2.VideoCapture(0) #0 default camera\n", 136 | "\n", 137 | "#width and hieght of actual capture\n", 138 | "\n", 139 | "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) #return like 1080.0\n", 140 | "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", 141 | "\n", 142 | "##########################\n", 143 | "'''\n", 144 | "\n", 145 | "Fourcc is a 4 byte code to specify the video codec.\n", 146 | "For Windows: DIVX\n", 147 | "\n", 148 | "'''\n", 149 | "#########################\n", 150 | "\n", 151 | "writer = cv2.VideoWriter('MyFirstVideoWrite.mp4',\n", 152 | " cv2.VideoWriter_fourcc(*'DIVX'),\n", 153 | " 20, #frames per seconds\n", 154 | " (width,height))\n", 155 | "\n", 156 | "\n", 157 | "while True:\n", 158 | " \n", 159 | " ret,frame =cap.read() #tuple unpacking\n", 160 | " \n", 161 | " #Operations (drawing)\n", 162 | " writer.write(frame)\n", 163 | " \n", 164 | " cv2.imshow('frame',frame)\n", 165 | " \n", 166 | " if cv2.waitKey(1) & 0xFF == 27: #ESC key\n", 167 | " break\n", 168 | " \n", 169 | "cap.release()\n", 170 | "writer.release()\n", 171 | "cv2.destroyAllWindows()\n", 172 | " " 173 | ], 174 | "execution_count": null, 175 | "outputs": [] 176 | } 177 | ] 178 | } -------------------------------------------------------------------------------- /4. Videos with Python and Open CV/2_Video_OpenCV_UsingVideoFiles.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.3" 21 | }, 22 | "colab": { 23 | "name": "2_Video_OpenCV_UsingVideoFiles.ipynb", 24 | "provenance": [], 25 | "include_colab_link": true 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "view-in-github", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "\"Open" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "id": "X-XGHsOwXPB3" 43 | }, 44 | "source": [ 45 | "#### Video Basics with Python and OpenCV (Using video files)\n", 46 | "\n", 47 | "In this program we will execute following things:\n", 48 | " - Open existing video files with OpenCV" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "metadata": { 54 | "id": "q0FgdkDzXPB4" 55 | }, 56 | "source": [ 57 | "import cv2\n", 58 | "\n", 59 | "cap = cv2.VideoCapture('MyFirstVideoWrite.mp4')\n", 60 | "\n", 61 | "if cap.isOpened() == False:\n", 62 | " print('Error: File not found or WRONG Codec Used !!')\n", 63 | "\n", 64 | "while cap.isOpened():\n", 65 | " ret,frame = cap.read()\n", 66 | " \n", 67 | " if ret == True: #video still playing\n", 68 | " cv2.imshow('frame',frame)\n", 69 | " if cv2.waitKey(10) & 0xFF == 27:\n", 70 | " break\n", 71 | " \n", 72 | " else: #when youa re done showing everything, break from while video, exit from the video\n", 73 | " break\n", 74 | "\n", 75 | "cap.release()\n", 76 | "cv2.destroyAllWindows()\n", 77 | " " 78 | ], 79 | "execution_count": null, 80 | "outputs": [] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "metadata": { 85 | "id": "fE88OlwjXPB4" 86 | }, 87 | "source": [ 88 | "#above code video is displayed very fast\n", 89 | "\n", 90 | "# to control the speed\n", 91 | "\n", 92 | "import cv2\n", 93 | "import time\n", 94 | "\n", 95 | "cap = cv2.VideoCapture('MyFirstVideoWrite.mp4')\n", 96 | "\n", 97 | "if cap.isOpened() == False:\n", 98 | " print('Error: File not found or WRONG Codec Used !!')\n", 99 | "\n", 100 | "while cap.isOpened():\n", 101 | " ret,frame = cap.read()\n", 102 | " \n", 103 | " if ret == True: #video still playing \n", 104 | " \n", 105 | " #with 20fps\n", 106 | " time.sleep(1/20) #1/fps\n", 107 | " \n", 108 | " cv2.imshow('frame',frame)\n", 109 | " if cv2.waitKey(10) & 0xFF == 27:\n", 110 | " break\n", 111 | " \n", 112 | " else: #when youa re done showing everything, break from while video, exit from the video\n", 113 | " break\n", 114 | "\n", 115 | "cap.release()\n", 116 | "cv2.destroyAllWindows()" 117 | ], 118 | "execution_count": null, 119 | "outputs": [] 120 | } 121 | ] 122 | } -------------------------------------------------------------------------------- /4. Videos with Python and Open CV/3_Video_OpenCV_DrawingonVideo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.3" 21 | }, 22 | "colab": { 23 | "name": "3_Video_OpenCV_DrawingonVideo.ipynb", 24 | "provenance": [], 25 | "include_colab_link": true 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "view-in-github", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "\"Open" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "id": "aqjeCQrKXQpw" 43 | }, 44 | "source": [ 45 | "#### Video Basics with Python and OpenCV (Using video files)\n", 46 | "\n", 47 | "In this program we will execute following things:\n", 48 | " - Draw on video" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "metadata": { 54 | "id": "Y4OXAWhqXQpw" 55 | }, 56 | "source": [ 57 | "import cv2\n", 58 | "\n", 59 | "cap = cv2.VideoCapture(0)\n", 60 | "\n", 61 | "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n", 62 | "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", 63 | "\n", 64 | "#to draw on video\n", 65 | "\n", 66 | "#For top left corner\n", 67 | "x = width // 2\n", 68 | "y = height // 2 \n", 69 | "# // means we want to keep x and y coordinates as integers\n", 70 | "\n", 71 | "#for the rectangle we want to draw\n", 72 | "\n", 73 | "w = width // 4\n", 74 | "h = height // 4\n", 75 | "#Bottom right x+w,y+h\n", 76 | "\n", 77 | "while True:\n", 78 | " ret,frame = cap.read()\n", 79 | " cv2.rectangle(frame,\n", 80 | " (x,y),\n", 81 | " (x+w,y+h),color=(0,0,255),\n", 82 | " thickness=4)\n", 83 | " cv2.imshow('frame',frame)\n", 84 | " \n", 85 | " if cv2.waitKey(1) & 0xFF == 27:\n", 86 | " break\n", 87 | "\n", 88 | "cap.release()\n", 89 | "cv2.destroyAllWindows()" 90 | ], 91 | "execution_count": null, 92 | "outputs": [] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "metadata": { 97 | "id": "o2H0szvNXQpx" 98 | }, 99 | "source": [ 100 | "#interactive draw on video\n", 101 | "\n", 102 | "import cv2\n", 103 | "\n", 104 | "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n", 105 | "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n", 106 | "\n", 107 | "#### CALLBACK FUNCTION ####\n", 108 | "\n", 109 | "def draw_rectangle(event,x,y,flags,param):\n", 110 | " \n", 111 | " global pt1,pt2,topLeft_clicked,botRight_clicked\n", 112 | " \n", 113 | " if event == cv2.EVENT_LBUTTONDOWN:\n", 114 | " \n", 115 | " #RESET the rectangle (erase previous drawn rectangle)\n", 116 | " if topLeft_clicked == True and botRight_clicked == True:\n", 117 | " pt1 = (0,0) #top left\n", 118 | " pt2 = (0,0) #bottom right\n", 119 | " topLeft_clicked = False\n", 120 | " botRight_clicked = False\n", 121 | " \n", 122 | " if topLeft_clicked == False:\n", 123 | " pt1 = (x,y)\n", 124 | " topLeft_clicked = True\n", 125 | " \n", 126 | " elif botRight_clicked == False:\n", 127 | " pt2 = (x,y)\n", 128 | " botRight_clicked = True\n", 129 | "\n", 130 | "# GLOBAL VARIABLES\n", 131 | "\n", 132 | "pt1 = (0,0) #top left\n", 133 | "pt2 = (0,0) #bottom right\n", 134 | "topLeft_clicked = False\n", 135 | "botRight_clicked = False\n", 136 | " \n", 137 | "#CONNECT TO CALLBACK\n", 138 | "cap = cv2.VideoCapture(0)\n", 139 | "\n", 140 | "cv2.namedWindow('Test')\n", 141 | "cv2.setMouseCallback('Test',draw_rectangle)\n", 142 | "\n", 143 | "\n", 144 | "while True:\n", 145 | " ret,frame = cap.read()\n", 146 | "\n", 147 | " #### DRAWING ON FRAME BASED OFF GLOBAL VARIABLES\n", 148 | " \n", 149 | " if topLeft_clicked: #if top left is clicked\n", 150 | " cv2.circle(frame,center=pt1,radius=5,color=(0,0,255),thickness=-1)\n", 151 | " if topLeft_clicked and botRight_clicked: #if both are clicked\n", 152 | " cv2.rectangle(frame,pt1,pt2,(0,0,255),3)\n", 153 | " \n", 154 | " cv2.imshow('Test',frame)\n", 155 | " \n", 156 | " if cv2.waitKey(1) & 0xFF == 27:\n", 157 | " break\n", 158 | "\n", 159 | "cap.release()\n", 160 | "cv2.destroyAllWindows()" 161 | ], 162 | "execution_count": null, 163 | "outputs": [] 164 | }, 165 | { 166 | "cell_type": "code", 167 | "metadata": { 168 | "id": "xPO5l7g4XQpx" 169 | }, 170 | "source": [ 171 | "" 172 | ], 173 | "execution_count": null, 174 | "outputs": [] 175 | } 176 | ] 177 | } -------------------------------------------------------------------------------- /4. Videos with Python and Open CV/4_Video_OpenCV_Assessment.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "kernelspec": { 6 | "display_name": "Python 3", 7 | "language": "python", 8 | "name": "python3" 9 | }, 10 | "language_info": { 11 | "codemirror_mode": { 12 | "name": "ipython", 13 | "version": 3 14 | }, 15 | "file_extension": ".py", 16 | "mimetype": "text/x-python", 17 | "name": "python", 18 | "nbconvert_exporter": "python", 19 | "pygments_lexer": "ipython3", 20 | "version": "3.7.3" 21 | }, 22 | "colab": { 23 | "name": "4_Video_OpenCV_Assessment.ipynb", 24 | "provenance": [], 25 | "include_colab_link": true 26 | } 27 | }, 28 | "cells": [ 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "view-in-github", 33 | "colab_type": "text" 34 | }, 35 | "source": [ 36 | "\"Open" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "id": "O5K8vfjlXRi9" 43 | }, 44 | "source": [ 45 | "\n", 46 | "\n", 47 | "Copyright Pierian Data Inc." 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "id": "ngp7vFrCXRi9" 54 | }, 55 | "source": [ 56 | "# Video Basics Assessment \n", 57 | "\n", 58 | "* **Note: This assessment is quite hard! Feel free to treat it as a code along and jump to the solutions** *\n", 59 | "\n", 60 | "## Project Task\n", 61 | "\n", 62 | "**You only have one task here. Create a program that reads in a live stream from a camera on your computer (or if you don't have a camera, just open up a video file). Then whenever you click the left mouse button down, create a blue circle around where you've clicked. Check out the video for an example of what the final project should look like**" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": { 68 | "id": "FTQ7qRgbXRi9" 69 | }, 70 | "source": [ 71 | "**Guide**\n", 72 | "\n", 73 | "* Create a draw_circle function for the callback function\n", 74 | "* Use two events cv2.EVENT_LBUTTONDOWN and cv2.EVENT_LBUTTONUP\n", 75 | "* Use a boolean variable to keep track if the mouse has been clicked up and down based on the events above\n", 76 | "* Use a tuple to keep track of the x and y where the mouse was clicked.\n", 77 | "* You should be able to then draw a circle on the frame based on the x,y coordinates from the Event \n", 78 | "\n", 79 | "Check out the skeleton guide below:" 80 | ] 81 | }, 82 | { 83 | "cell_type": "code", 84 | "metadata": { 85 | "id": "WYO_34O9XRi9" 86 | }, 87 | "source": [ 88 | "import cv2\n", 89 | "\n", 90 | "# Create a function based on a CV2 Event (Left button click)\n", 91 | "\n", 92 | "# mouse callback function\n", 93 | "def draw_circle(event,x,y,flags,param):\n", 94 | "\n", 95 | " global center,clicked\n", 96 | "\n", 97 | " # get mouse click on down and track center\n", 98 | " \n", 99 | " if event == cv2.EVENT_LBUTTONDOWN:\n", 100 | " center = (x,y)\n", 101 | " clicked = False\n", 102 | " \n", 103 | " if event == cv2.EVENT_LBUTTONUP:\n", 104 | " clicked = True\n", 105 | " \n", 106 | " \n", 107 | " # Use boolean variable to track if the mouse has been released\n", 108 | " \n", 109 | "\n", 110 | " \n", 111 | "# Haven't drawn anything yet!\n", 112 | "center = (0,0)\n", 113 | "clicked = False\n", 114 | "\n", 115 | "\n", 116 | "# Capture Video\n", 117 | "\n", 118 | "cap = cv2.VideoCapture(0)\n", 119 | "\n", 120 | "# Create a named window for connections\n", 121 | "\n", 122 | "cv2.namedWindow('Test')\n", 123 | "\n", 124 | "# Bind draw_circle function to mouse cliks\n", 125 | "\n", 126 | "cv2.setMouseCallback('Test',draw_circle)\n", 127 | "\n", 128 | "\n", 129 | "while True:\n", 130 | " # Capture frame-by-frame\n", 131 | " \n", 132 | " ret,frame = cap.read()\n", 133 | "\n", 134 | " # Use if statement to see if clicked is true\n", 135 | " \n", 136 | " if clicked:\n", 137 | " # Draw circle on frame\n", 138 | " cv2.circle(frame,center=center,\n", 139 | " radius=50,\n", 140 | " color=(255,0,0),\n", 141 | " thickness = 5)\n", 142 | " \n", 143 | " # Display the resulting frame\n", 144 | " \n", 145 | " cv2.imshow('Test',frame)\n", 146 | "\n", 147 | " # This command let's us quit with the \"q\" button on a keyboard.\n", 148 | " # Simply pressing X on the window won't work!\n", 149 | " if cv2.waitKey(1) & 0xFF == 27:\n", 150 | " break\n", 151 | "\n", 152 | "# When everything is done, release the capture\n", 153 | "cap.release()\n", 154 | "cv2.destroyAllWindows()" 155 | ], 156 | "execution_count": null, 157 | "outputs": [] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "metadata": { 162 | "id": "BMxbZzozXRi-" 163 | }, 164 | "source": [ 165 | "" 166 | ], 167 | "execution_count": null, 168 | "outputs": [] 169 | } 170 | ] 171 | } -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/8_ObjectDetection_OpenCV_CustomSeeds_WatershedAlgorithm.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | road = cv2.imread('road_image.jpg') 6 | 7 | scale_percent = 100 # percent of original size 8 | width = int(road.shape[1] * scale_percent / 100) 9 | height = int(road.shape[0] * scale_percent / 100) 10 | dim = (width, height) 11 | # resize image 12 | road = cv2.resize(road, dim, interpolation = cv2.INTER_AREA) 13 | road_copy = np.copy(road) 14 | 15 | #set marker image 16 | marker_image = np.zeros(road.shape[:2],dtype=np.int32) 17 | segments = np.zeros(road.shape,dtype=np.uint8) 18 | 19 | from matplotlib import cm 20 | 21 | #we want this to be tuple 22 | 23 | def create_rgb(i): 24 | return tuple(np.array(cm.tab10(i)[:3])*255) 25 | 26 | #defining its RGB colors 27 | colors=[] 28 | for i in range(10): 29 | colors.append(create_rgb(i)) 30 | 31 | ###################### 32 | ## GLOBAL VARIABLES ## 33 | n_markers = 10 #0-9 34 | current_marker=1 #color choice 35 | 36 | marks_updated = False #markers updated by Watershed 37 | 38 | ###################### 39 | ## CALLBACK FUNCTION ## 40 | 41 | def mouse_callback(event,x,y,flags,param): 42 | global marks_updated 43 | 44 | if event == cv2.EVENT_LBUTTONDOWN: 45 | #Markers paased to watershed algo 46 | cv2.circle(marker_image,(x,y),2,(current_marker),-1) 47 | #user sees on image 48 | cv2.circle(road_copy,(x,y),2,colors[current_marker],-1) 49 | 50 | marks_updated = True 51 | 52 | ###################### 53 | ## WHILE TRUE ## 54 | 55 | cv2.namedWindow('Road Image') 56 | cv2.setMouseCallback('Road Image',mouse_callback) 57 | 58 | while True: 59 | cv2.imshow('Watershed Segments',segments) 60 | cv2.imshow('Road Image',road_copy) 61 | 62 | #Close all windows 63 | 64 | k = cv2.waitKey(1) 65 | if k == 27: 66 | break 67 | 68 | #Clearing all colors (USER PRESSES C KEY) 69 | 70 | elif k == ord('c'): 71 | road_copy = road.copy() 72 | marker_image = np.zeros(road.shape[:2],dtype=np.int32) 73 | segments = np.zeros(road.shape,dtype=np.uint8) 74 | 75 | #update color choice 76 | 77 | elif k > 0 and chr(k).isdigit(): 78 | current_marker = int(chr(k)) #check current marker 79 | 80 | # Update markings 81 | 82 | if marks_updated: 83 | marker_image_copy = marker_image.copy() 84 | cv2.watershed(road,marker_image_copy) 85 | 86 | segments = np.zeros(road.shape,dtype=np.uint8) 87 | 88 | for color_ind in range(n_markers): 89 | segments[marker_image_copy == (color_ind)] = colors[color_ind] 90 | 91 | cv2.destroyAllWindows() 92 | -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/9_ObjectDetection_OpenCV_FaceDetection_CameraCapture.py: -------------------------------------------------------------------------------- 1 | #Writing a program to detect face from live camera feed 2 | 3 | #Import Libraries 4 | import cv2 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | 8 | #upload 3 images and in gray scale 9 | nadia = cv2.imread('Nadia_Murad.jpg',0) 10 | denis = cv2.imread('Denis_Mukwege.jpg',0) 11 | solvay = cv2.imread('solvay_conference.jpg',0) 12 | 13 | # Pre-trained XML Haarcascade files 14 | 15 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 16 | eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') 17 | 18 | # Functions to detect face and eyes 19 | def detect_face(img): 20 | 21 | face_img = img.copy() 22 | face_rects = face_cascade.detectMultiScale(face_img) 23 | 24 | for (x,y,w,h) in face_rects: 25 | cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,255,255),10) 26 | 27 | return face_img 28 | 29 | def detect_eyes(img): 30 | 31 | face_img = img.copy() 32 | eyes_rects = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2,minNeighbors=5) 33 | 34 | for (x,y,w,h) in eyes_rects: 35 | cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,255,255),10) 36 | 37 | return face_img 38 | 39 | # Real live camera feed and detection 40 | 41 | cap = cv2.VideoCapture(0) 42 | 43 | while True: 44 | ret,frame = cap.read(0) 45 | frame = detect_face(frame) #change detect_eyes for eyes detection 46 | 47 | cv2.imshow('Video Face Detect',frame) 48 | 49 | k = cv2.waitKey(1) 50 | if k == 27: 51 | break 52 | 53 | cap.release() 54 | cv2.destroyAllWindows() 55 | -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/9_ObjectDetection_OpenCV_FaceDetection_CameraCapture_All3.py: -------------------------------------------------------------------------------- 1 | #Writing a program to detect face from live camera feed 2 | 3 | #Import Libraries 4 | import cv2 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | 8 | #upload 3 images and in gray scale 9 | nadia = cv2.imread('Nadia_Murad.jpg',0) 10 | denis = cv2.imread('Denis_Mukwege.jpg',0) 11 | solvay = cv2.imread('solvay_conference.jpg',0) 12 | 13 | # Pre-trained XML Haarcascade files 14 | 15 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 16 | eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') 17 | smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml') 18 | # Functions to detect face and eyes 19 | def detect(img): 20 | 21 | face_img = img.copy() 22 | face_rects = face_cascade.detectMultiScale(face_img) 23 | 24 | for (x,y,w,h) in face_rects: 25 | cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,130,0),10) 26 | 27 | eyes_rects = eye_cascade.detectMultiScale(face_img,scaleFactor=1.2,minNeighbors=5) 28 | 29 | for (x,y,w,h) in eyes_rects: 30 | cv2.rectangle(face_img,(x,y),(x+w,y+h),(0,180,60),10) 31 | 32 | smile_rects = smile_cascade.detectMultiScale(face_img,1.7,20) 33 | 34 | for (x,y,w,h) in smile_rects: 35 | cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,0,130),10) 36 | 37 | return face_img 38 | # Real live camera feed and detection 39 | 40 | cap = cv2.VideoCapture(0) 41 | 42 | while True: 43 | ret,frame = cap.read(0) 44 | frame = detect(frame) #change detect_eyes for eyes detection 45 | 46 | cv2.imshow('Video Face Detect',frame) 47 | 48 | k = cv2.waitKey(1) 49 | if k == 27: 50 | break 51 | 52 | cap.release() 53 | cv2.destroyAllWindows() 54 | -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/Denis_Mukwege.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/Denis_Mukwege.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/Nadia_Murad.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/Nadia_Murad.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/dot_grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/dot_grid.png -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/flat_chessboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/flat_chessboard.png -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/many_cereals.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/many_cereals.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/pennies.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/pennies.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/real_chessboard.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/real_chessboard.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/reeses_puffs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/reeses_puffs.png -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/road_image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/road_image.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/sammy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/sammy.jpg -------------------------------------------------------------------------------- /5. Object Detection with OpenCV and Python/solvay_conference.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/5. Object Detection with OpenCV and Python/solvay_conference.jpg -------------------------------------------------------------------------------- /6. Object Tracking/1_ObjectTracking_Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "1_ObjectTracking_Introduction.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "authorship_tag": "ABX9TyP8vFes5mrlOmMsqhBSeUp2", 10 | "include_colab_link": true 11 | }, 12 | "kernelspec": { 13 | "name": "python3", 14 | "display_name": "Python 3" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "view-in-github", 22 | "colab_type": "text" 23 | }, 24 | "source": [ 25 | "\"Open" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": { 31 | "id": "aJ3fGAmciSyq" 32 | }, 33 | "source": [ 34 | "### Object Tracking\r\n", 35 | "\r\n", 36 | "In this section, we will cover:\r\n", 37 | "\r\n", 38 | "- Basic Object Tracking Techniques\r\n", 39 | " - Optical Flow\r\n", 40 | " - Meanshift and Camshift\r\n", 41 | "- Advanced Tracking\r\n", 42 | " - Built-In Tracking APIs\r\n", 43 | "\r\n", 44 | "#### Optical Flow\r\n", 45 | "\r\n", 46 | "- It is defined ss the pattern of apparent motion of image objects between 2 consecutive frames caused by the movement of object or camera.\r\n", 47 | "- Assumptions: \r\n", 48 | " - Pixel intensities of object between consecutive frames DONOT CHANGWE \r\n", 49 | " - Neighbouring pixels have similar motion\r\n", 50 | "- Methods:\r\n", 51 | " - Take given set of points and a frame.\r\n", 52 | " - Attempt to find those points in next frame.\r\n", 53 | " - Upto user to supply the points to track.\r\n", 54 | "- We need to also see that in which direction object is moving or is it the camera which is moving.\r\n", 55 | "- General way to track a object: We will pass previous frame, previous points and current points to the Lucas Kanade function.\r\n", 56 | "- The function then attempts to locate points in the current frame.\r\n", 57 | "- Lucas Kanade computes optical flow for a sparse feature set i.e. only points that it was told to track.\r\n", 58 | " - But to track all points in the video, we have to use Gunner Farneback's algorithm that computes dense optical flow . It will color them black if no flow is detected.\r\n", 59 | "\r\n", 60 | "- Optical Flow Equation:\r\n", 61 | " - Consider an object with intensity $I (x, y, t)$, after time $dt$, it moves to by $dx$ and $dy$, now, the new intensity would be, $I (x+dx, y+dy, t+dt)$.\r\n", 62 | " - We, assume that the pixel intensities are constant between the two frames, i.e.,\r\n", 63 | "\\begin{align*}\r\n", 64 | " I (x, y, t) = I (x+dx, y+dy, t+dt)\r\n", 65 | "\\end{align*}\r\n", 66 | "\r\n", 67 | " - Then take taylor series approximation of right-hand side, resulting in,\r\n", 68 | "\r\n", 69 | " \\begin{align*}\r\n", 70 | " \\dfrac{dI}{dx}\\delta x+\\dfrac{dI}{dy}\\delta y+\\dfrac{dI}{dt}\\delta t=0\r\n", 71 | " \\end{align*}\r\n", 72 | "\r\n", 73 | " - Dividing equation above by $\\delta t $ gives us Optical Flow Equation:\r\n", 74 | "\r\n", 75 | " \\begin{align*}\r\n", 76 | " \\dfrac{dI}{dx}u+\\dfrac{dI}{dy}v+\\dfrac{dI}{dt}=0\r\n", 77 | " \\end{align*}\r\n", 78 | "\r\n", 79 | " - where $u= \\delta x/\\delta t, v= \\delta y/\\delta t$ and $dl/dx$ is image gradient along horizontal axis and $dl/dy$ is image gradient along vertical axis and $dl/dt$ is along the time. \r\n", 80 | "\r\n", 81 | "\r\n", 82 | "\r\n", 83 | "---\r\n", 84 | "\r\n", 85 | "\r\n", 86 | "#### Lucas - Kanade Function" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "metadata": { 92 | "id": "rwE1t2flOcWC" 93 | }, 94 | "source": [ 95 | "import cv2\r\n", 96 | "import numpy as np\r\n" 97 | ], 98 | "execution_count": 1, 99 | "outputs": [] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "metadata": { 104 | "id": "5pHIsw9HpKSe" 105 | }, 106 | "source": [ 107 | "#Creating a dictionary to track corners\r\n", 108 | "corner_track_params = dict(maxCorners=10,\r\n", 109 | " qualityLevel=0.3,\r\n", 110 | " minDistance=7,\r\n", 111 | " blockSize=7)\r\n", 112 | "#parameters for Lucas Kanade function\r\n", 113 | "#Smaller window - more senstive to noise and may miss larger motions\r\n", 114 | "'''\r\n", 115 | "LK method using the idea of image pyramid \r\n", 116 | "Level0 - original image, Level1 - 1/2 resol, Level2 - 1/4 resol, Level3 - 1/8 resol, Level4 - 1/15 resol\r\n", 117 | "At each level the image is blurred and subsample i.e. allows to find optical flow at various resolutions\r\n", 118 | "'''\r\n", 119 | "lk_params = dict(winSize=(200,200),\r\n", 120 | " maxLevel=2,\r\n", 121 | " criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03))" 122 | ], 123 | "execution_count": 2, 124 | "outputs": [] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "metadata": { 129 | "id": "agBAX3Jqrl-5" 130 | }, 131 | "source": [ 132 | "# live streaming capturing of Sparse Optical Flow\r\n", 133 | "\r\n", 134 | "cap = cv2.VideoCapture(0)\r\n", 135 | "ret,prev_frame = cap.read()\r\n", 136 | "\r\n", 137 | "prev_gray = cv2.cvtColor(prev_frame,cv2.COLOR_BGR2GRAY)\r\n", 138 | "\r\n", 139 | "# points to track\r\n", 140 | "\r\n", 141 | "prevPts = cv2.goodFeaturesToTrack(prev_gray,mask=None,**corner_track_params)\r\n", 142 | "\r\n", 143 | "#mask\r\n", 144 | "mask = np.zeros_like(prev_frame)\r\n", 145 | "\r\n", 146 | "while True:\r\n", 147 | "\r\n", 148 | " ret, frame = cap.read()\r\n", 149 | " frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)\r\n", 150 | "\r\n", 151 | " nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray,frame_gray,prevPts, None,**lk_params)\r\n", 152 | "\r\n", 153 | " good_new = nextPts[status==1]\r\n", 154 | " good_prev = prevPts[status==1]\r\n", 155 | "\r\n", 156 | " for i, (new,prev), in enumerate(zip(good_new,good_prev)):\r\n", 157 | " x_new,y_new = new.ravel()\r\n", 158 | " x_prev,y_prev = prev.ravel()\r\n", 159 | "\r\n", 160 | " mask = cv2.line(mask,(x_new,y_new),(x_prev,y_prev),(0,255,0),3)\r\n", 161 | "\r\n", 162 | " frame = cv2.circle(frame,(x_new,y_new),8,(0,0,255),-1) #draing circle on current points in a frame are\r\n", 163 | " \r\n", 164 | " img = cv2.add(frame,mask)\r\n", 165 | " cv2.imshow('tracking',img)\r\n", 166 | "\r\n", 167 | " k = cv2.waitKey(30) & 0xFF\r\n", 168 | " if k == 27:\r\n", 169 | " break\r\n", 170 | " \r\n", 171 | " prev_gray = frame_gray.copy()\r\n", 172 | " prevPts = good_new.reshape(-1,1,2)\r\n", 173 | "\r\n", 174 | "cv2.destroyAllWindows()\r\n", 175 | "cap.release()" 176 | ], 177 | "execution_count": null, 178 | "outputs": [] 179 | } 180 | ] 181 | } -------------------------------------------------------------------------------- /6. Object Tracking/2_ObjectTracking_GunnerFarneBack.py: -------------------------------------------------------------------------------- 1 | # Gunner Farneback's algorithm 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | cap = cv2.VideoCapture(0) 7 | 8 | ret, frame1 = cap.read() #capturing image 9 | 10 | prvsImg = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) #converting to grayscale 11 | 12 | hsv_mask = np.zeros_like(frame1) 13 | hsv_mask[:,:,1] = 255 14 | 15 | while True: 16 | ret,frame2 = cap.read() 17 | #compare previous entire iamge to the current image 18 | nextImg = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) 19 | #optical flow 20 | #calcOpticalFlowFarneback(prev, next, flow, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags) 21 | flow = cv2.calcOpticalFlowFarneback(prvsImg,nextImg,None,0.5,3,15,3,5,1.2,0) #default 22 | 23 | #Flow object contains vector flow cartesian information - pointing at direction each pixel is happening in. 24 | #convert to poalr (mag, angle) that can be mapped to hue color mapping 25 | # mag - saturation direction, angle between hue and saturation 26 | #moving left will be red, moving red - blue 27 | 28 | mag, ang = cv2.cartToPolar(flow[:,:,0],flow[:,:,1],angleInDegrees=True) #for every pixel in x,y pick horiztonal and vertical 29 | 30 | hsv_mask[:,:,0] = ang/2 #looking at half the hues (180-360) 31 | hsv_mask[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) 32 | 33 | bgr = cv2.cvtColor(hsv_mask,cv2.COLOR_HSV2BGR) 34 | 35 | cv2.imshow('frame',bgr) 36 | 37 | k = cv2.waitKey(10) & 0xFF 38 | if k == 27: 39 | break 40 | prvsImg = nextImg 41 | 42 | cv2.destroyAllWindows() 43 | cap.release() 44 | 45 | -------------------------------------------------------------------------------- /6. Object Tracking/3_ObjectTracking_Introduction_LucasKanade.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | #Creating a dictionary to track corners 5 | corner_track_params = dict(maxCorners=10, 6 | qualityLevel=0.3, 7 | minDistance=7, 8 | blockSize=7) 9 | #parameters for Lucas Kanade function 10 | #Smaller window - more senstive to noise and may miss larger motions 11 | ''' 12 | LK method using the idea of image pyramid 13 | Level0 - original image, Level1 - 1/2 resol, Level2 - 1/4 resol, Level3 - 1/8 resol, Level4 - 1/15 resol 14 | At each level the image is blurred and subsample i.e. allows to find optical flow at various resolutions 15 | ''' 16 | lk_params = dict(winSize=(200,200), 17 | maxLevel=2, 18 | criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,0.03)) 19 | # live streaming capturing of Sparse Optical Flow 20 | 21 | cap = cv2.VideoCapture(0) 22 | ret,prev_frame = cap.read() 23 | 24 | prev_gray = cv2.cvtColor(prev_frame,cv2.COLOR_BGR2GRAY) 25 | 26 | # points to track 27 | 28 | prevPts = cv2.goodFeaturesToTrack(prev_gray,mask=None,**corner_track_params) 29 | 30 | #mask 31 | mask = np.zeros_like(prev_frame) 32 | 33 | while True: 34 | 35 | ret, frame = cap.read() 36 | frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) 37 | 38 | nextPts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray,frame_gray,prevPts, None,**lk_params) 39 | 40 | good_new = nextPts[status==1] 41 | good_prev = prevPts[status==1] 42 | 43 | for i, (new,prev), in enumerate(zip(good_new,good_prev)): 44 | x_new,y_new = new.ravel() 45 | x_prev,y_prev = prev.ravel() 46 | 47 | mask = cv2.line(mask,(x_new,y_new),(x_prev,y_prev),(0,255,0),3) 48 | 49 | frame = cv2.circle(frame,(x_new,y_new),8,(0,0,255),-1) #draing circle on current points in a frame are 50 | 51 | img = cv2.add(frame,mask) 52 | cv2.imshow('tracking',img) 53 | 54 | k = cv2.waitKey(30) & 0xFF 55 | if k == 27: 56 | break 57 | 58 | prev_gray = frame_gray.copy() 59 | prevPts = good_new.reshape(-1,1,2) 60 | 61 | cv2.destroyAllWindows() 62 | cap.release() 63 | -------------------------------------------------------------------------------- /6. Object Tracking/4_ObjectTracking_MeanShift_CamShift_1_Theory.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "4_ObjectTracking_MeanShift_CamShift_1_Theory.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "toc_visible": true, 10 | "authorship_tag": "ABX9TyNVy2ordxWSnWh8Bemy8gyW", 11 | "include_colab_link": true 12 | }, 13 | "kernelspec": { 14 | "name": "python3", 15 | "display_name": "Python 3" 16 | } 17 | }, 18 | "cells": [ 19 | { 20 | "cell_type": "markdown", 21 | "metadata": { 22 | "id": "view-in-github", 23 | "colab_type": "text" 24 | }, 25 | "source": [ 26 | "\"Open" 27 | ] 28 | }, 29 | { 30 | "cell_type": "markdown", 31 | "metadata": { 32 | "id": "OPrhTXQWEaAd" 33 | }, 34 | "source": [ 35 | "### Object Tracking\r\n", 36 | "\r\n", 37 | "In this section, we will cover:\r\n", 38 | "\r\n", 39 | "- Basic Object Tracking Techniques\r\n", 40 | " - Optical Flow\r\n", 41 | " - Meanshift and Camshift\r\n", 42 | "- Advanced Tracking\r\n", 43 | " - Built-In Tracking APIs\r\n", 44 | "\r\n", 45 | "#### Meanshift and CamShift (Continously Adaptive Meanshift)\r\n", 46 | "\r\n", 47 | "- Basic tracking methods:\r\n", 48 | "- Let us understand through an example:\r\n", 49 | " - We have intial set of points and we want to assign them into clusters\r\n", 50 | "\r\n", 51 | "![alt text](https://drive.google.com/uc?id=10_0IiQcujV5ZnDCOgmjsQpQeOGa149FV)\r\n", 52 | "\r\n", 53 | " - We have both red (faintly there) and blue points.\r\n", 54 | " - For Meanshift, we find the direction to the closest cluster centroid by where most points are nearby at. i.e. calculating weighted mean \r\n", 55 | " - For each ieration blue points will move to the centroid center for each cluster\r\n", 56 | " - After One Iteration , all blue points move towards the clusters and we can see red points that were beneath the blue points before iteration\r\n", 57 | "\r\n", 58 | "![alt text](https://drive.google.com/uc?id=1ObD-42gEgVrmqUb_bU5qGTg4wx6d1AAX)\r\n", 59 | "\r\n", 60 | " - After Second Iteration , the bottom clusters have begun to covergence\r\n", 61 | " - After Third Iteration , 3 clusters were found at each centroid.\r\n", 62 | " - Trying further iterations, there is no more change in clustering, so we stop there.\r\n", 63 | " - So we identify three clusters (2 bottom)\r\n", 64 | " - In MeanShift algorithm, you can't day before how man y clusters you want to form as in this case there was a chace to have 4 clusters. Else we can use K-means algorithm (done in ML) where we can choose number of clusters beforehand.\r\n", 65 | " - In Object Tracking , Meanshift can calculate the color histogram of the target area, and then keep sliding the tracking window to the closest match.\r\n", 66 | " - Meanshift won't change the window size if the target moves away or towards the camera. But we can use CAMShift to update the size of the window (new ROI and orientation is adjusted and again Meanshift is applied to new scaled window) \r\n", 67 | "\r\n" 68 | ] 69 | } 70 | ] 71 | } -------------------------------------------------------------------------------- /6. Object Tracking/4_ObjectTracking_MeanShift_CamShift_2_CamShift.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | cap = cv2.VideoCapture(0) 5 | 6 | ret,frame = cap.read() 7 | 8 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 9 | face_rects = face_cascade.detectMultiScale(frame) 10 | 11 | #track one single only using this code 12 | 13 | (face_x,face_y,w,h)=tuple(face_rects[0]) 14 | 15 | track_window = (face_x,face_y,w,h) #to track the tuple 16 | 17 | #Region of interest 18 | 19 | roi = frame[face_y:face_y+h,face_x:face_x+w] 20 | 21 | #HSV color mapping 22 | 23 | hsv_roi = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV) 24 | 25 | #ROI histogram to back track mean shift 26 | 27 | roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) 28 | 29 | cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) 30 | 31 | #Determinent criteria 32 | 33 | term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,1) 34 | 35 | while True: 36 | ret, frame = cap.read() #keep grabbing from camera 37 | 38 | if ret == True: 39 | hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) 40 | dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) 41 | 42 | ############################# 43 | 44 | ret, track_window = cv2.CamShift(dst,track_window,term_criteria) 45 | 46 | #ret, track_window = cv2.meanShift(dst,track_window,term_criteria) 47 | 48 | pts = cv2.boxPoints(ret) 49 | pts = np.int0(pts)#converting all into int 50 | 51 | img2 = cv2.polylines(frame,[pts],True,(0,0,255),5) #cont. updating the rectangle 52 | 53 | ################################# 54 | 55 | cv2.imshow('img',img2) 56 | 57 | k = cv2.waitKey(10) & 0xFF 58 | if k == 27: 59 | break 60 | else: 61 | break 62 | 63 | cv2.destroyAllWindows() 64 | cap.release() 65 | -------------------------------------------------------------------------------- /6. Object Tracking/4_ObjectTracking_MeanShift_CamShift_2_MeanShift.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | cap = cv2.VideoCapture(0) 5 | 6 | ret,frame = cap.read() 7 | 8 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 9 | face_rects = face_cascade.detectMultiScale(frame) 10 | 11 | #track one single only using this code 12 | 13 | (face_x,face_y,w,h)=tuple(face_rects[0]) 14 | 15 | track_window = (face_x,face_y,w,h) #to track the tuple 16 | 17 | #Region of interest 18 | 19 | roi = frame[face_y:face_y+h,face_x:face_x+w] 20 | 21 | #HSV color mapping 22 | 23 | hsv_roi = cv2.cvtColor(roi,cv2.COLOR_BGR2HSV) 24 | 25 | #ROI histogram to back track mean shift 26 | 27 | roi_hist = cv2.calcHist([hsv_roi],[0],None,[180],[0,180]) 28 | 29 | cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX) 30 | 31 | #Determinent criteria 32 | 33 | term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,10,1) 34 | 35 | while True: 36 | ret, frame = cap.read() #keep grabbing from camera 37 | 38 | if ret == True: 39 | hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV) 40 | dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1) 41 | 42 | ret, track_window = cv2.meanShift(dst,track_window,term_criteria) 43 | 44 | x,y,w,h = track_window 45 | img2 = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),5) #cont. updating the rectangle 46 | 47 | cv2.imshow('img',img2) 48 | 49 | k = cv2.waitKey(10) & 0xFF 50 | if k == 27: 51 | break 52 | else: 53 | break 54 | 55 | cv2.destroyAllWindows() 56 | cap.release() 57 | -------------------------------------------------------------------------------- /7. Deep Learning CNN/1_DeepLearning_ConvolutionNeuralNetworks.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "1_DeepLearning_ConvolutionNeuralNetworks.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "machine_shape": "hm", 10 | "authorship_tag": "ABX9TyM4OTn7K+IdHGfUcwCwSqRO", 11 | "include_colab_link": true 12 | }, 13 | "kernelspec": { 14 | "name": "python3", 15 | "display_name": "Python 3" 16 | }, 17 | "accelerator": "GPU" 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "id": "yvWDl_nLoM_Q" 34 | }, 35 | "source": [ 36 | "### Deep Learning For Object Detection and Image Classification\r\n", 37 | "\r\n", 38 | "Here in this section we will cover only applications of Deep Learning for Image Classification problems.\r\n", 39 | "\r\n", 40 | " - Convolution Neural Networks (CNN) Theory \r\n", 41 | " - Keras CNN with MNIST Dataset\r\n", 42 | " - Keras CNN with CIFAR-10\r\n", 43 | " - Deep Learning on Custom Images (Cats & Dogs)\r\n", 44 | "\r\n", 45 | "\r\n", 46 | "---\r\n", 47 | "\r\n" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": { 53 | "id": "RFXAfAwUo4DC" 54 | }, 55 | "source": [ 56 | "#### Convolution Neural Networks (CNN) Theory \r\n", 57 | "\r\n", 58 | "Basically when dealing in 2D data (images) we need to tackle different color channels and use tensors to form our data correctly.\r\n", 59 | "\r\n", 60 | "Convolution Neural Networks (CNN) is a simple perceptron model (Biological research - Neurons can focus on small local receptive field when activated). \r\n", 61 | " - Yann LeCun et al. published their work in 1998 linking Artificial Neural Network (ANN) with images that resulted in CNN and implemented in LeNet-5 architecture to classify MNIST dataset.\r\n", 62 | " - A simple CNN architecture looks like this:\r\n", 63 | "\r\n", 64 | " ![alt text](https://drive.google.com/uc?id=1q33DA7IetuaQ7Qmb9b05ur-uSz7d3XoA)\r\n", 65 | "\r\n", 66 | " - As we have see in CNN, we have Input, Feature Extraction (convolution, sub-sampling, pooling) and Classification . So we can break out in following sections to learn deep into CNN.\r\n", 67 | " - Tensors, DNN vs CNN, Convolution & Filters\r\n", 68 | " - Padding, Pooling Layers, & Review Dropout\r\n", 69 | "\r\n", 70 | "##### Tensors\r\n", 71 | "\r\n", 72 | "- N-Dimensional Arrays - Scaler (1), Vector (2), Matrix (3), Tensor (4)\r\n", 73 | "- Tensors are very convenient to feed set of images (I,H,W,C), where:\r\n", 74 | " - I = Images\r\n", 75 | " - H = Height of images in pixels\r\n", 76 | " - W = Width of images in pixels\r\n", 77 | " - C = Color Channels: 1-Gray Scale, 3-RGB\r\n", 78 | "\r\n", 79 | "##### DNN vs CNN\r\n", 80 | "\r\n", 81 | "- Densely Connected Layer:\r\n", 82 | " - Every neuron in one layer is connected to every other neuron in the another layer.\r\n", 83 | "- Convolution Layer:\r\n", 84 | " - Every neuron in one layer is connected to nearby neurons only directly inspired by biological visual cortex where you are only looking at local receptive fields\r\n", 85 | "\r\n", 86 | "![alt text](https://drive.google.com/uc?id=1F2pr8Ov1LbJWLfNKZvjsrS5ok66-qFac)\r\n", 87 | "\r\n", 88 | "- Why consider CNN over DNN ?\r\n", 89 | " - In general practice we have $28\\times28$ pixels data but in reality we have images that are atleast $256\\times256$ pixels data i.e. $\\approx 56000$ total pixels that leads to too many parameters, unscalable to new images. \r\n", 90 | " - CNN have a major advantage: \r\n", 91 | " - For IMAGE PROCESSING: as nearby pixels are much coorealted to each other and helpful in image detection \r\n", 92 | " - Each CNN layer looks at an increasing part of the image.\r\n", 93 | " - Helps in INVARIANCE: by having units only connected to nearby units\r\n", 94 | " - CNN also helps with regularization, limiting search of weights to the size of the convolution.\r\n", 95 | "\r\n", 96 | "##### CNN \r\n", 97 | "\r\n", 98 | "- Generally we have input layer and for each pixels there are nearby neurons connected to the convolution layer.\r\n", 99 | "- At the edge of the image there are issues as there may not be enough input neurons from the actual data. This is fixed by adding padding of zeros around it. \r\n", 100 | "\r\n", 101 | " ![alt text](https://drive.google.com/uc?id=1fHLYmzAXM4hpuikxyYvduBYeZOYNkTuo)\r\n", 102 | "\r\n", 103 | "- 1D Convolution\r\n", 104 | " - We can treat these weights as a filter\r\n", 105 | " - Example of 1 filter\r\n", 106 | "\r\n", 107 | " \\begin{align*}\r\n", 108 | " y = w_1x_1+w_2x_2\r\n", 109 | " \\end{align*}\r\n", 110 | "\r\n", 111 | " - If $(w_1,w_2)=(1,-1)$ then $y=x_1-x_2$ and $y_{\\mathrm{max}}$ (activation) occurs at $(x_1,x_2)=(1,0)$ - This is an exmaple of edge detection.\r\n", 112 | " \r\n", 113 | " ![alt text](https://drive.google.com/uc?id=1kkdQcfeLd92YaoFoojTejZIYqNxxIfGc)\r\n", 114 | "\r\n", 115 | "\r\n", 116 | "- When we have multiple weights across input neurons repeated consecutive times - stride of filter . In this case we have 1 filter, 2 filter size (weigths) and stride = 2.\r\n", 117 | "- Also, we can have single stride as well but last neuron in this case might be left out that can be added with padding.\r\n", 118 | "- Plus we can increase number of filters of 2 or 4 as shown in the figure. Each filter will be detecting different features\r\n", 119 | "- For simplicity, we present the CNN network as set of blocks\r\n", 120 | "\r\n", 121 | " ![alt text](https://drive.google.com/uc?id=1LlTaY11T1MHkSEmAfpdXEiurhNG9sujH)\r\n", 122 | "\r\n", 123 | "- 2D Convolution\r\n", 124 | " - Now we have height and width of image itself ans subsections are directly related to tensors\r\n", 125 | " - Similarly we can have color added to input image if it is colored image.\r\n", 126 | "\r\n", 127 | "![alt text](https://drive.google.com/uc?id=1SZ0haGvqk1tCvTn6eEAI2kLYvsCOxvDQ)\r\n", 128 | "\r\n", 129 | "- CNN Filters\r\n", 130 | " - Looked as grid system.\r\n", 131 | " - Input image (padded already) has [-1,0,1] where -1 is lightest and +1 is the darkest.\r\n", 132 | " - $3\\times3$ filter is multiplied with input image $(6\\times6)$ resulting in $3\\times3$ matrix for first pixel whose sum gives us total pixel value.\r\n", 133 | "\r\n", 134 | "\r\n", 135 | " ![alt text](https://drive.google.com/uc?id=1G9Grq8rlLtPrvFr6SQcSyRPg-hE3w3Yr)\r\n", 136 | "\r\n", 137 | " - Taking into account stride distance (how fast you are moving across image with filters)\r\n", 138 | "\r\n", 139 | " ![alt text](https://drive.google.com/uc?id=12lijzKVkXBYratAjoxvSrPRwiAIK5O0P)\r\n", 140 | "\r\n", 141 | "\r\n", 142 | "\r\n", 143 | "---\r\n", 144 | "\r\n", 145 | "---\r\n", 146 | "\r\n", 147 | "\r\n", 148 | "\r\n", 149 | "\r\n" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": { 155 | "id": "McO8NhCqFuFi" 156 | }, 157 | "source": [ 158 | "##### Sub-Sampling (Pooling)\r\n", 159 | "\r\n", 160 | " Pooling layers will subsample the input image, which reduces the memory use and computer load as well as reducing the number of parameters.\r\n", 161 | "\r\n", 162 | "- For an image, we take a $n\\times n$ pool (generally we take $2\\times2$ pooling), from which we evaluate maximum value.\r\n", 163 | "- We decide that mximum value is going to be the representative of the entire kernel.\r\n", 164 | "\r\n", 165 | " ![alt text](https://drive.google.com/uc?id=11OrR7JfXRxEkF7DIFKSkWJBwlZ3exd_a)\r\n", 166 | "\r\n", 167 | " - Pooling layer ends up removing a lot of information (even $2\\times2$) can remove 75% of the input data. But for CNN for iamges it is just too computational expensive.\r\n", 168 | "\r\n", 169 | "##### Dropout\r\n", 170 | "\r\n", 171 | " Form of regularization to help prevent overfitting. \r\n", 172 | " - During training, units are randomly dropped along with their connections\r\n", 173 | "\r\n", 174 | "##### Famours CNN architectures\r\n", 175 | "\r\n", 176 | " - LeNet-5 by Yann LeCun\r\n", 177 | " - AlexNet \r\n", 178 | " - GoogLeNet\r\n", 179 | " - ResNet\r\n", 180 | "\r\n", 181 | "AlexNet\r\n", 182 | "\r\n", 183 | "![alt text](https://drive.google.com/uc?id=1kEP-nECVSCCCmMP_7sm4elB-f_SseGuq)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "markdown", 188 | "metadata": { 189 | "id": "dRmdKU2CJPAV" 190 | }, 191 | "source": [ 192 | "

Keras CNN with MNIST

" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "metadata": { 198 | "id": "GJIXDhuvlD_B", 199 | "colab": { 200 | "base_uri": "https://localhost:8080/" 201 | }, 202 | "outputId": "d81a0a0a-23b9-46e9-f52b-3ed4195714d4" 203 | }, 204 | "source": [ 205 | "# Checking GPU available on Colab\r\n", 206 | "\r\n", 207 | "gpu_info = !nvidia-smi\r\n", 208 | "gpu_info = '\\n'.join(gpu_info)\r\n", 209 | "if gpu_info.find('failed') >= 0:\r\n", 210 | " print('Select the Runtime > \"Change runtime type\" menu to enable a GPU accelerator, ')\r\n", 211 | " print('and then re-execute this cell.')\r\n", 212 | "else:\r\n", 213 | " print(gpu_info)\r\n", 214 | "\r\n", 215 | "from psutil import virtual_memory\r\n", 216 | "ram_gb = virtual_memory().total / 1e9\r\n", 217 | "print('Your runtime has {:.1f} gigabytes of available RAM\\n'.format(ram_gb))\r\n", 218 | "\r\n", 219 | "if ram_gb < 20:\r\n", 220 | " print('To enable a high-RAM runtime, select the Runtime > \"Change runtime type\"')\r\n", 221 | " print('menu, and then select High-RAM in the Runtime shape dropdown. Then, ')\r\n", 222 | " print('re-execute this cell.')\r\n", 223 | "else:\r\n", 224 | " print('You are using a high-RAM runtime!')" 225 | ], 226 | "execution_count": 1, 227 | "outputs": [ 228 | { 229 | "output_type": "stream", 230 | "text": [ 231 | "Sun Dec 13 03:57:55 2020 \n", 232 | "+-----------------------------------------------------------------------------+\n", 233 | "| NVIDIA-SMI 455.45.01 Driver Version: 418.67 CUDA Version: 10.1 |\n", 234 | "|-------------------------------+----------------------+----------------------+\n", 235 | "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n", 236 | "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n", 237 | "| | | MIG M. |\n", 238 | "|===============================+======================+======================|\n", 239 | "| 0 Tesla P100-PCIE... Off | 00000000:00:04.0 Off | 0 |\n", 240 | "| N/A 39C P0 29W / 250W | 0MiB / 16280MiB | 0% Default |\n", 241 | "| | | ERR! |\n", 242 | "+-------------------------------+----------------------+----------------------+\n", 243 | " \n", 244 | "+-----------------------------------------------------------------------------+\n", 245 | "| Processes: |\n", 246 | "| GPU GI CI PID Type Process name GPU Memory |\n", 247 | "| ID ID Usage |\n", 248 | "|=============================================================================|\n", 249 | "| No running processes found |\n", 250 | "+-----------------------------------------------------------------------------+\n", 251 | "Your runtime has 27.4 gigabytes of available RAM\n", 252 | "\n", 253 | "You are using a high-RAM runtime!\n" 254 | ], 255 | "name": "stdout" 256 | } 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "metadata": { 262 | "id": "viktqHvgIr03" 263 | }, 264 | "source": [ 265 | "#import libraries\r\n", 266 | "\r\n", 267 | "from keras.datasets import mnist" 268 | ], 269 | "execution_count": 2, 270 | "outputs": [] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "metadata": { 275 | "id": "24I_u1htJbvd" 276 | }, 277 | "source": [ 278 | "(x_train,y_train) , (x_test,y_test) = mnist.load_data()" 279 | ], 280 | "execution_count": 3, 281 | "outputs": [] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "metadata": { 286 | "colab": { 287 | "base_uri": "https://localhost:8080/" 288 | }, 289 | "id": "Phi2RcTvJk-0", 290 | "outputId": "eaa9543d-08af-4721-881e-be0a42cca722" 291 | }, 292 | "source": [ 293 | "import matplotlib.pyplot as plt\r\n", 294 | "%matplotlib inline\r\n", 295 | "\r\n", 296 | "print('Training Dataset Size: ', x_train.shape)\r\n", 297 | "print('Testing Dataset Size: ', x_test.shape)" 298 | ], 299 | "execution_count": 4, 300 | "outputs": [ 301 | { 302 | "output_type": "stream", 303 | "text": [ 304 | "Training Dataset Size: (60000, 28, 28)\n", 305 | "Testing Dataset Size: (10000, 28, 28)\n" 306 | ], 307 | "name": "stdout" 308 | } 309 | ] 310 | }, 311 | { 312 | "cell_type": "markdown", 313 | "metadata": { 314 | "id": "MQRLTLDTJz0c" 315 | }, 316 | "source": [ 317 | "Here we see we have 60000 training images with $28\\times28$ pixel size with no color channel." 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "metadata": { 323 | "colab": { 324 | "base_uri": "https://localhost:8080/", 325 | "height": 283 326 | }, 327 | "id": "j4K6ViCLJy41", 328 | "outputId": "f219955d-362a-41e1-ce31-84e7ddf6315e" 329 | }, 330 | "source": [ 331 | "single_image = x_train[0]\r\n", 332 | "#plt.imshow(single_image,cmap='gray') for black background\r\n", 333 | "plt.imshow(single_image,cmap='gray_r')" 334 | ], 335 | "execution_count": 5, 336 | "outputs": [ 337 | { 338 | "output_type": "execute_result", 339 | "data": { 340 | "text/plain": [ 341 | "" 342 | ] 343 | }, 344 | "metadata": { 345 | "tags": [] 346 | }, 347 | "execution_count": 5 348 | }, 349 | { 350 | "output_type": "display_data", 351 | "data": { 352 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZ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3tlqRZ39lxmXNFfSgyrwsUvMdZ9a8LgVcWafJulTd//c3c9I+rOkOQXM0fbc/R1Jp8/bPEfShuz2BvX9Y2m5KrO1BXc/5u7vZ7e/lnRumfFCH7vEXC1RRNnHS/pbv6+PqL3We3dJfzWzvWa2uOhhBjDO3Y9lt49LGlfkMAOouYx3K523zHjbPHb1LH/eKF6g+7Fb3H2qpLskLcmerrYl7/sZrJ2unQ5qGe9WGWCZ8X8p8rGrd/nzRhVR9qOSJvT7+ufZtrbg7kezzyclbVX7LUV94twKutnnkwXP8y/ttIz3QMuMqw0euyKXPy+i7HskXWdmvzCziyXNl7StgDl+xMxGZi+cyMxGSpqt9luKepukRdntRZJeL3CWH2iXZbyrLTOugh+7wpc/d/eWf0i6W32vyH8maVURM1SZ65eS/jf7OFD0bJI2qe9p3Xfqe23jYUn/JmmnpEOS3pI0po1m+29JH0rqUV+xOgua7Rb1PUXvkbQv+7i76McuMVdLHjd+XRYIghfogCAoOxAEZQeCoOxAEJQdCIKyA0FQdiCI/wfvpjt5Q0mdXQAAAABJRU5ErkJggg==\n", 353 | "text/plain": [ 354 | "
" 355 | ] 356 | }, 357 | "metadata": { 358 | "tags": [], 359 | "needs_background": "light" 360 | } 361 | } 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "metadata": { 367 | "colab": { 368 | "base_uri": "https://localhost:8080/" 369 | }, 370 | "id": "0MbCxBrNKFuM", 371 | "outputId": "47bf93d1-7c6a-4ab7-bd5c-96bb727891bd" 372 | }, 373 | "source": [ 374 | "y_train" 375 | ], 376 | "execution_count": 6, 377 | "outputs": [ 378 | { 379 | "output_type": "execute_result", 380 | "data": { 381 | "text/plain": [ 382 | "array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)" 383 | ] 384 | }, 385 | "metadata": { 386 | "tags": [] 387 | }, 388 | "execution_count": 6 389 | } 390 | ] 391 | }, 392 | { 393 | "cell_type": "markdown", 394 | "metadata": { 395 | "id": "c1XWyIxpKWJ8" 396 | }, 397 | "source": [ 398 | "It seems to have different output values and network can get confused and think it as a regression problem instead of image classification problem.\r\n", 399 | "\r\n", 400 | " - So we need to standradize them using one-hot encoding" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "metadata": { 406 | "colab": { 407 | "base_uri": "https://localhost:8080/" 408 | }, 409 | "id": "RS2nQHRJKUns", 410 | "outputId": "d5637d4e-23e2-436c-a8cb-53f0f8313f4a" 411 | }, 412 | "source": [ 413 | "from keras.utils.np_utils import to_categorical\r\n", 414 | "\r\n", 415 | "y_cat_test = to_categorical(y_test,10)\r\n", 416 | "y_cat_train = to_categorical(y_train,10)\r\n", 417 | "\r\n", 418 | "print('First Index')\r\n", 419 | "print(y_train[0])\r\n", 420 | "print(y_cat_train[0])\r\n", 421 | "print('------------------------------------------------')\r\n", 422 | "print('Second Index')\r\n", 423 | "print(y_train[1])\r\n", 424 | "print(y_cat_train[1])" 425 | ], 426 | "execution_count": 7, 427 | "outputs": [ 428 | { 429 | "output_type": "stream", 430 | "text": [ 431 | "First Index\n", 432 | "5\n", 433 | "[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]\n", 434 | "------------------------------------------------\n", 435 | "Second Index\n", 436 | "0\n", 437 | "[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n" 438 | ], 439 | "name": "stdout" 440 | } 441 | ] 442 | }, 443 | { 444 | "cell_type": "markdown", 445 | "metadata": { 446 | "id": "LJ6i4NhxLaGS" 447 | }, 448 | "source": [ 449 | "Also the image is not normalize as it has values ranging between [0-255]" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "metadata": { 455 | "colab": { 456 | "base_uri": "https://localhost:8080/" 457 | }, 458 | "id": "Hlf7zDA0LEr7", 459 | "outputId": "892b6e36-7dea-466d-afc8-f1ae95a88e21" 460 | }, 461 | "source": [ 462 | "single_image.max()" 463 | ], 464 | "execution_count": 8, 465 | "outputs": [ 466 | { 467 | "output_type": "execute_result", 468 | "data": { 469 | "text/plain": [ 470 | "255" 471 | ] 472 | }, 473 | "metadata": { 474 | "tags": [] 475 | }, 476 | "execution_count": 8 477 | } 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "metadata": { 483 | "colab": { 484 | "base_uri": "https://localhost:8080/" 485 | }, 486 | "id": "LXAlFeb_LisS", 487 | "outputId": "fe6f7abf-9940-4ad1-b67d-469098fc95d1" 488 | }, 489 | "source": [ 490 | "#can use sklearn directly to normalize but let us see the method to normalize if we don't ahver sklearn\r\n", 491 | "\r\n", 492 | "x_train = x_train/x_train.max()\r\n", 493 | "x_test = x_test/x_test.max()\r\n", 494 | "\r\n", 495 | "single_image = x_train[0]\r\n", 496 | "single_image.max()" 497 | ], 498 | "execution_count": 9, 499 | "outputs": [ 500 | { 501 | "output_type": "execute_result", 502 | "data": { 503 | "text/plain": [ 504 | "1.0" 505 | ] 506 | }, 507 | "metadata": { 508 | "tags": [] 509 | }, 510 | "execution_count": 9 511 | } 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "metadata": { 517 | "id": "sLF4hcAEL55q" 518 | }, 519 | "source": [ 520 | "#scaling the data (generalized network to work on any image data)\r\n", 521 | "\r\n", 522 | "x_train = x_train.reshape(60000,28,28,1) #just telling there is a channel (color)\r\n", 523 | "x_test= x_test.reshape(10000,28,28,1)" 524 | ], 525 | "execution_count": 10, 526 | "outputs": [] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "metadata": { 531 | "id": "yGCQJLFSMbIJ" 532 | }, 533 | "source": [ 534 | "#Build and train out model now\r\n", 535 | "\r\n", 536 | "from keras.models import Sequential\r\n", 537 | "from keras.layers import Dense,Conv2D,MaxPool2D,Flatten" 538 | ], 539 | "execution_count": 11, 540 | "outputs": [] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "metadata": { 545 | "id": "xa2-x8XDMpXp" 546 | }, 547 | "source": [ 548 | "model = Sequential() #create a model\r\n", 549 | "\r\n", 550 | "#Conv Layer\r\n", 551 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 552 | " input_shape=(28,28,1),activation='relu'))\r\n", 553 | "#Pooling layer\r\n", 554 | "model.add(MaxPool2D(pool_size=(2,2)))\r\n", 555 | "\r\n", 556 | "#Flatten out to understand Dense layer (2D-->1D)\r\n", 557 | "model.add(Flatten())\r\n", 558 | "\r\n", 559 | "#Dense Layer\r\n", 560 | "model.add(Dense(128,activation='relu'))\r\n", 561 | "\r\n", 562 | "#Output Layer\r\n", 563 | "model.add(Dense(10,activation='softmax'))\r\n", 564 | "\r\n", 565 | "model.compile(loss='categorical_crossentropy',\r\n", 566 | " optimizer='rmsprop',\r\n", 567 | " metrics=['accuracy'])" 568 | ], 569 | "execution_count": 12, 570 | "outputs": [] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "metadata": { 575 | "colab": { 576 | "base_uri": "https://localhost:8080/" 577 | }, 578 | "id": "1rv0GFxaNtHA", 579 | "outputId": "6930b2fb-adfc-45ea-82cc-bff8b8ea21c4" 580 | }, 581 | "source": [ 582 | "model.summary()" 583 | ], 584 | "execution_count": 13, 585 | "outputs": [ 586 | { 587 | "output_type": "stream", 588 | "text": [ 589 | "Model: \"sequential\"\n", 590 | "_________________________________________________________________\n", 591 | "Layer (type) Output Shape Param # \n", 592 | "=================================================================\n", 593 | "conv2d (Conv2D) (None, 25, 25, 32) 544 \n", 594 | "_________________________________________________________________\n", 595 | "max_pooling2d (MaxPooling2D) (None, 12, 12, 32) 0 \n", 596 | "_________________________________________________________________\n", 597 | "flatten (Flatten) (None, 4608) 0 \n", 598 | "_________________________________________________________________\n", 599 | "dense (Dense) (None, 128) 589952 \n", 600 | "_________________________________________________________________\n", 601 | "dense_1 (Dense) (None, 10) 1290 \n", 602 | "=================================================================\n", 603 | "Total params: 591,786\n", 604 | "Trainable params: 591,786\n", 605 | "Non-trainable params: 0\n", 606 | "_________________________________________________________________\n" 607 | ], 608 | "name": "stdout" 609 | } 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "metadata": { 615 | "colab": { 616 | "base_uri": "https://localhost:8080/" 617 | }, 618 | "id": "lroTi_irNvs4", 619 | "outputId": "bd64cf5b-c915-44c4-8404-e77cf31e4c11" 620 | }, 621 | "source": [ 622 | "#Train model\r\n", 623 | "\r\n", 624 | "model.fit(x_train,y_cat_train,epochs=50)" 625 | ], 626 | "execution_count": 14, 627 | "outputs": [ 628 | { 629 | "output_type": "stream", 630 | "text": [ 631 | "Epoch 1/50\n", 632 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.1431 - accuracy: 0.9564\n", 633 | "Epoch 2/50\n", 634 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0486 - accuracy: 0.9848\n", 635 | "Epoch 3/50\n", 636 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0354 - accuracy: 0.9897\n", 637 | "Epoch 4/50\n", 638 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0274 - accuracy: 0.9923\n", 639 | "Epoch 5/50\n", 640 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0225 - accuracy: 0.9937\n", 641 | "Epoch 6/50\n", 642 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0192 - accuracy: 0.9951\n", 643 | "Epoch 7/50\n", 644 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0156 - accuracy: 0.9957\n", 645 | "Epoch 8/50\n", 646 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0129 - accuracy: 0.9963\n", 647 | "Epoch 9/50\n", 648 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0103 - accuracy: 0.9974\n", 649 | "Epoch 10/50\n", 650 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0090 - accuracy: 0.9974\n", 651 | "Epoch 11/50\n", 652 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0070 - accuracy: 0.9981\n", 653 | "Epoch 12/50\n", 654 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0067 - accuracy: 0.9984\n", 655 | "Epoch 13/50\n", 656 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0056 - accuracy: 0.9984\n", 657 | "Epoch 14/50\n", 658 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0051 - accuracy: 0.9986\n", 659 | "Epoch 15/50\n", 660 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0040 - accuracy: 0.9989\n", 661 | "Epoch 16/50\n", 662 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0033 - accuracy: 0.9991\n", 663 | "Epoch 17/50\n", 664 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0028 - accuracy: 0.9994\n", 665 | "Epoch 18/50\n", 666 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0023 - accuracy: 0.9994\n", 667 | "Epoch 19/50\n", 668 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0015 - accuracy: 0.9995\n", 669 | "Epoch 20/50\n", 670 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0018 - accuracy: 0.9996\n", 671 | "Epoch 21/50\n", 672 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0018 - accuracy: 0.9995\n", 673 | "Epoch 22/50\n", 674 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0014 - accuracy: 0.9995\n", 675 | "Epoch 23/50\n", 676 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0011 - accuracy: 0.9997\n", 677 | "Epoch 24/50\n", 678 | "1875/1875 [==============================] - 4s 2ms/step - loss: 7.3954e-04 - accuracy: 0.9997\n", 679 | "Epoch 25/50\n", 680 | "1875/1875 [==============================] - 4s 2ms/step - loss: 0.0010 - accuracy: 0.9997\n", 681 | "Epoch 26/50\n", 682 | "1875/1875 [==============================] - 4s 2ms/step - loss: 5.0499e-04 - accuracy: 0.9999\n", 683 | "Epoch 27/50\n", 684 | "1875/1875 [==============================] - 4s 2ms/step - loss: 4.0783e-04 - accuracy: 0.9999\n", 685 | "Epoch 28/50\n", 686 | "1875/1875 [==============================] - 4s 2ms/step - loss: 5.0375e-04 - accuracy: 0.9999\n", 687 | "Epoch 29/50\n", 688 | "1875/1875 [==============================] - 4s 2ms/step - loss: 2.1033e-04 - accuracy: 0.9999\n", 689 | "Epoch 30/50\n", 690 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.2799e-04 - accuracy: 0.9999\n", 691 | "Epoch 31/50\n", 692 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.0447e-04 - accuracy: 0.9999\n", 693 | "Epoch 32/50\n", 694 | "1875/1875 [==============================] - 4s 2ms/step - loss: 3.7025e-04 - accuracy: 0.9999\n", 695 | "Epoch 33/50\n", 696 | "1875/1875 [==============================] - 4s 2ms/step - loss: 2.3881e-04 - accuracy: 0.9999\n", 697 | "Epoch 34/50\n", 698 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.1371e-04 - accuracy: 0.9999\n", 699 | "Epoch 35/50\n", 700 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.5942e-05 - accuracy: 1.0000\n", 701 | "Epoch 36/50\n", 702 | "1875/1875 [==============================] - 4s 2ms/step - loss: 3.4979e-05 - accuracy: 1.0000\n", 703 | "Epoch 37/50\n", 704 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.6463e-04 - accuracy: 1.0000\n", 705 | "Epoch 38/50\n", 706 | "1875/1875 [==============================] - 4s 2ms/step - loss: 3.4141e-05 - accuracy: 1.0000\n", 707 | "Epoch 39/50\n", 708 | "1875/1875 [==============================] - 4s 2ms/step - loss: 3.4061e-05 - accuracy: 1.0000\n", 709 | "Epoch 40/50\n", 710 | "1875/1875 [==============================] - 4s 2ms/step - loss: 6.0182e-06 - accuracy: 1.0000\n", 711 | "Epoch 41/50\n", 712 | "1875/1875 [==============================] - 4s 2ms/step - loss: 3.3435e-05 - accuracy: 1.0000\n", 713 | "Epoch 42/50\n", 714 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.3480e-06 - accuracy: 1.0000\n", 715 | "Epoch 43/50\n", 716 | "1875/1875 [==============================] - 4s 2ms/step - loss: 4.6047e-06 - accuracy: 1.0000\n", 717 | "Epoch 44/50\n", 718 | "1875/1875 [==============================] - 4s 2ms/step - loss: 9.4366e-09 - accuracy: 1.0000\n", 719 | "Epoch 45/50\n", 720 | "1875/1875 [==============================] - 4s 2ms/step - loss: 4.9419e-07 - accuracy: 1.0000\n", 721 | "Epoch 46/50\n", 722 | "1875/1875 [==============================] - 4s 2ms/step - loss: 4.0631e-08 - accuracy: 1.0000\n", 723 | "Epoch 47/50\n", 724 | "1875/1875 [==============================] - 4s 2ms/step - loss: 2.0537e-08 - accuracy: 1.0000\n", 725 | "Epoch 48/50\n", 726 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.7147e-04 - accuracy: 1.0000\n", 727 | "Epoch 49/50\n", 728 | "1875/1875 [==============================] - 4s 2ms/step - loss: 1.8091e-05 - accuracy: 1.0000\n", 729 | "Epoch 50/50\n", 730 | "1875/1875 [==============================] - 4s 2ms/step - loss: 4.9173e-08 - accuracy: 1.0000\n" 731 | ], 732 | "name": "stdout" 733 | }, 734 | { 735 | "output_type": "execute_result", 736 | "data": { 737 | "text/plain": [ 738 | "" 739 | ] 740 | }, 741 | "metadata": { 742 | "tags": [] 743 | }, 744 | "execution_count": 14 745 | } 746 | ] 747 | }, 748 | { 749 | "cell_type": "markdown", 750 | "metadata": { 751 | "id": "qVl8l1JtQCp5" 752 | }, 753 | "source": [ 754 | "Evaluate the model" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "metadata": { 760 | "colab": { 761 | "base_uri": "https://localhost:8080/" 762 | }, 763 | "id": "M0SxAUHPN7jX", 764 | "outputId": "fa969ccf-5d0d-4bcb-d723-2cb6a0ca07f6" 765 | }, 766 | "source": [ 767 | "print('Metrics used: ', model.metrics_names)\r\n", 768 | "\r\n", 769 | "#evaluate using test dataset\r\n", 770 | "\r\n", 771 | "model.evaluate(x_test,y_cat_test)" 772 | ], 773 | "execution_count": 15, 774 | "outputs": [ 775 | { 776 | "output_type": "stream", 777 | "text": [ 778 | "Metrics used: ['loss', 'accuracy']\n", 779 | "313/313 [==============================] - 1s 2ms/step - loss: 0.2285 - accuracy: 0.9872\n" 780 | ], 781 | "name": "stdout" 782 | }, 783 | { 784 | "output_type": "execute_result", 785 | "data": { 786 | "text/plain": [ 787 | "[0.22847983241081238, 0.9872000217437744]" 788 | ] 789 | }, 790 | "metadata": { 791 | "tags": [] 792 | }, 793 | "execution_count": 15 794 | } 795 | ] 796 | }, 797 | { 798 | "cell_type": "code", 799 | "metadata": { 800 | "colab": { 801 | "base_uri": "https://localhost:8080/" 802 | }, 803 | "id": "rgsoXjKVQWqu", 804 | "outputId": "57fecfb0-3605-4421-d2fd-66fb03d2fc86" 805 | }, 806 | "source": [ 807 | "from sklearn.metrics import classification_report\r\n", 808 | "predictions = model.predict_classes(x_test)\r\n", 809 | "\r\n", 810 | "print('First Index')\r\n", 811 | "print('Testing Dataset: ',y_cat_test[0])\r\n", 812 | "print('Predicted value: ',predictions[0])\r\n", 813 | "print('------------------------------------------------')\r\n", 814 | "print('Second Index')\r\n", 815 | "print('Testing Dataset: ',y_cat_test[1])\r\n", 816 | "print('Predicted value: ',predictions[1])\r\n", 817 | "\r\n", 818 | "print(classification_report(y_test,predictions))" 819 | ], 820 | "execution_count": 18, 821 | "outputs": [ 822 | { 823 | "output_type": "stream", 824 | "text": [ 825 | "WARNING:tensorflow:From :2: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.\n", 826 | "Instructions for updating:\n", 827 | "Please use instead:* `np.argmax(model.predict(x), axis=-1)`, if your model does multi-class classification (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype(\"int32\")`, if your model does binary classification (e.g. if it uses a `sigmoid` last-layer activation).\n", 828 | "First Index\n", 829 | "Testing Dataset: [0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]\n", 830 | "Predicted value: 7\n", 831 | "------------------------------------------------\n", 832 | "Second Index\n", 833 | "Testing Dataset: [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]\n", 834 | "Predicted value: 2\n", 835 | " precision recall f1-score support\n", 836 | "\n", 837 | " 0 0.99 0.99 0.99 980\n", 838 | " 1 0.99 0.99 0.99 1135\n", 839 | " 2 0.99 0.98 0.98 1032\n", 840 | " 3 0.99 0.99 0.99 1010\n", 841 | " 4 0.99 0.99 0.99 982\n", 842 | " 5 0.99 0.99 0.99 892\n", 843 | " 6 0.99 0.99 0.99 958\n", 844 | " 7 0.99 0.98 0.99 1028\n", 845 | " 8 0.98 0.99 0.98 974\n", 846 | " 9 0.99 0.98 0.98 1009\n", 847 | "\n", 848 | " accuracy 0.99 10000\n", 849 | " macro avg 0.99 0.99 0.99 10000\n", 850 | "weighted avg 0.99 0.99 0.99 10000\n", 851 | "\n" 852 | ], 853 | "name": "stdout" 854 | } 855 | ] 856 | } 857 | ] 858 | } -------------------------------------------------------------------------------- /7. Deep Learning CNN/2_DeepLearning_ConvolutionNeuralNetworks_CIFAR10.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "2_DeepLearning_ConvolutionNeuralNetworks_CIFAR10.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "machine_shape": "hm", 10 | "authorship_tag": "ABX9TyMPW+Bia8eOw6kMT2cnEhRT", 11 | "include_colab_link": true 12 | }, 13 | "kernelspec": { 14 | "name": "python3", 15 | "display_name": "Python 3" 16 | }, 17 | "accelerator": "GPU" 18 | }, 19 | "cells": [ 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "id": "view-in-github", 24 | "colab_type": "text" 25 | }, 26 | "source": [ 27 | "\"Open" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "id": "TGQeTRBlSLym" 34 | }, 35 | "source": [ 36 | "### Deep Learning For Object Detection and Image Classification\r\n", 37 | "\r\n", 38 | "

Keras CNN with CIFAR-10

\r\n", 39 | "\r\n", 40 | "- Color images dataset" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "metadata": { 46 | "colab": { 47 | "base_uri": "https://localhost:8080/" 48 | }, 49 | "id": "EEFZgVSpTAsk", 50 | "outputId": "fcb9471e-41c5-41c2-d47a-c858893dc414" 51 | }, 52 | "source": [ 53 | "# Checking GPU available on Colab\r\n", 54 | "\r\n", 55 | "gpu_info = !nvidia-smi\r\n", 56 | "gpu_info = '\\n'.join(gpu_info)\r\n", 57 | "if gpu_info.find('failed') >= 0:\r\n", 58 | " print('Select the Runtime > \"Change runtime type\" menu to enable a GPU accelerator, ')\r\n", 59 | " print('and then re-execute this cell.')\r\n", 60 | "else:\r\n", 61 | " print(gpu_info)\r\n", 62 | "\r\n", 63 | "from psutil import virtual_memory\r\n", 64 | "ram_gb = virtual_memory().total / 1e9\r\n", 65 | "print('Your runtime has {:.1f} gigabytes of available RAM\\n'.format(ram_gb))\r\n", 66 | "\r\n", 67 | "if ram_gb < 20:\r\n", 68 | " print('To enable a high-RAM runtime, select the Runtime > \"Change runtime type\"')\r\n", 69 | " print('menu, and then select High-RAM in the Runtime shape dropdown. Then, ')\r\n", 70 | " print('re-execute this cell.')\r\n", 71 | "else:\r\n", 72 | " print('You are using a high-RAM runtime!')" 73 | ], 74 | "execution_count": 1, 75 | "outputs": [ 76 | { 77 | "output_type": "stream", 78 | "text": [ 79 | "Sun Dec 13 04:13:02 2020 \n", 80 | "+-----------------------------------------------------------------------------+\n", 81 | "| NVIDIA-SMI 455.45.01 Driver Version: 418.67 CUDA Version: 10.1 |\n", 82 | "|-------------------------------+----------------------+----------------------+\n", 83 | "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n", 84 | "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n", 85 | "| | | MIG M. |\n", 86 | "|===============================+======================+======================|\n", 87 | "| 0 Tesla V100-SXM2... Off | 00000000:00:04.0 Off | 0 |\n", 88 | "| N/A 37C P0 26W / 300W | 0MiB / 16130MiB | 0% Default |\n", 89 | "| | | ERR! |\n", 90 | "+-------------------------------+----------------------+----------------------+\n", 91 | " \n", 92 | "+-----------------------------------------------------------------------------+\n", 93 | "| Processes: |\n", 94 | "| GPU GI CI PID Type Process name GPU Memory |\n", 95 | "| ID ID Usage |\n", 96 | "|=============================================================================|\n", 97 | "| No running processes found |\n", 98 | "+-----------------------------------------------------------------------------+\n", 99 | "Your runtime has 27.4 gigabytes of available RAM\n", 100 | "\n", 101 | "You are using a high-RAM runtime!\n" 102 | ], 103 | "name": "stdout" 104 | } 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": { 110 | "id": "ySsL8QtgV-zI" 111 | }, 112 | "source": [ 113 | "##### With Smaller CNN Model" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "metadata": { 119 | "id": "chzuc-mFSGWv" 120 | }, 121 | "source": [ 122 | "#import libraries\r\n", 123 | "\r\n", 124 | "from keras.datasets import cifar10\r\n", 125 | "import matplotlib.pyplot as plt\r\n", 126 | "%matplotlib inline\r\n", 127 | "\r\n", 128 | "from keras.utils.np_utils import to_categorical\r\n", 129 | "\r\n", 130 | "from keras.models import Sequential\r\n", 131 | "from keras.layers import Dense,Conv2D,MaxPool2D,Flatten\r\n", 132 | "\r\n", 133 | "from sklearn.metrics import classification_report" 134 | ], 135 | "execution_count": 2, 136 | "outputs": [] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "metadata": { 141 | "colab": { 142 | "base_uri": "https://localhost:8080/" 143 | }, 144 | "id": "AaxGB37mSksL", 145 | "outputId": "52c2a639-04dc-4391-bb15-64a24d5567f5" 146 | }, 147 | "source": [ 148 | "(x_train,y_train) , (x_test,y_test) = cifar10.load_data()\r\n", 149 | "print('Training size: ',x_train.shape) # we have 50000 images of training dataset with 32 x 32 pixels of 3 color channels" 150 | ], 151 | "execution_count": 3, 152 | "outputs": [ 153 | { 154 | "output_type": "stream", 155 | "text": [ 156 | "Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\n", 157 | "170500096/170498071 [==============================] - 6s 0us/step\n", 158 | "Training size: (50000, 32, 32, 3)\n" 159 | ], 160 | "name": "stdout" 161 | } 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "metadata": { 167 | "colab": { 168 | "base_uri": "https://localhost:8080/", 169 | "height": 284 170 | }, 171 | "id": "HG_bW_hNTazD", 172 | "outputId": "937ee6d1-4215-4c9f-b0bb-e11ff5465e25" 173 | }, 174 | "source": [ 175 | "single_image = x_train[0]\r\n", 176 | "plt.imshow(single_image) #low resolution" 177 | ], 178 | "execution_count": 4, 179 | "outputs": [ 180 | { 181 | "output_type": "execute_result", 182 | "data": { 183 | "text/plain": [ 184 | "" 185 | ] 186 | }, 187 | "metadata": { 188 | "tags": [] 189 | }, 190 | "execution_count": 4 191 | }, 192 | { 193 | "output_type": "display_data", 194 | "data": { 195 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD5CAYAAADhukOtAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAfMklEQVR4nO2da2yc53Xn/2dunOGdFC+SKNmy5UvtNLbiqIbXyXaTBi3coKgTYJFNPgT+EFRF0QAN0P1gZIFNFtgPyWKTIB8WWSgbt+4im8vm0hiFsW1qpDDaFK7l2PG9tizLkSiKokRS5HCGcz37YcZb2fv8H9IiOVTy/H+AoOF7+LzvmWfe877zPn+ec8zdIYT41Sez2w4IIXqDgl2IRFCwC5EICnYhEkHBLkQiKNiFSITcVgab2X0AvgogC+B/uPsXYr+fz+e9r1gM2lqtFh2XQVgezBo/ViHHr2P5iC2XzVKbWfiAZpFrZsTHZpO/55ggmo35SKTUtrf5sdr8aJaJvIEI7Xb4vcV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M3vKzI7tkg9vMu3uc93X5wFM76IvnzazZ7tf83f8ceJKzOwQOvUTnsAuzsnb/AB6PCc7UeQ19QW697v7XQB+F8Afm9lv7rZDQOfKjs6FaDf4GoDD6PQImAPwpV4d2MwGAXwfwGfc/S2laXo5JwE/ej4nvoUir4zdCPZZAAev+JkWq9xp3H22+/8FAD/E7lbemTezfQDQ/f/Cbjjh7vPdE60N4Ovo0ZyYWR6dAPumu/+gu7nncxLyY7fmpHvsd1zklbEbwf4kgJu7K4sFAB8H8EivnTCzATMbevM1gN8B8Hx81I7yCDqFO4FdLOD5ZnB1+Sh6MCdmZujUMHzJ3b98hamnc8L86PWc7FiR116tML5ttfHD6Kx0vgbgP+ySDzeiowT8HMALvfQDwLfQ+TrYQOfZ61Po9Mx7DMCrAP4WwPgu+fE/ATwH4Fl0gm1fD/x4Pzpf0Z8F8Ez334d7PScRP3o6JwDuQKeI67PoXFj+4xXn7D8BOAngfwPoeyf71V/QCZEIqS/QCZEMCnYhEkHBLkQiKNiFSAQFuxCJoGAXIhEU7EIkgoJdiET4vyrWWZ/xQ9u6AAAAAElFTkSuQmCC\n", 196 | "text/plain": [ 197 | "
" 198 | ] 199 | }, 200 | "metadata": { 201 | "tags": [], 202 | "needs_background": "light" 203 | } 204 | } 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": { 210 | "id": "e6Obz_UVT565" 211 | }, 212 | "source": [ 213 | "##### Pre-processing" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "metadata": { 219 | "colab": { 220 | "base_uri": "https://localhost:8080/" 221 | }, 222 | "id": "EGwmXXQITzDJ", 223 | "outputId": "edb41ab7-fc49-4d41-f06c-2ef10433ae2c" 224 | }, 225 | "source": [ 226 | "x_train = x_train/x_train.max()\r\n", 227 | "x_test = x_test/x_test.max()\r\n", 228 | "\r\n", 229 | "single_image = x_train[0]\r\n", 230 | "single_image.max()" 231 | ], 232 | "execution_count": 5, 233 | "outputs": [ 234 | { 235 | "output_type": "execute_result", 236 | "data": { 237 | "text/plain": [ 238 | "1.0" 239 | ] 240 | }, 241 | "metadata": { 242 | "tags": [] 243 | }, 244 | "execution_count": 5 245 | } 246 | ] 247 | }, 248 | { 249 | "cell_type": "code", 250 | "metadata": { 251 | "colab": { 252 | "base_uri": "https://localhost:8080/" 253 | }, 254 | "id": "MVXgt1uMUEcq", 255 | "outputId": "47281d7f-6b35-480d-c31d-e46a31055958" 256 | }, 257 | "source": [ 258 | "x_test.shape #10000 images" 259 | ], 260 | "execution_count": 6, 261 | "outputs": [ 262 | { 263 | "output_type": "execute_result", 264 | "data": { 265 | "text/plain": [ 266 | "(10000, 32, 32, 3)" 267 | ] 268 | }, 269 | "metadata": { 270 | "tags": [] 271 | }, 272 | "execution_count": 6 273 | } 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "metadata": { 279 | "colab": { 280 | "base_uri": "https://localhost:8080/" 281 | }, 282 | "id": "hcSlhN-cUHdC", 283 | "outputId": "a73e2264-8ac4-4cd0-d971-eeb61dcb4990" 284 | }, 285 | "source": [ 286 | "y_cat_test = to_categorical(y_test,10)\r\n", 287 | "y_cat_train = to_categorical(y_train,10)\r\n", 288 | "\r\n", 289 | "print('First Index')\r\n", 290 | "print(y_train[0])\r\n", 291 | "print(y_cat_train[0])\r\n", 292 | "print('------------------------------------------------')\r\n", 293 | "print('Second Index')\r\n", 294 | "print(y_train[1])\r\n", 295 | "print(y_cat_train[1])" 296 | ], 297 | "execution_count": 7, 298 | "outputs": [ 299 | { 300 | "output_type": "stream", 301 | "text": [ 302 | "First Index\n", 303 | "[6]\n", 304 | "[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]\n", 305 | "------------------------------------------------\n", 306 | "Second Index\n", 307 | "[9]\n", 308 | "[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]\n" 309 | ], 310 | "name": "stdout" 311 | } 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "metadata": { 317 | "id": "ADXJFpyEUNJC" 318 | }, 319 | "source": [ 320 | "model = Sequential() #create a model\r\n", 321 | "\r\n", 322 | "#Conv Layer\r\n", 323 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 324 | " input_shape=(32,32,3),activation='relu'))\r\n", 325 | "#Pooling layer\r\n", 326 | "model.add(MaxPool2D(pool_size=(2,2)))\r\n", 327 | "\r\n", 328 | "#----------------------------------------------------#\r\n", 329 | "\r\n", 330 | "#Conv Layer\r\n", 331 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 332 | " input_shape=(32,32,3),activation='relu'))\r\n", 333 | "#Pooling layer\r\n", 334 | "model.add(MaxPool2D(pool_size=(2,2)))\r\n", 335 | "\r\n", 336 | "#----------------------------------------------------#\r\n", 337 | "\r\n", 338 | "#Flatten out to understand Dense layer (2D-->1D)\r\n", 339 | "model.add(Flatten())\r\n", 340 | "\r\n", 341 | "#Dense Layer\r\n", 342 | "model.add(Dense(256,activation='relu'))\r\n", 343 | "\r\n", 344 | "#Output Layer\r\n", 345 | "model.add(Dense(10,activation='softmax'))\r\n", 346 | "\r\n", 347 | "model.compile(loss='categorical_crossentropy',\r\n", 348 | " optimizer='rmsprop',\r\n", 349 | " metrics=['accuracy'])" 350 | ], 351 | "execution_count": 8, 352 | "outputs": [] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "metadata": { 357 | "colab": { 358 | "base_uri": "https://localhost:8080/" 359 | }, 360 | "id": "ekOQC6lZU_S6", 361 | "outputId": "4a0831e4-2a2c-46c2-e2af-04c2cc19c567" 362 | }, 363 | "source": [ 364 | "model.summary()" 365 | ], 366 | "execution_count": 9, 367 | "outputs": [ 368 | { 369 | "output_type": "stream", 370 | "text": [ 371 | "Model: \"sequential\"\n", 372 | "_________________________________________________________________\n", 373 | "Layer (type) Output Shape Param # \n", 374 | "=================================================================\n", 375 | "conv2d (Conv2D) (None, 29, 29, 32) 1568 \n", 376 | "_________________________________________________________________\n", 377 | "max_pooling2d (MaxPooling2D) (None, 14, 14, 32) 0 \n", 378 | "_________________________________________________________________\n", 379 | "conv2d_1 (Conv2D) (None, 11, 11, 32) 16416 \n", 380 | "_________________________________________________________________\n", 381 | "max_pooling2d_1 (MaxPooling2 (None, 5, 5, 32) 0 \n", 382 | "_________________________________________________________________\n", 383 | "flatten (Flatten) (None, 800) 0 \n", 384 | "_________________________________________________________________\n", 385 | "dense (Dense) (None, 256) 205056 \n", 386 | "_________________________________________________________________\n", 387 | "dense_1 (Dense) (None, 10) 2570 \n", 388 | "=================================================================\n", 389 | "Total params: 225,610\n", 390 | "Trainable params: 225,610\n", 391 | "Non-trainable params: 0\n", 392 | "_________________________________________________________________\n" 393 | ], 394 | "name": "stdout" 395 | } 396 | ] 397 | }, 398 | { 399 | "cell_type": "code", 400 | "metadata": { 401 | "colab": { 402 | "base_uri": "https://localhost:8080/" 403 | }, 404 | "id": "L-FNFqMQVBBa", 405 | "outputId": "cb32afa9-54f7-40ac-f7b4-3369d0845527" 406 | }, 407 | "source": [ 408 | "#Train model\r\n", 409 | "\r\n", 410 | "model.fit(x_train,y_cat_train,epochs=50,verbose=1)" 411 | ], 412 | "execution_count": 10, 413 | "outputs": [ 414 | { 415 | "output_type": "stream", 416 | "text": [ 417 | "Epoch 1/50\n", 418 | "1563/1563 [==============================] - 5s 3ms/step - loss: 1.5220 - accuracy: 0.4546\n", 419 | "Epoch 2/50\n", 420 | "1563/1563 [==============================] - 5s 3ms/step - loss: 1.1645 - accuracy: 0.5930\n", 421 | "Epoch 3/50\n", 422 | "1563/1563 [==============================] - 5s 3ms/step - loss: 1.0066 - accuracy: 0.6505\n", 423 | "Epoch 4/50\n", 424 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.9008 - accuracy: 0.6902\n", 425 | "Epoch 5/50\n", 426 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.8161 - accuracy: 0.7211\n", 427 | "Epoch 6/50\n", 428 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.7505 - accuracy: 0.7455\n", 429 | "Epoch 7/50\n", 430 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.7006 - accuracy: 0.7616\n", 431 | "Epoch 8/50\n", 432 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.6512 - accuracy: 0.7785\n", 433 | "Epoch 9/50\n", 434 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.6184 - accuracy: 0.7924\n", 435 | "Epoch 10/50\n", 436 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.5862 - accuracy: 0.8025\n", 437 | "Epoch 11/50\n", 438 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.5634 - accuracy: 0.8133\n", 439 | "Epoch 12/50\n", 440 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.5338 - accuracy: 0.8210\n", 441 | "Epoch 13/50\n", 442 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.5210 - accuracy: 0.8272\n", 443 | "Epoch 14/50\n", 444 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.4986 - accuracy: 0.8342\n", 445 | "Epoch 15/50\n", 446 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.4801 - accuracy: 0.8394\n", 447 | "Epoch 16/50\n", 448 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4709 - accuracy: 0.8460\n", 449 | "Epoch 17/50\n", 450 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4484 - accuracy: 0.8499\n", 451 | "Epoch 18/50\n", 452 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4422 - accuracy: 0.8547\n", 453 | "Epoch 19/50\n", 454 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4347 - accuracy: 0.8600\n", 455 | "Epoch 20/50\n", 456 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4171 - accuracy: 0.8645\n", 457 | "Epoch 21/50\n", 458 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4069 - accuracy: 0.8691\n", 459 | "Epoch 22/50\n", 460 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.4056 - accuracy: 0.8715\n", 461 | "Epoch 23/50\n", 462 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3874 - accuracy: 0.8768\n", 463 | "Epoch 24/50\n", 464 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3832 - accuracy: 0.8794\n", 465 | "Epoch 25/50\n", 466 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3728 - accuracy: 0.8814\n", 467 | "Epoch 26/50\n", 468 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3752 - accuracy: 0.8813\n", 469 | "Epoch 27/50\n", 470 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3786 - accuracy: 0.8835\n", 471 | "Epoch 28/50\n", 472 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3692 - accuracy: 0.8865\n", 473 | "Epoch 29/50\n", 474 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3673 - accuracy: 0.8883\n", 475 | "Epoch 30/50\n", 476 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3620 - accuracy: 0.8897\n", 477 | "Epoch 31/50\n", 478 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3517 - accuracy: 0.8942\n", 479 | "Epoch 32/50\n", 480 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3553 - accuracy: 0.8920\n", 481 | "Epoch 33/50\n", 482 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3465 - accuracy: 0.8941\n", 483 | "Epoch 34/50\n", 484 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3432 - accuracy: 0.8975\n", 485 | "Epoch 35/50\n", 486 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3414 - accuracy: 0.9002\n", 487 | "Epoch 36/50\n", 488 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3300 - accuracy: 0.9035\n", 489 | "Epoch 37/50\n", 490 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3224 - accuracy: 0.9064\n", 491 | "Epoch 38/50\n", 492 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3270 - accuracy: 0.9066\n", 493 | "Epoch 39/50\n", 494 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3182 - accuracy: 0.9089\n", 495 | "Epoch 40/50\n", 496 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3163 - accuracy: 0.9090\n", 497 | "Epoch 41/50\n", 498 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3222 - accuracy: 0.9105\n", 499 | "Epoch 42/50\n", 500 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3218 - accuracy: 0.9118\n", 501 | "Epoch 43/50\n", 502 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3211 - accuracy: 0.9124\n", 503 | "Epoch 44/50\n", 504 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3196 - accuracy: 0.9138\n", 505 | "Epoch 45/50\n", 506 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3135 - accuracy: 0.9147\n", 507 | "Epoch 46/50\n", 508 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3071 - accuracy: 0.9159\n", 509 | "Epoch 47/50\n", 510 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.2970 - accuracy: 0.9174\n", 511 | "Epoch 48/50\n", 512 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3111 - accuracy: 0.9165\n", 513 | "Epoch 49/50\n", 514 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3055 - accuracy: 0.9208\n", 515 | "Epoch 50/50\n", 516 | "1563/1563 [==============================] - 5s 3ms/step - loss: 0.3020 - accuracy: 0.9195\n" 517 | ], 518 | "name": "stdout" 519 | }, 520 | { 521 | "output_type": "execute_result", 522 | "data": { 523 | "text/plain": [ 524 | "" 525 | ] 526 | }, 527 | "metadata": { 528 | "tags": [] 529 | }, 530 | "execution_count": 10 531 | } 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "metadata": { 537 | "colab": { 538 | "base_uri": "https://localhost:8080/" 539 | }, 540 | "id": "-UYqEfjAVQ1r", 541 | "outputId": "fc4c117d-9bfa-4265-af72-7b2207ef199e" 542 | }, 543 | "source": [ 544 | "print('Metrics used: ', model.metrics_names)\r\n", 545 | "\r\n", 546 | "#evaluate using test dataset\r\n", 547 | "\r\n", 548 | "model.evaluate(x_test,y_cat_test)" 549 | ], 550 | "execution_count": 11, 551 | "outputs": [ 552 | { 553 | "output_type": "stream", 554 | "text": [ 555 | "Metrics used: ['loss', 'accuracy']\n", 556 | "313/313 [==============================] - 1s 2ms/step - loss: 4.0851 - accuracy: 0.6779\n" 557 | ], 558 | "name": "stdout" 559 | }, 560 | { 561 | "output_type": "execute_result", 562 | "data": { 563 | "text/plain": [ 564 | "[4.085117816925049, 0.6779000163078308]" 565 | ] 566 | }, 567 | "metadata": { 568 | "tags": [] 569 | }, 570 | "execution_count": 11 571 | } 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "metadata": { 577 | "colab": { 578 | "base_uri": "https://localhost:8080/" 579 | }, 580 | "id": "UjEiMsSbVa2K", 581 | "outputId": "91e2da72-6529-4982-c855-84776ef74907" 582 | }, 583 | "source": [ 584 | "predictions = model.predict_classes(x_test)\r\n", 585 | "\r\n", 586 | "print('First Index')\r\n", 587 | "print('Testing Dataset: ',y_cat_test[0])\r\n", 588 | "print('Predicted value: ',predictions[0])\r\n", 589 | "print('------------------------------------------------')\r\n", 590 | "print('Second Index')\r\n", 591 | "print('Testing Dataset: ',y_cat_test[1])\r\n", 592 | "print('Predicted value: ',predictions[1])\r\n", 593 | "\r\n", 594 | "print(classification_report(y_test,predictions))" 595 | ], 596 | "execution_count": 12, 597 | "outputs": [ 598 | { 599 | "output_type": "stream", 600 | "text": [ 601 | "WARNING:tensorflow:From :1: Sequential.predict_classes (from tensorflow.python.keras.engine.sequential) is deprecated and will be removed after 2021-01-01.\n", 602 | "Instructions for updating:\n", 603 | "Please use instead:* `np.argmax(model.predict(x), axis=-1)`, if your model does multi-class classification (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype(\"int32\")`, if your model does binary classification (e.g. if it uses a `sigmoid` last-layer activation).\n", 604 | "First Index\n", 605 | "Testing Dataset: [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n", 606 | "Predicted value: 3\n", 607 | "------------------------------------------------\n", 608 | "Second Index\n", 609 | "Testing Dataset: [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n", 610 | "Predicted value: 8\n", 611 | " precision recall f1-score support\n", 612 | "\n", 613 | " 0 0.78 0.60 0.68 1000\n", 614 | " 1 0.78 0.84 0.81 1000\n", 615 | " 2 0.59 0.58 0.58 1000\n", 616 | " 3 0.48 0.45 0.47 1000\n", 617 | " 4 0.64 0.60 0.62 1000\n", 618 | " 5 0.54 0.64 0.58 1000\n", 619 | " 6 0.75 0.73 0.74 1000\n", 620 | " 7 0.74 0.74 0.74 1000\n", 621 | " 8 0.77 0.80 0.79 1000\n", 622 | " 9 0.73 0.81 0.77 1000\n", 623 | "\n", 624 | " accuracy 0.68 10000\n", 625 | " macro avg 0.68 0.68 0.68 10000\n", 626 | "weighted avg 0.68 0.68 0.68 10000\n", 627 | "\n" 628 | ], 629 | "name": "stdout" 630 | } 631 | ] 632 | }, 633 | { 634 | "cell_type": "markdown", 635 | "metadata": { 636 | "id": "X9UPk2UDWHvH" 637 | }, 638 | "source": [ 639 | "##### With Larger Model" 640 | ] 641 | }, 642 | { 643 | "cell_type": "code", 644 | "metadata": { 645 | "id": "-3053tRdWHJA" 646 | }, 647 | "source": [ 648 | "model = Sequential() #create a model\r\n", 649 | "\r\n", 650 | "#Conv Layer\r\n", 651 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 652 | " input_shape=(32,32,3),activation='relu'))\r\n", 653 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 654 | " input_shape=(32,32,3),activation='relu'))\r\n", 655 | "#Pooling layer\r\n", 656 | "model.add(MaxPool2D(pool_size=(2,2)))\r\n", 657 | "\r\n", 658 | "#----------------------------------------------------#\r\n", 659 | "\r\n", 660 | "#Conv Layer\r\n", 661 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 662 | " input_shape=(32,32,3),activation='relu'))\r\n", 663 | "model.add(Conv2D(filters=32,kernel_size=(4,4),\r\n", 664 | " input_shape=(32,32,3),activation='relu'))\r\n", 665 | "#Pooling layer\r\n", 666 | "model.add(MaxPool2D(pool_size=(2,2)))\r\n", 667 | "\r\n", 668 | "#----------------------------------------------------#\r\n", 669 | "\r\n", 670 | "#Flatten out to understand Dense layer (2D-->1D)\r\n", 671 | "model.add(Flatten())\r\n", 672 | "\r\n", 673 | "#Dense Layer\r\n", 674 | "model.add(Dense(512,activation='relu'))\r\n", 675 | "\r\n", 676 | "#Output Layer\r\n", 677 | "model.add(Dense(10,activation='softmax'))\r\n", 678 | "\r\n", 679 | "model.compile(loss='categorical_crossentropy',\r\n", 680 | " optimizer='rmsprop',\r\n", 681 | " metrics=['accuracy'])" 682 | ], 683 | "execution_count": 13, 684 | "outputs": [] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "metadata": { 689 | "colab": { 690 | "base_uri": "https://localhost:8080/" 691 | }, 692 | "id": "Ks3n9xFOWm4f", 693 | "outputId": "6d409e00-9756-4f7e-d169-5e6dd6edcbab" 694 | }, 695 | "source": [ 696 | "model.summary()" 697 | ], 698 | "execution_count": 14, 699 | "outputs": [ 700 | { 701 | "output_type": "stream", 702 | "text": [ 703 | "Model: \"sequential_1\"\n", 704 | "_________________________________________________________________\n", 705 | "Layer (type) Output Shape Param # \n", 706 | "=================================================================\n", 707 | "conv2d_2 (Conv2D) (None, 29, 29, 32) 1568 \n", 708 | "_________________________________________________________________\n", 709 | "conv2d_3 (Conv2D) (None, 26, 26, 32) 16416 \n", 710 | "_________________________________________________________________\n", 711 | "max_pooling2d_2 (MaxPooling2 (None, 13, 13, 32) 0 \n", 712 | "_________________________________________________________________\n", 713 | "conv2d_4 (Conv2D) (None, 10, 10, 32) 16416 \n", 714 | "_________________________________________________________________\n", 715 | "conv2d_5 (Conv2D) (None, 7, 7, 32) 16416 \n", 716 | "_________________________________________________________________\n", 717 | "max_pooling2d_3 (MaxPooling2 (None, 3, 3, 32) 0 \n", 718 | "_________________________________________________________________\n", 719 | "flatten_1 (Flatten) (None, 288) 0 \n", 720 | "_________________________________________________________________\n", 721 | "dense_2 (Dense) (None, 512) 147968 \n", 722 | "_________________________________________________________________\n", 723 | "dense_3 (Dense) (None, 10) 5130 \n", 724 | "=================================================================\n", 725 | "Total params: 203,914\n", 726 | "Trainable params: 203,914\n", 727 | "Non-trainable params: 0\n", 728 | "_________________________________________________________________\n" 729 | ], 730 | "name": "stdout" 731 | } 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "metadata": { 737 | "colab": { 738 | "base_uri": "https://localhost:8080/" 739 | }, 740 | "id": "cMsFCl2KWoi3", 741 | "outputId": "40840163-8596-4ec7-f36f-b95f2c333920" 742 | }, 743 | "source": [ 744 | "#Train model\r\n", 745 | "\r\n", 746 | "model.fit(x_train,y_cat_train,epochs=50,verbose=1)" 747 | ], 748 | "execution_count": 15, 749 | "outputs": [ 750 | { 751 | "output_type": "stream", 752 | "text": [ 753 | "Epoch 1/50\n", 754 | "1563/1563 [==============================] - 7s 4ms/step - loss: 1.6236 - accuracy: 0.4134\n", 755 | "Epoch 2/50\n", 756 | "1563/1563 [==============================] - 7s 4ms/step - loss: 1.2469 - accuracy: 0.5572\n", 757 | "Epoch 3/50\n", 758 | "1563/1563 [==============================] - 6s 4ms/step - loss: 1.0996 - accuracy: 0.6135\n", 759 | "Epoch 4/50\n", 760 | "1563/1563 [==============================] - 6s 4ms/step - loss: 1.0043 - accuracy: 0.6519\n", 761 | "Epoch 5/50\n", 762 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9579 - accuracy: 0.6699\n", 763 | "Epoch 6/50\n", 764 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9354 - accuracy: 0.6827\n", 765 | "Epoch 7/50\n", 766 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9263 - accuracy: 0.6849\n", 767 | "Epoch 8/50\n", 768 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9208 - accuracy: 0.6877\n", 769 | "Epoch 9/50\n", 770 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9194 - accuracy: 0.6929\n", 771 | "Epoch 10/50\n", 772 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9214 - accuracy: 0.6872\n", 773 | "Epoch 11/50\n", 774 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9117 - accuracy: 0.6917\n", 775 | "Epoch 12/50\n", 776 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9093 - accuracy: 0.6952\n", 777 | "Epoch 13/50\n", 778 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9106 - accuracy: 0.6945\n", 779 | "Epoch 14/50\n", 780 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9171 - accuracy: 0.6927\n", 781 | "Epoch 15/50\n", 782 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9144 - accuracy: 0.6953\n", 783 | "Epoch 16/50\n", 784 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9152 - accuracy: 0.6949\n", 785 | "Epoch 17/50\n", 786 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9153 - accuracy: 0.6963\n", 787 | "Epoch 18/50\n", 788 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9222 - accuracy: 0.6930\n", 789 | "Epoch 19/50\n", 790 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9211 - accuracy: 0.6937\n", 791 | "Epoch 20/50\n", 792 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9218 - accuracy: 0.6945\n", 793 | "Epoch 21/50\n", 794 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9210 - accuracy: 0.6944\n", 795 | "Epoch 22/50\n", 796 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9255 - accuracy: 0.6940\n", 797 | "Epoch 23/50\n", 798 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9182 - accuracy: 0.6967\n", 799 | "Epoch 24/50\n", 800 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9226 - accuracy: 0.6955\n", 801 | "Epoch 25/50\n", 802 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9463 - accuracy: 0.6989\n", 803 | "Epoch 26/50\n", 804 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9094 - accuracy: 0.6996\n", 805 | "Epoch 27/50\n", 806 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8969 - accuracy: 0.7024\n", 807 | "Epoch 28/50\n", 808 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9106 - accuracy: 0.6998\n", 809 | "Epoch 29/50\n", 810 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9059 - accuracy: 0.7031\n", 811 | "Epoch 30/50\n", 812 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8905 - accuracy: 0.7059\n", 813 | "Epoch 31/50\n", 814 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8830 - accuracy: 0.7099\n", 815 | "Epoch 32/50\n", 816 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8819 - accuracy: 0.7085\n", 817 | "Epoch 33/50\n", 818 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8661 - accuracy: 0.7145\n", 819 | "Epoch 34/50\n", 820 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8920 - accuracy: 0.7120\n", 821 | "Epoch 35/50\n", 822 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8635 - accuracy: 0.7168\n", 823 | "Epoch 36/50\n", 824 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8785 - accuracy: 0.7120\n", 825 | "Epoch 37/50\n", 826 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8632 - accuracy: 0.7171\n", 827 | "Epoch 38/50\n", 828 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8651 - accuracy: 0.7184\n", 829 | "Epoch 39/50\n", 830 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8658 - accuracy: 0.7186\n", 831 | "Epoch 40/50\n", 832 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8609 - accuracy: 0.7187\n", 833 | "Epoch 41/50\n", 834 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8733 - accuracy: 0.7188\n", 835 | "Epoch 42/50\n", 836 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8909 - accuracy: 0.7134\n", 837 | "Epoch 43/50\n", 838 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8830 - accuracy: 0.7132\n", 839 | "Epoch 44/50\n", 840 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9068 - accuracy: 0.7164\n", 841 | "Epoch 45/50\n", 842 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8927 - accuracy: 0.7133\n", 843 | "Epoch 46/50\n", 844 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8873 - accuracy: 0.7127\n", 845 | "Epoch 47/50\n", 846 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8915 - accuracy: 0.7132\n", 847 | "Epoch 48/50\n", 848 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9123 - accuracy: 0.7077\n", 849 | "Epoch 49/50\n", 850 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.8991 - accuracy: 0.7106\n", 851 | "Epoch 50/50\n", 852 | "1563/1563 [==============================] - 6s 4ms/step - loss: 0.9303 - accuracy: 0.7047\n" 853 | ], 854 | "name": "stdout" 855 | }, 856 | { 857 | "output_type": "execute_result", 858 | "data": { 859 | "text/plain": [ 860 | "" 861 | ] 862 | }, 863 | "metadata": { 864 | "tags": [] 865 | }, 866 | "execution_count": 15 867 | } 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "metadata": { 873 | "colab": { 874 | "base_uri": "https://localhost:8080/" 875 | }, 876 | "id": "zkXcnKk_WuBu", 877 | "outputId": "59c18bcc-57e6-4132-ace0-05e9363b4078" 878 | }, 879 | "source": [ 880 | "print('Metrics used: ', model.metrics_names)\r\n", 881 | "\r\n", 882 | "#evaluate using test dataset\r\n", 883 | "\r\n", 884 | "model.evaluate(x_test,y_cat_test)" 885 | ], 886 | "execution_count": 16, 887 | "outputs": [ 888 | { 889 | "output_type": "stream", 890 | "text": [ 891 | "Metrics used: ['loss', 'accuracy']\n", 892 | "313/313 [==============================] - 1s 2ms/step - loss: 1.3410 - accuracy: 0.6346\n" 893 | ], 894 | "name": "stdout" 895 | }, 896 | { 897 | "output_type": "execute_result", 898 | "data": { 899 | "text/plain": [ 900 | "[1.3409852981567383, 0.6345999836921692]" 901 | ] 902 | }, 903 | "metadata": { 904 | "tags": [] 905 | }, 906 | "execution_count": 16 907 | } 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "metadata": { 913 | "colab": { 914 | "base_uri": "https://localhost:8080/" 915 | }, 916 | "id": "rVTvqHluWzlH", 917 | "outputId": "1eb5c6f3-7aed-4bca-ffb0-3f1e6b5b05f3" 918 | }, 919 | "source": [ 920 | "predictions = model.predict_classes(x_test)\r\n", 921 | "\r\n", 922 | "print('First Index')\r\n", 923 | "print('Testing Dataset: ',y_cat_test[0])\r\n", 924 | "print('Predicted value: ',predictions[0])\r\n", 925 | "print('------------------------------------------------')\r\n", 926 | "print('Second Index')\r\n", 927 | "print('Testing Dataset: ',y_cat_test[1])\r\n", 928 | "print('Predicted value: ',predictions[1])\r\n", 929 | "\r\n", 930 | "print(classification_report(y_test,predictions))" 931 | ], 932 | "execution_count": 17, 933 | "outputs": [ 934 | { 935 | "output_type": "stream", 936 | "text": [ 937 | "First Index\n", 938 | "Testing Dataset: [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n", 939 | "Predicted value: 9\n", 940 | "------------------------------------------------\n", 941 | "Second Index\n", 942 | "Testing Dataset: [0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n", 943 | "Predicted value: 1\n", 944 | " precision recall f1-score support\n", 945 | "\n", 946 | " 0 0.46 0.86 0.60 1000\n", 947 | " 1 0.78 0.84 0.81 1000\n", 948 | " 2 0.43 0.65 0.52 1000\n", 949 | " 3 0.81 0.18 0.30 1000\n", 950 | " 4 0.76 0.41 0.53 1000\n", 951 | " 5 0.54 0.67 0.60 1000\n", 952 | " 6 0.81 0.69 0.74 1000\n", 953 | " 7 0.75 0.63 0.69 1000\n", 954 | " 8 0.80 0.67 0.73 1000\n", 955 | " 9 0.75 0.73 0.74 1000\n", 956 | "\n", 957 | " accuracy 0.63 10000\n", 958 | " macro avg 0.69 0.63 0.63 10000\n", 959 | "weighted avg 0.69 0.63 0.63 10000\n", 960 | "\n" 961 | ], 962 | "name": "stdout" 963 | } 964 | ] 965 | } 966 | ] 967 | } -------------------------------------------------------------------------------- /8. Captsone/Capstone_Part_1_VariablesBackgroundFunction.py: -------------------------------------------------------------------------------- 1 | ############################ 2 | ''' 3 | In this project, we will create a program that can detect a hand, 4 | segment the hand and count the number of fingers being held up 5 | 6 | In PART I: 7 | - Define global variables 8 | - Function that updates running average of the background values in an ROI 9 | 10 | Strategy for counting the fingers: 11 | ### Grab ROI 12 | ### Calculate running average background for 60 frames of video 13 | ### Once average value is found, then the hand can enter ROI an apply thresholding etchniques to apply isolation 14 | ### Once the hand enters ROI we will use COnvex Hull to draw polygon around hand 15 | ### Calculate center of hand and calculate angle of center with the outer fingers points 16 | ### Also using some angles we can calculate some distances between fingers. 17 | ''' 18 | 19 | # Import libraries 20 | import cv2 21 | import numpy as np 22 | 23 | from sklearn.metrics import pairwise #distance calculation 24 | 25 | ## Global variables 26 | 27 | background=None 28 | accumulated_weight=0.5 #[0-1] 29 | 30 | roi_top = 20 # setting ROI dimensions on camera 31 | roi_bottom = 300 32 | roi_right = 300 33 | roi_left = 600 34 | 35 | #Function for avg background value 36 | 37 | def calc_accum_avg(frame,accumulated_weight): 38 | 39 | global background #first time it is None 40 | 41 | if background is None: 42 | background = frame.copy().astype('float') 43 | return None 44 | 45 | cv2.accumulateWeighted(frame,background,accumulated_weight) 46 | # The function calculates the weighted sum of the input image src and the accumulator dst so that dst 47 | # # becomes a running average of a frame sequence: 48 | ### Not returning anything in this function, just gloabl variable and updating accumulated weight function. 49 | -------------------------------------------------------------------------------- /8. Captsone/Capstone_Part_2_Segmentation.py: -------------------------------------------------------------------------------- 1 | ############################ 2 | ''' 3 | In this project, we will create a program that can detect a hand, 4 | segment the hand and count the number of fingers being held up 5 | 6 | In PART I: 7 | - Define global variables 8 | - Function that updates running average of the background values in an ROI 9 | 10 | Strategy for counting the fingers: 11 | ### Grab ROI 12 | ### Calculate running average background for 60 frames of video 13 | ### Once average value is found, then the hand can enter ROI an apply thresholding etchniques to apply isolation 14 | ### Once the hand enters ROI we will use COnvex Hull to draw polygon around hand 15 | ### Calculate center of hand and calculate angle of center with the outer fingers points 16 | ### Also using some angles we can calculate some distances between fingers. 17 | ''' 18 | 19 | # Import libraries 20 | import cv2 21 | import numpy as np 22 | 23 | from sklearn.metrics import pairwise #distance calculation 24 | 25 | ## Global variables 26 | 27 | background=None 28 | accumulated_weight=0.5 #[0-1] 29 | 30 | roi_top = 20 # setting ROI dimensions on camera 31 | roi_bottom = 300 32 | roi_right = 300 33 | roi_left = 600 34 | 35 | #Function for avg background value 36 | 37 | def calc_accum_avg(frame,accumulated_weight): 38 | 39 | global background #first time it is None 40 | 41 | if background is None: 42 | background = frame.copy().astype('float') 43 | return None 44 | 45 | cv2.accumulateWeighted(frame,background,accumulated_weight) 46 | # The function calculates the weighted sum of the input image src and the accumulator dst so that dst 47 | # # becomes a running average of a frame sequence: 48 | ### Not returning anything in this function, just gloabl variable and updating accumulated weight function. 49 | 50 | ################################################################################################################# 51 | ################################################################################################################ 52 | ### PART II - SEGMENTATION 53 | #need to check this threshold value for different objects 54 | def segment(frame,threshold_min=25): 55 | 56 | diff = cv2.absdiff(background.astype('uint8'),frame) #absolute difference 57 | 58 | ret,thresholded = cv2.threshold(diff,threshold_min,255,cv2.THRESH_BINARY) #min value 25 59 | 60 | contours,hierarchy = cv2.findContours(thresholded.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)# to grab the image and contours 61 | 62 | if len(contours) == 0: # if we didn't grab any contours 63 | return None 64 | 65 | 66 | else: 67 | #Assuming largest external contour in ROI is HAND 68 | hand_segment = max(contours,key=cv2.contourArea) 69 | 70 | return(thresholded,hand_segment) 71 | -------------------------------------------------------------------------------- /8. Captsone/Capstone_Part_3_Counting_ConvexHull.py: -------------------------------------------------------------------------------- 1 | ############################ 2 | ''' 3 | In this project, we will create a program that can detect a hand, 4 | segment the hand and count the number of fingers being held up 5 | 6 | In PART I: 7 | - Define global variables 8 | - Function that updates running average of the background values in an ROI 9 | 10 | Strategy for counting the fingers: 11 | ### Grab ROI 12 | ### Calculate running average background for 60 frames of video 13 | ### Once average value is found, then the hand can enter ROI an apply thresholding etchniques to apply isolation 14 | ### Once the hand enters ROI we will use COnvex Hull to draw polygon around hand 15 | ### Calculate center of hand and calculate angle of center with the outer fingers points 16 | ### Also using some angles we can calculate some distances between fingers. 17 | ''' 18 | 19 | # Import libraries 20 | import cv2 21 | import numpy as np 22 | 23 | from sklearn.metrics import pairwise #distance calculation 24 | 25 | ## Global variables 26 | 27 | background=None 28 | accumulated_weight=0.5 #[0-1] 29 | 30 | roi_top = 20 # setting ROI dimensions on camera 31 | roi_bottom = 300 32 | roi_right = 300 33 | roi_left = 600 34 | 35 | #Function for avg background value 36 | 37 | def calc_accum_avg(frame,accumulated_weight): 38 | 39 | global background #first time it is None 40 | 41 | if background is None: 42 | background = frame.copy().astype('float') 43 | return None 44 | 45 | cv2.accumulateWeighted(frame,background,accumulated_weight) 46 | # The function calculates the weighted sum of the input image src and the accumulator dst so that dst 47 | # # becomes a running average of a frame sequence: 48 | ### Not returning anything in this function, just gloabl variable and updating accumulated weight function. 49 | 50 | ################################################################################################################# 51 | ################################################################################################################ 52 | ### PART II - SEGMENTATION 53 | #need to check this threshold value for different objects 54 | def segment(frame,threshold_min=25): 55 | 56 | diff = cv2.absdiff(background.astype('uint8'),frame) #absolute difference 57 | 58 | ret,thresholded = cv2.threshold(diff,threshold_min,255,cv2.THRESH_BINARY) #min value 25 59 | 60 | contours,hierarchy = cv2.findContours(thresholded.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)# to grab the image and contours 61 | 62 | if len(contours) == 0: # if we didn't grab any contours 63 | return None 64 | 65 | 66 | else: 67 | #Assuming largest external contour in ROI is HAND 68 | hand_segment = max(contours,key=cv2.contourArea) 69 | 70 | return(thresholded,hand_segment) 71 | 72 | ################################################################################################################# 73 | ################################################################################################################ 74 | ### PART III - COUNTING FINGERS AND CONVEXHULL 75 | 76 | def count_fingers(thresholded,hand_segment): 77 | 78 | conv_hull = cv2.convexHull(hand_segment) 79 | 80 | #EXTREME POINTS 81 | top = tuple(conv_hull[conv_hull[:,:,1].argmin()][0]) #most extreme top point 82 | bottom= tuple(conv_hull[conv_hull[:,:,1].argmax()][0]) 83 | left = tuple(conv_hull[conv_hull[:,:,0].argmin()][0]) 84 | right = tuple(conv_hull[conv_hull[:,:,0].argmax()][0]) 85 | 86 | cX = (left[0]+right[0])// 2 87 | cY = (top[1]+bottom[1])// 2 88 | 89 | distance = pairwise.euclidean_distances([[cX,cY]],[left,right,top,bottom])[0] #diatance between fingers 90 | 91 | max_distance = distance.max() 92 | 93 | #Create a circle around max distance 94 | 95 | radius = int(0.9*max_distance) #90% of max distance 96 | circumfrence = (2*np.pi*radius) 97 | 98 | circular_roi = np.zeros(thresholded.shape[:2],dtype='uint8') 99 | 100 | cv2.circle(circular_roi,(cX,cY),radius,255,10) 101 | 102 | circular_roi = cv2.bitwise_and(thresholded,thresholded,mask=circular_roi) 103 | 104 | #grab contours in ROI 105 | 106 | contours,hierarchy = cv2.findContours(circular_roi.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 107 | 108 | count = 0 #no figners are extended 109 | 110 | for cnt in contours: 111 | 112 | (x,y,w,h) = cv2.boundingRect(cnt) 113 | #to check contour is not at very bottom region 114 | out_of_wrist = (cY+(cY*0.25)) > (y+h) 115 | 116 | #doesn't exceed 25 of points outside 117 | 118 | limit_points = ((circumfrence*0.25) > cnt.shape[0]) 119 | 120 | if out_of_wrist and limit_points: 121 | count+=1 122 | 123 | return count 124 | -------------------------------------------------------------------------------- /8. Captsone/Capstone_Part_4_Final.py: -------------------------------------------------------------------------------- 1 | ############################ 2 | ''' 3 | In this project, we will create a program that can detect a hand, 4 | segment the hand and count the number of fingers being held up 5 | 6 | In PART I: 7 | - Define global variables 8 | - Function that updates running average of the background values in an ROI 9 | 10 | Strategy for counting the fingers: 11 | ### Grab ROI 12 | ### Calculate running average background for 60 frames of video 13 | ### Once average value is found, then the hand can enter ROI an apply thresholding etchniques to apply isolation 14 | ### Once the hand enters ROI we will use COnvex Hull to draw polygon around hand 15 | ### Calculate center of hand and calculate angle of center with the outer fingers points 16 | ### Also using some angles we can calculate some distances between fingers. 17 | ''' 18 | 19 | # Import libraries 20 | import cv2 21 | import numpy as np 22 | 23 | from sklearn.metrics import pairwise #distance calculation 24 | 25 | ## Global variables 26 | 27 | background=None 28 | accumulated_weight=0.5 #[0-1] 29 | 30 | roi_top = 20 # setting ROI dimensions on camera 31 | roi_bottom = 300 32 | roi_right = 300 33 | roi_left = 600 34 | 35 | #Function for avg background value 36 | 37 | def calc_accum_avg(frame,accumulated_weight): 38 | 39 | global background #first time it is None 40 | 41 | if background is None: 42 | background = frame.copy().astype('float') 43 | return None 44 | 45 | cv2.accumulateWeighted(frame,background,accumulated_weight) 46 | # The function calculates the weighted sum of the input image src and the accumulator dst so that dst 47 | # # becomes a running average of a frame sequence: 48 | ### Not returning anything in this function, just gloabl variable and updating accumulated weight function. 49 | 50 | ################################################################################################################# 51 | ################################################################################################################ 52 | ### PART II - SEGMENTATION 53 | #need to check this threshold value for different objects 54 | def segment(frame,threshold_min=25): 55 | 56 | diff = cv2.absdiff(background.astype('uint8'),frame) #absolute difference 57 | 58 | ret,thresholded = cv2.threshold(diff,threshold_min,255,cv2.THRESH_BINARY) #min value 25 59 | 60 | contours,hierarchy = cv2.findContours(thresholded.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)# to grab the image and contours 61 | 62 | if len(contours) == 0: # if we didn't grab any contours 63 | return None 64 | 65 | 66 | else: 67 | #Assuming largest external contour in ROI is HAND 68 | hand_segment = max(contours,key=cv2.contourArea) 69 | 70 | return(thresholded,hand_segment) 71 | 72 | ################################################################################################################# 73 | ################################################################################################################ 74 | ### PART III - COUNTING FINGERS AND CONVEXHULL 75 | 76 | def count_fingers(thresholded,hand_segment): 77 | 78 | conv_hull = cv2.convexHull(hand_segment) 79 | 80 | #EXTREME POINTS 81 | top = tuple(conv_hull[conv_hull[:,:,1].argmin()][0]) #most extreme top point 82 | bottom= tuple(conv_hull[conv_hull[:,:,1].argmax()][0]) 83 | left = tuple(conv_hull[conv_hull[:,:,0].argmin()][0]) 84 | right = tuple(conv_hull[conv_hull[:,:,0].argmax()][0]) 85 | 86 | cX = (left[0]+right[0])// 2 87 | cY = (top[1]+bottom[1])// 2 88 | 89 | distance = pairwise.euclidean_distances([[cX,cY]],[left,right,top,bottom])[0] #diatance between fingers 90 | 91 | max_distance = distance.max() 92 | 93 | #Create a circle around max distance 94 | 95 | radius = int(0.9*max_distance) #90% of max distance 96 | circumfrence = (2*np.pi*radius) 97 | 98 | circular_roi = np.zeros(thresholded.shape[:2],dtype='uint8') 99 | 100 | cv2.circle(circular_roi,(cX,cY),radius,255,10) 101 | 102 | circular_roi = cv2.bitwise_and(thresholded,thresholded,mask=circular_roi) 103 | 104 | #grab contours in ROI 105 | 106 | contours,hierarchy = cv2.findContours(circular_roi.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 107 | 108 | count = 0 #no figners are extended 109 | 110 | for cnt in contours: 111 | 112 | (x,y,w,h) = cv2.boundingRect(cnt) 113 | #to check contour is not at very bottom region 114 | out_of_wrist = (cY+(cY*0.25)) > (y+h) 115 | 116 | #doesn't exceed 25 of points outside 117 | 118 | limit_points = ((circumfrence*0.25) > cnt.shape[0]) 119 | 120 | if out_of_wrist and limit_points: 121 | count+=1 122 | 123 | return count 124 | 125 | ################################################################################################################# 126 | ################################################################################################################ 127 | 128 | cam = cv2.VideoCapture(0) 129 | 130 | num_frames = 0 131 | #first 60 frames for background avg 132 | 133 | while True: 134 | ret,frame = cam.read() 135 | frame_copy = frame.copy() 136 | 137 | roi =frame[roi_top:roi_bottom,roi_right:roi_left] 138 | #gray scale 139 | gray = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY) 140 | # apply bit of blur to gray scale image 141 | gray = cv2.GaussianBlur(gray,(7,7),0) 142 | 143 | #accumulated weights 144 | if num_frames <60: 145 | calc_accum_avg(gray,accumulated_weight) 146 | 147 | if num_frames <=59: 148 | cv2.putText(frame_copy,'WAIT, GETTING BACKGROUND',(200,200),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,1,(0,0,255),2) 149 | cv2.imshow('Finger Count',frame_copy) 150 | else: 151 | 152 | hand = segment(gray) 153 | 154 | if hand is not None: 155 | thresholded,hand_segment = hand 156 | 157 | #DRAW contours around real hand in live stream 158 | cv2.drawContours(frame_copy,[hand_segment+(roi_right,roi_top)],-1,(255,0,0),5) 159 | 160 | fingers = count_fingers(thresholded,hand_segment) 161 | 162 | cv2.putText(frame_copy,str(fingers),(70,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2) 163 | 164 | cv2.imshow('Thresholded', thresholded) 165 | 166 | cv2.rectangle(frame_copy,(roi_left,roi_top),(roi_right,roi_bottom),(0,0,255),5) 167 | 168 | num_frames +=1 169 | 170 | cv2.imshow('Finger Count', frame_copy) 171 | 172 | k = cv2.waitKey(1) & 0xFF 173 | if k == 27: 174 | break 175 | 176 | cam.release() 177 | cv2.destroyAllWindows() 178 | -------------------------------------------------------------------------------- /8. Captsone/Readme.md: -------------------------------------------------------------------------------- 1 | ### Capstone Project 2 | 3 | In this project, we will create a program that can detect a hand, 4 | segment the hand and count the number of fingers being held up 5 | 6 | Strategy for counting the fingers: 7 | * Grab ROI 8 | * Calculate running average background for 60 frames of video 9 | * Once average value is found, then the hand can enter ROI an apply thresholding etchniques to apply isolation 10 | * Once the hand enters ROI we will use COnvex Hull to draw polygon around hand 11 | * Calculate center of hand and calculate angle of center with the outer fingers points 12 | * Also using some angles we can calculate some distances between fingers. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning 2 | 3 | In this repository, we will work on Computer Vision related topics with OpenCV and Deep learning. Applications include ***Image Processing, Object Tracking, Convolutional Neural Networks, Yolo v3 with Python*** 4 | 5 | Goals of this course: 6 | - [ ] Understand Computer Vision Applications 7 | - [ ] Understand how to use OpenCV and Python work with iamges and videos 8 | - [ ] Apply these skills to projects 9 | 10 | Course Outline 11 | - [ ] NumPy and Image Basics ( Indexing & Slicing, read and display images, basic compands and drawings ) 12 | - [ ] Image Processing with OpenCV ( Advanced OpenCV operations ) 13 | - [ ] Video Processing with OpenCV ( Basic working of video files, streaming webcam with OpenCV library) 14 | - [ ] Object Detection ( template matching to face detection) 15 | - [ ] Object Tracking ( tracking objects in videos) 16 | - [ ] Deep Learning with Computer Vision ( Neural networks, Image Classification ) 17 | 18 | **We will be skip NumPy basics as we will focus more on image basics with Numpy arrays and move from there** 19 | -------------------------------------------------------------------------------- /images/FaceDetection1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/images/FaceDetection1.gif -------------------------------------------------------------------------------- /images/FaceDetection2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/images/FaceDetection2.gif -------------------------------------------------------------------------------- /images/FaceDetection3.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/images/FaceDetection3.gif -------------------------------------------------------------------------------- /images/Readme.md: -------------------------------------------------------------------------------- 1 | ##### images 2 | -------------------------------------------------------------------------------- /images/Watershed1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/worklifesg/Python-for-Computer-Vision-with-OpenCV-and-Deep-Learning/6f726c128b439e62471e154b7c62dfd6f4381f0b/images/Watershed1.gif --------------------------------------------------------------------------------