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
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    if not ret:
        break
    frame = detect_and_blur_faces(frame)
    cv2.imshow('Blurred Face Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Comments

Popular posts from this blog

Revolutionizing Industries: Solving Challenges with Ansible

How to Install & run docker inside docker?

Creating a Python Script to Capture and Process Face Photos