<p>另一个可用的选项是<a href="http://dlib.net/python/" rel="nofollow noreferrer">dlib</a>,它基于机器学习方法。</p>
<pre><code>import dlib
import Image
from skimage import io
import matplotlib.pyplot as plt
def detect_faces(image):
# Create a face detector
face_detector = dlib.get_frontal_face_detector()
# Run detector and get bounding boxes of the faces on image.
detected_faces = face_detector(image, 1)
face_frames = [(x.left(), x.top(),
x.right(), x.bottom()) for x in detected_faces]
return face_frames
# Load image
img_path = 'test.jpg'
image = io.imread(img_path)
# Detect faces
detected_faces = detect_faces(image)
# Crop faces and plot
for n, face_rect in enumerate(detected_faces):
face = Image.fromarray(image).crop(face_rect)
plt.subplot(1, len(detected_faces), n+1)
plt.axis('off')
plt.imshow(face)
</code></pre>
<p><a href="https://i.stack.imgur.com/V6562.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V6562.jpg" alt="enter image description here"/></a>
<a href="https://i.stack.imgur.com/xHZtu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xHZtu.png" alt="enter image description here"/></a></p>