Skip to content

Latest commit

 

History

History
94 lines (80 loc) · 5.93 KB

File metadata and controls

94 lines (80 loc) · 5.93 KB

Sketch App using Webcam

Software used

  • Python

Technical Stack

  • Python libraries : NumPy and OpenCV

    Numpy

    NumPy stands for "Numerical python". Numpy is a general-purpose array-processing package. It is used for working with arrays and also in the fields like linear algebra,matrices,etc. Numpy provides a high-performance multidimensional array and basic tools to compute with and manipulate these arrays. Numpy arrays are great alternative to python lists.Numpy arrays are fast,effecient and easy to use.

    OpenCV

    OpenCv stands for "Open Source Computer Vision Library". It is an open source computer vision and machine learning software library. It is widely used in building sterling computer vision projects. All the OpenCV array structures are converted to and from Numpy arrays. Using OpenCV library, you can read & write images,Capture & save videos,Process images (filter, transform) Perform face detection. It is used in the fields of Automobile,Security,Transportation,Robotics,etc.

    Steps to build a Sketch application using webcam

    • Step 1:
    • Install NumPy and OpenCV libraries in the python environment using the command prompt.
      Enter the following commands:
      pip install numpy
      pip install OpenCV

    • Step 2:
    • After installing the python libraries we are all set to jump into the code for buliding a basic sketch app

      .
      Python code for Sketch app
      import cv2
      import numpy as np

      def sketch(image):
        

      # convert the captured image into gray scale image


         grayimg=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        

      # blurring the image

         blurimg=cv2.GaussianBlur(grayimg,(3,3),0)
        

      # extracting edges

         edges=cv2.Canny(blurimg,10,80)
        

      #applying threshold

         ret,mimg=cv2.threshold(edges,50,255,cv2.THRESH_BINARY_INV)
         return mimg

      # Capturing video from webcam

      vid_capt=cv2.VideoCapture(0)

      # Capturing the video frame by frame

      while True:
         ret,pic_capt=vid_capt.read()
         cv2.imshow('Your Sketch',getmysketch(pic_capt))
         # Key13 is ENTER_KEY
         if cv2.waitKey(1)==13:
         break

      # releasing_webcam

      vid_capt.release()

      # destroying_window

      cv2.destroyAllWindows()

      Step by step explanation

      vid_capt=cv2.VideoCapture(0)
      while True:
         ret,pic_capt=vid_capt.read()
         cv2.imshow('Your Sketch',getmysketch(pic_capt))
         if cv2.waitKey(1)==13:
         break
      • vid_capt=cv2.VideoCapture(0): It performs the function to open the webcam and to capture the video.
      • ret,pic_capt=cap.read(): It captures the video frame by frame.
      • cv2.imshow('Your Sketch',getmysketch(pic_capt)): It pops up a dialog box with the sketch of the frame captured with the title 'Your Sketch'. To display the sketch it calls the function as shown 'getmysketch(frame)'
      • if cv2.waitKey(1)==13:
           break:   It breaks the ongoing activity and closes the dialog box after pressing the enter button.

      def sketch(image):
         grayimg=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
         blurimg=cv2.GaussianBlur(grayimg,(3,3),0)
         edges=cv2.Canny(blurimg,10,80)
         ret,mimg=cv2.threshold(edges,50,255,cv2.THRESH_BINARY_INV)
         return mimg

      • def sketch(image): Function declared with image as a parameter to output the sketch of the captured image.
      • grayimg=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY): It converts the captured image to gray scale image.We convert the captured image into gray scale image to reduce the complexity or compress i.e An RGB Image consists of 3 layers R,G,B. It’s a 3 dimensional matrix, whereas gray scale image is a 2 dimensional matrix.
      • blurimg=cv2.GaussianBlur(grayimg,(3,3),0): It blurs the gray scale image obtained through the above operation. It is useful for removing noise. It actually removes high frequency content (eg: noise, edges) from the image.We simply reduce the edge content and makes the transition form one color to the other very smooth
      • edges=cv2.Canny(blurimg,10,80): It is executed to extract the edges from the blurred grayscale image.Edge detection is an image processing technique for finding the boundaries of objects within images.It is used to detect a person,license plate or any other things.It works by detecting discontinuities in brightness. It is basically used for image segmentation and data extraction.
      • ret,mimg=cv2.threshold(edges,50,255,cv2.THRESH_BINARY_INV):Gray scale image is converted into Binary Image, it uses a threshold. Thresholding is a process of converting image to binary form.In OpenCV thresholding is done on grayscale images, which have pixel values ranging from 0–255. When you threshold an image you classify these pixels into groups setting a upper and lower bound to each group. Suppose gray scale values are from 0 (Pure Black) to 255(Pure White) , values greater than threshold will be converted into 1 (White) and below to threshold will be converted into 0 (Black).
      • Image captured through webcam initially


        Gray scale image


        Blurred image


        Edges


        Threshold image


        Output