Commit fb016012 authored by Arne Gerdes's avatar Arne Gerdes

Landmarks aus dlip und Haar cascade aus OpenCV

parent dd124872
import cv2 import cv2
import dlib
import numpy as np
# Create the haar cascade
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# create the landmark predictor
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
while (True): while (True):
# Read the Video
ret, img = ret, img =
# convert the video to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
# Detect faces in the video
faces = detector.detectMultiScale(
minSize=(100, 100),
# Draw a rectangle around the faces
for (x, y, w, h) in faces: for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) cv2.rectangle(gray, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Converting the OpenCV rectangle coordinates to Dlib rectangle
dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))
# use that rectangle as the bounding box to detect the face landmarks,
# and extract out the coordinates of the landmarks so OpenCV can use them
detected_landmarks = predictor(gray, dlib_rect).parts()
landmarks = np.matrix([[p.x, p.y] for p in detected_landmarks])
# enumerate through the landmark coordinates and mark them on the image
for idx, point in enumerate(landmarks):
pos = (point[0, 0], point[0, 1])
# annotate the positions
cv2.putText(gray, str(idx), pos,
color=(0, 0, 255))
# draw points on the landmark positions, pos, 3, color=(0, 255, 255))
# draw the annotated image on an OpenCV window
cv2.imshow('Window', gray)
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment