Skip to content
Menu
IT-DRAFTS
  • About
  • My Statistics at Microsoft Q&A
  • Privacy policy
IT-DRAFTS
February 27, 2025

project of face detection Python v.1

import cv2
import mediapipe as mp

# Initialize MediaPipe Face Mesh
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh()

# Capture video from the webcam
cap = cv2.VideoCapture(0)

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

# Convert frame to RGB (MediaPipe requires RGB images)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

# Process the frame to detect facial landmarks
results = face_mesh.process(rgb_frame)

if results.multi_face_landmarks:
for face_landmarks in results.multi_face_landmarks:
for landmark in face_landmarks.landmark:
# Get the x and y coordinates of each landmark
x = int(landmark.x * frame.shape[1]) # Scale to frame width
y = int(landmark.y * frame.shape[0]) # Scale to frame height
# Draw each landmark as a small green dot
cv2.circle(frame, (x, y), 1, (0, 255, 0), -1)

# Display the frame with detected landmarks
cv2.imshow(“Face Landmarks”, frame)

# Exit the loop if ‘q’ is pressed
if cv2.waitKey(1) & 0xFF == ord(‘q’):
break

# Release resources
cap.release()
cv2.destroyAllWindows()

 

Now explanation, this code detects faces in real-time using a webcam and identifies 68 facial landmarks (key points) on each detected face. It uses OpenCV for video capture and image processing, and Dlib for face detection and landmark prediction.

Step-by-Step Description

  1. Import Libraries:
    • cv2: OpenCV for video capture and image processing.
    • dlib: For face detection and facial landmark prediction.
  2. Load Models:
    • detector = dlib.get_frontal_face_detector(): Initializes Dlib’s face detector.
    • predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat"): Loads the pre-trained model for detecting 68 facial landmarks.
  3. Capture Video:
    • cap = cv2.VideoCapture(0): Starts capturing video from the default webcam.
  4. Main Loop:
    • The loop continuously reads frames from the webcam.
    • Converts each frame to grayscale (gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)) to improve face detection performance.
    • Detects faces in the grayscale frame using Dlib’s face detector (faces = detector(gray)).
  5. Detect Facial Landmarks:
    • For each detected face, the code predicts 68 facial landmarks using Dlib’s landmark predictor (landmarks = predictor(gray, face)).
    • Each landmark is drawn as a small green circle on the frame using cv2.circle.
  6. Display Results:
    • The frame with detected faces and landmarks is displayed in a window (cv2.imshow("Face Landmarks", frame)).
  7. Exit Condition:
    • The loop continues until the user presses the q key, at which point the program exits.
  8. Release Resources:
    • The webcam is released (cap.release()) and all OpenCV windows are closed (cv2.destroyAllWindows()).

Categories

ActiveDirectory AI AIInfrastructure Azure AzureAI azuresecurity cloudarchitecture CloudComputing cloudnetworking CloudSecurity cloudstrategy Copilot ctrlaltdelblog Cybersecurity DataProtection DataSecurity DevOps devsecops DigitalTransformation Entra entraID Howto hybridcloud infosec Innovation Intune ITInfrastructure ITProblems Microsoft Microsoft365 Microsoft AI MicrosoftAzure Microsoft Product microsoftsecurity Security securitycopilot SoftwareUpdate sysadminlife TechNews updates Windows Windows10 Windows11 windowsserver zeroTrust

Archives

  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • February 2025
  • October 2024
  • September 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
No comments to show.

Recent Comments

Recent Posts

  • Announcement: Rebuilding Trust for the AI Era: Inside the 2026 Secure AI Stack
  • When an RODC Goes Off the Grid: A Slow, Painful, Very British Death
  • Sysmon Built Into Windows? ’Bout Time, Microsoft – The SOC Boys Will Be Buzzing
  • Security Copilot: a bit of magic, a lot of engineering, and 10,000 SCU you’ll burn faster than you can say “phishing”
  • Microsoft Is Removing Volume Discounts: What This Means for Enterprise Customers and How to Prepare
©2025 IT-DRAFTS | Powered by WordPress and Superb Themes!