Live Webcam Drawing using OpenCV

Live webcam drawing is a fun and interactive way to use computer vision and graphics to create artistic projects. With the OpenCV library, you can easily access and manipulate the video feed from a webcam in real-time.

Here are some examples of how to use OpenCV for live webcam drawing:

Drawing circles on faces

One simple example is to use the OpenCV Haar cascade classifier to detect faces in the webcam feed, and then draw a circle around each face. This can be done using the following code:

import cv2 # Load the Haar cascade for face detection face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Initialize the webcam cap = cv2.VideoCapture(0) while True: # Read the frame from the webcam _, frame = cap.read() # Convert the frame to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces in the frame faces = face_cascade.detectMultiScale(gray, 1.3, 5) # Draw a circle around each face for (x,y,w,h) in faces: cv2.circle(frame, (x+w//2, y+h//2), w//2, (255,0,0), 2) # Show the frame cv2.imshow('Webcam', frame) # Break the loop if the user presses 'q' if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the webcam cap.release() # Destroy all windows cv2.destroyAllWindows()

This will open a window showing the webcam feed, with circles drawn around any faces detected in the frame.

Drawing lines based on motion

Another example is to use the OpenCV difference between frames method to detect motion in the webcam feed, and then draw a line from the previous position of the moving object to its current position.

No comments

Powered by Blogger.