Skip to content

Instantly share code, notes, and snippets.

@pollardld
Last active June 24, 2024 00:58
Show Gist options
  • Save pollardld/d0e940bac3a60245498df212c10c2f24 to your computer and use it in GitHub Desktop.
Save pollardld/d0e940bac3a60245498df212c10c2f24 to your computer and use it in GitHub Desktop.
Code samples from ChatGPT
import numpy as np
from PIL import Image
import cv2
import dlib
# Load the image
image_path = "/mnt/data/me.jpeg"
image = Image.open(image_path)
# Convert the image to OpenCV format
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
# Load the pre-trained face detector and shape predictor from dlib
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("/mnt/data/shape_predictor_68_face_landmarks.dat")
# Convert the image to grayscale for processing
gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = detector(gray)
# Function to draw the detected landmarks on the face
def draw_landmarks(image, landmarks):
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(image, (x, y), 1, (255, 0, 0), -1)
# Process each detected face
for face in faces:
landmarks = predictor(gray, face)
draw_landmarks(image_cv, landmarks)
# Save and display the image with landmarks
landmarked_image_path = "/mnt/data/me_with_landmarks.jpg"
cv2.imwrite(landmarked_image_path, image_cv)
landmarked_image_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment