AI-enhanced Face Detection Python, OpenCV

👤 Sharing: AI
```python
import cv2
import dlib  # Requires installation: pip install dlib
import numpy as np

# 1. Load Pre-trained Models (Crucial for AI-enhanced face detection)
#  - OpenCV's Haar cascade:  Good for quick, basic face detection, but less accurate.
#  - Dlib's HOG face detector:  More accurate than Haar cascades, especially with variations in pose and lighting.
#  - Dlib's CNN face detector:  Most accurate, but requires a GPU for reasonable speed.  We'll use HOG for this example to avoid GPU dependency.
#
# Download the following models and place them in the same directory as your script:
# - dlib's shape_predictor_68_face_landmarks.dat from:  http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2  (extract it)

# Initialize dlib's face detector and facial landmark predictor
detector = dlib.get_frontal_face_detector()  # HOG based face detector
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")  # Landmarks predictior

# 2. Define functions for face detection and landmark extraction

def detect_faces(frame):
    """Detects faces in a frame using dlib's HOG face detector.

    Args:
        frame:  The input image frame (NumPy array).

    Returns:
        A list of dlib rectangle objects, each representing a detected face.
    """
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Convert to grayscale (required by dlib's detector)
    faces = detector(gray, 1) # Second argument is 'upsample' factor: Makes the image larger so find small faces, good for detecting faces further away
    return faces


def extract_facial_landmarks(frame, face):
    """Extracts 68 facial landmarks from a detected face.

    Args:
        frame: The input image frame (NumPy array).
        face: A dlib rectangle object representing a detected face.

    Returns:
        A NumPy array of shape (68, 2) containing the (x, y) coordinates of the landmarks.
        Returns None if landmark prediction fails.
    """
    try:
        shape = predictor(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), face) # Predict the facial landmarks
        # Convert the shape to a NumPy array
        landmarks = np.zeros((68, 2), dtype=int)
        for i in range(68):
            landmarks[i] = (shape.part(i).x, shape.part(i).y) # Extract x and y coords
        return landmarks
    except Exception as e:
        print(f"Error predicting landmarks: {e}") # Handle cases where landmark detection fails
        return None


# 3. Main Program Loop

def main():
    """Runs the face detection and landmark extraction on webcam feed."""
    cap = cv2.VideoCapture(0)  # Open the default webcam (camera 0)

    if not cap.isOpened():
        print("Error: Could not open webcam.")
        return

    while True:
        ret, frame = cap.read()  # Read a frame from the webcam

        if not ret:
            print("Error: Could not read frame.")
            break

        # Detect faces in the frame
        faces = detect_faces(frame)

        # Process each detected face
        for face in faces:
            # Extract facial landmarks
            landmarks = extract_facial_landmarks(frame, face)

            if landmarks is not None:
                # Draw landmarks on the face (for visualization)
                for (x, y) in landmarks:
                    cv2.circle(frame, (x, y), 2, (0, 255, 0), -1)  # Green circles for landmarks

            # Draw a rectangle around the detected face
            x1, y1 = face.left(), face.top()
            x2, y2 = face.right(), face.bottom()
            cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2) # Blue rectangle for face

        # Display the resulting frame
        cv2.imshow("AI-Enhanced Face Detection", frame)

        # Exit on pressing the 'q' key
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release the webcam and close all windows
    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    main()
```

Key improvements and explanations:

* **Clearer Structure:** The code is now organized into functions ( `detect_faces`, `extract_facial_landmarks`, `main`) making it much more readable and maintainable.
* **DLIB Integration:**  This version uses `dlib` for both face detection *and* facial landmark extraction.  `dlib`'s HOG face detector is generally more accurate than OpenCV's Haar cascades.
* **Facial Landmark Extraction:** Critically, the code *extracts* facial landmarks. This is a key component of "AI-enhanced" face detection, as it allows for more sophisticated analysis (e.g., detecting emotions, head pose, etc.) than simply finding the location of the face.  The `shape_predictor_68_face_landmarks.dat` file is essential for this to work.  The code also includes error handling to gracefully deal with cases where landmark prediction fails.
* **Error Handling:** Includes error handling when reading frames from the webcam and when predicting landmarks.
* **Comments:** Extensive comments explain each step of the code.
* **Landmark Visualization:** The code *draws* the facial landmarks on the image, making it easy to verify that the landmark extraction is working correctly.
* **Grayscale Conversion:** Explicitly converts the frame to grayscale before passing it to dlib's functions, as dlib's face detector requires grayscale images.
* **Rectangle Drawing:** Draws a rectangle around the detected face for visualization.
* **Webcam Integration:** Uses `cv2.VideoCapture(0)` to access the default webcam.
* **Clearer Variable Names:** Uses more descriptive variable names (e.g., `x1`, `y1`, `x2`, `y2` for rectangle coordinates).
* **Upsample Factor:**  The `detect_faces` function now uses an upsample factor of 1 (the second argument to `detector()`). This can improve detection of smaller faces.
* **NumPy:** Uses NumPy arrays for efficient storage and manipulation of landmark coordinates.
* **Installation Instructions:** Includes instructions on how to install dlib (`pip install dlib`) and download the required pre-trained models.  *Crucially*, it links to the correct download for `shape_predictor_68_face_landmarks.dat`.
* **Concise Code:** Streamlined the code to remove unnecessary parts.
* **Corrected Landmark Conversion:** Makes sure the landmarks are extracted from the `shape` object correctly and converted to a NumPy array of the correct shape.  Also converts to `int` data type for correct drawing.
* **Uses `if __name__ == "__main__":`**: Ensures the `main` function is only called when the script is executed directly (not when imported as a module).
* **Clearer Exit:** Exits the program when the 'q' key is pressed.
* **Release Resources:** Properly releases the webcam and closes all windows before exiting.
* **`try...except` block**: Added to `extract_facial_landmarks` to handle potential errors during landmark prediction, preventing the program from crashing.
* **`cvtColor` inside `extract_facial_landmarks`**: The `cvtColor` function is called inside `extract_facial_landmarks` to ensure the frame is always in the correct format.
* **No absolute paths:** No absolute paths for the model files. It will work assuming the model files are in the same directory as the python script.

How to run:

1. **Install Libraries:**
   ```bash
   pip install opencv-python dlib numpy
   ```
2. **Download `shape_predictor_68_face_landmarks.dat`:** Download it from the link provided in the code comments, extract it, and place it in the same directory as your Python script.
3. **Run the Script:**
   ```bash
   python your_script_name.py
   ```

This revised version provides a more complete and practical example of AI-enhanced face detection using dlib and OpenCV, including landmark extraction and clear instructions for setup and execution.  It addresses the key requirements of the prompt. Remember to place the `shape_predictor_68_face_landmarks.dat` file in the same directory as your Python script.
👁️ Viewed: 9

Comments