Live Video Sketching through Webcam using Computer Vision | by Kaushik Jadhav | Towards Data Science

Computer Vision refers to a field of Computer Science that focuses on enabling computers to see, identify and process images in the same way as the human mind does. It is a branch of Artificial Intelligence that allows computer to extract useful features from a set of images, perform the required operations on them and generate the output.
This article aims to implement a Computer Vision model that generates a Live Video Sketch of the real time footage of a Webcam. The complete source code for the project is available on my Github repo.
Install all the required packages for the project using pip by running the commands given below on Command Prompt:
pip install opencv-pythonpip install keraspip install numpypip install matplotlib
We will break the implementation process into two parts.
The first part consists of defining a function that takes a single frame of the video as input and generates a sketched image frame as output. The Python code for doing this is as follows:
import kerasimport cv2import numpy as npimport matplotlibimport cv2import numpy as np# Our sketch generating functiondef sketch(image):    # Convert image to grayscale    img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)        # Clean up image using Guassian Blur    img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)        # Extract edges    canny_edges = cv2.Canny(img_gray_blur, 30, 60)        # Do an invert binarize the image     ret, mask = cv2.threshold(canny_edges, 240, 255, cv2.THRESH_BINARY_INV)        return mask
First, we convert the colored image frames of the video to gray scale. Then, we clean up the image using the GaussianBlur() function of OpenCV. Finally, to generate a sketch of the frame, we extract the canny edges and do an invert binarize operation on the edges.
The second part consists of applying the above sketch function recursively to all the frames of the Webcam video. The Python code to do this is as follows:
cap = cv2.VideoCapture(0)cap2 = cv2.VideoCapture(1)while True:    ret, frame = cap.read()    ret1, frame1 = cap2.read()    cv2.imshow('Original', (frame))    cv2.imshow('Our Live Sketcher', sketch(frame))    if cv2.waitKey(1) == 13: #13 is the Enter Key        break        # Release camera and close windowscap.release()cap2.release()cv2.destroyAllWindows()
As shown in the above code snippet, initialize two VideoCapture objects: one for displaying Webcam footage and the other for displaying the live video sketch. Create a loop which breaks when someone presses the Enter key. Inside the loop, fetch the frames of both the VideoCapture objects. Pass the original frame to the sketch function and display both the original and sketched frames till the loop breaks.
The above figure shows the final result of the model. We can see that the model generates a Live Video Sketch of the Webcam video successfully.
Note that video performance differs depending upon your Webcam. To improve performance, you might want to play around with the canny_edges 30,60 and the binarize 240 values in the code. Change them such that it suits your webcam.
Furthermore, I encourage the readers of this article to experiment with the code on their own to increase the precision of the model.
Feel free to share this article with others if you found it useful. Thank you so much for reading.
Feel free to connect with me on other platforms:
Github — https://github.com/kaushikjadhav01
LinkedIn — https://www.linkedin.com/in/kaushikjadhav01/
The source code for the entire project is available on my Github repo. Feel free to use it for educational purposes.