Write the code for live video stream using cv2. Detect the human face(s) from that live video stream. 👉🏻 Identify and get it's/their's position. And blur the part of a detected face and keep the rest part as it is.
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
def detect_and_blur_faces(frame):
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
face_roi = frame[y:y+h, x:x+w]
blurred_face = cv2.GaussianBlur(face_roi, (23, 23), 30)
frame[y:y+h, x:x+w] = blurred_face
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
num_faces = len(faces)
cv2.putText(frame, f"Faces detected: {num_faces}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
return frame
Comments
Post a Comment