Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Live Sketch Using Webcam with Python OpenCV [Easy Guide]

  • Isha BansalIsha Bansal
  • September 30, 2021
  • Python Programming Examples
Live Sketch Using Webcam with Python OpenCV [Easy Guide]

Today in this tutorial, you will learn an application of OpenCV which will make you realize how powerful OpenCV can get.

In the project, we will take a live webcam feed and convert it into a live sketch with the help of numpy and OpenCV library.

Let’s begin with the amazing project!


Step 1: Importing Modules

To get started, we need to import OpenCV and Numpy (assuming that you have the libraries installed). We define OpenCV and Numpy in our code as follows:

import cv2
import numpy as np

Step 2: Define a function to convert frame to Sketch

In order to covert a frame into a sketch we will follow a number of steps which are listed below:

  1. Convert Image into a gray image
  2. Apply Gaussian Blur to the gray image obtained
  3. Apply Canny Edge Detection to the Gaussian Image
  4. In the end, Invert the Image and get the Binary Inverted Image

The code for the function is shown below.

def sketch(image):
    img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)
    canny_edges = cv2.Canny(img_gray_blur, 10, 70)
    ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)
    return mask

Step 3: Open Live Webcam and apply function

We need to use the webcam and extract image frames from the video. In order to achieve the same, we will use the VideoCapture and read function to extract frames one after another.

Now use the imshow function to display the live webcam and apply the sketch function created in the previous step.

The final step is to create an exit condition for the window. Here we have kept the key the Enter Key as the exit key for the window. In the end, destroy all the windows that were opened and yet to be closed during the program.

cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('Our Live Sketcher', sketch(frame))
    if cv2.waitKey(1) == 13: #13 is the Enter Key
        break
cap.release()
cv2.destroyAllWindows()

The Complete Code

import cv2
import numpy as np

def sketch(image):
    img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)
    canny_edges = cv2.Canny(img_gray_blur, 10, 70)
    ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)
    return mask
    
cap = cv2.VideoCapture(0)
while True:
    ret, frame = cap.read()
    cv2.imshow('Our Live Sketcher', sketch(frame))
    if cv2.waitKey(1) == 13: #13 is the Enter Key
        break
cap.release()
cv2.destroyAllWindows()

The Final Output

The small video below displays the final output obtained after running the whole code mentioned in the previous section.


Conclusion

I hope you understood the concept and loved the output. Try out the simple code yourself and observe the power of the OpenCV library.

Happy Coding! 😇

Want to learn more? Check out the tutorials mentioned below:

  1. How to extract images from video using Python OpenCV?
  2. Python and OpenCV: Apply Filters to Images
  3. Animation in Python

Isha Bansal
Isha Bansal

Hey there stranger!
Do check out my blogs if you are a keen learner!

Hope you like them!

Articles: 185
Credit Card Reader in Python using OpenCV
Previous Post Credit Card Reader in Python using OpenCV
Next Post Draw Shapes Using OpenCV - A Complete How-To Guide
Draw Shapes Using OpenCV – A Complete How-To Guide

Related Posts

How to Calculate Distance Between Two Points in Python

How to Calculate Distance Between Two Points in Python

  • November 26, 2026
CIFAR 10 Dataset: Load and Use in Python

CIFAR 10 Dataset: Load and Use in Python

  • November 24, 2026
Maximum A Posteriori Estimation in Python

Maximum A Posteriori Estimation in Python

  • November 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.