What is a webcam?
A webcam is a video camera that feeds or streams an image or video in real-time to or through a computer network, such as the Internet. Webcams are typically small cameras that sit on a desk, attach to a user's monitor, or are built into the hardware(such as a laptop's front camera). Webcams can be used during a video chat session involving two or more people, with conversations that include live audio and video.
In this post, we will use this webcam to convert any live video to a sketch with the help of OpenCV using python so let's drive into the code
First import the required libraries
import cv2
import numpy as np
The function for sketching
# Our sketch generating function
def 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, 10, 70)
# Do an invert binarize the image
ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)
return mask
The video loop
# Initialize webcam, cap is the object provided by VideoCapture
# It contains a boolean indicating if it was sucessful (ret)
# It also contains the images collected from the webcam (frame)
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
To release the camera:
# Release camera and close windows
cap.release()
cv2.destroyAllWindows()
Github:
You can also get the code here
Comments
Post a Comment