In [1]:
import cv2
print('OpenCV Version: {}'.format(cv2.__version__))
In [2]:
# Used to display the images.
%matplotlib inline
from matplotlib import pyplot as plt
In [3]:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
In [5]:
image = cv2.imread('./input/liv-tyler.jpg')
In [13]:
scale_factor = 1.1
min_neighbors = 3
min_size = (30, 30)
flags = cv2.CASCADE_SCALE_IMAGE
faces = face_cascade.detectMultiScale(image,
scaleFactor=scale_factor,
minNeighbors=min_neighbors,
minSize=min_size,
flags=flags)
x, y, w, h = faces[0]
In [7]:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
cv2.imwrite('./output/highlight-liv-tyler-face.jpg', image)
Out[7]:
In [8]:
image = cv2.imread('./input/liv-tyler.jpg')
crop_image = image[y-50:y+h+50, x-50:x+w+50]
cv2.imwrite('./output/crop-liv-tyler-face.jpg', crop_image)
Out[8]:
In [9]:
image = cv2.imread('./input/justice-league.jpg')
In [10]:
scale_factor = 1.1
min_neighbors = 3
min_size = (30, 30)
flags = cv2.CASCADE_SCALE_IMAGE
faces = face_cascade.detectMultiScale(image,
scaleFactor=scale_factor,
minNeighbors=min_neighbors,
minSize=min_size,
flags=flags)
In [14]:
for x, y, w, h in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)
cv2.imwrite('./output/highlight-faces-justice-leauge.jpg', image)
Out[14]:
In [15]:
image = cv2.imread('./input/justice-league.jpg')
for x, y, w, h in faces:
crop_image = image[y-50:y+h+50, x-50:x+w+50]
cv2.imwrite('./output/crop-justice-leauge-face_{0}-{1}.jpg'.format(x, y), crop_image)
| Name | Face |
|---|---|
| Superman | |
| Cyborg | |
| Wonder Woman | |
| Aquaman |
In [ ]: