山内セミナー(2019/05/29)

関連サイトと資料

サンプル画像


test_face_detection.jpg


test_cat_face_detection.jpg

サンプルプログラム(1)

face_detection_opencv_haar.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(2, 2, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')        
	

def show_detection(image, faces):
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 5)
    return image
	

img = cv2.imread("test_face_detection.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	
cas_alt2 = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
cas_default = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
	
faces_alt2 = cas_alt2.detectMultiScale(gray)
faces_default = cas_default.detectMultiScale(gray)
	
retval, faces_haar_alt2 = cv2.face.getFacesHAAR(img, "haarcascade_frontalface_alt2.xml")
faces_haar_alt2 = np.squeeze(faces_haar_alt2)
retval, faces_haar_default = cv2.face.getFacesHAAR(img, "haarcascade_frontalface_default.xml")
faces_haar_default = np.squeeze(faces_haar_default)
	
img_faces_alt2 = show_detection(img.copy(), faces_alt2)
img_faces_default = show_detection(img.copy(), faces_default)
img_faces_haar_alt2 = show_detection(img.copy(), faces_haar_alt2)
img_faces_haar_default = show_detection(img.copy(), faces_haar_default)
	
fig = plt.figure(figsize=(10, 8))
plt.suptitle("Face detection using haar feature-based cascade classifiers", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
show_img_with_matplotlib(img_faces_alt2, "detectMultiScale(frontalface_alt2): " + str(len(faces_alt2)), 1)
show_img_with_matplotlib(img_faces_default, "detectMultiScale(frontalface_default): " + str(len(faces_default)), 2)
show_img_with_matplotlib(img_faces_haar_alt2, "getFacesHAAR(frontalface_alt2): " + str(len(faces_haar_alt2)), 3)
show_img_with_matplotlib(img_faces_haar_default, "getFacesHAAR(frontalface_default): " + str(len(faces_haar_default)), 4)
	
plt.show()

サンプルプログラム(2)

cat_face_detection_opencv_haar.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(2, 2, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')        
	

def show_detection(image, faces):
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 5)
    return image
	

img = cv2.imread("test_cat_face_detection.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	
cas_catface = cv2.CascadeClassifier("haarcascade_frontalcatface.xml")
cas_catface_extended = cv2.CascadeClassifier("haarcascade_frontalcatface_extended.xml")
	
faces_cas_catface = cas_catface.detectMultiScale(gray)
faces_cas_catface_extended = cas_catface_extended.detectMultiScale(gray)
	
retval, faces_haar_cat = cv2.face.getFacesHAAR(img, "haarcascade_frontalcatface.xml")
faces_haar_cat = np.squeeze(faces_haar_cat)
retval, faces_haar_cat_extended = cv2.face.getFacesHAAR(img, "haarcascade_frontalcatface_extended.xml")
faces_haar_cat_extended = np.squeeze(faces_haar_cat_extended)
	
img_cas_catface = show_detection(img.copy(), faces_cas_catface)
img_cas_catface_extended = show_detection(img.copy(), faces_cas_catface_extended)
img_faces_haar_cat = show_detection(img.copy(), faces_haar_cat)
img_faces_haar_cat_extended = show_detection(img.copy(), faces_haar_cat_extended)
	
fig = plt.figure(figsize=(10, 8))
plt.suptitle("Cat face detection using haar feature-based cascade classifiers", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
	
show_img_with_matplotlib(img_cas_catface, "detectMultiScale(frontalcatface): " + str(len(faces_cas_catface)), 1)
show_img_with_matplotlib(img_cas_catface_extended,
                         "detectMultiScale(frontalcatface_extended): " + str(len(faces_cas_catface_extended)), 2)
show_img_with_matplotlib(img_faces_haar_cat, "getFacesHAAR(frontalcatface): " + str(len(faces_haar_cat)), 3)
show_img_with_matplotlib(img_faces_haar_cat_extended,
                         "getFacesHAAR(frontalcatface_extended): " + str(len(faces_haar_cat_extended)), 4)
	
plt.show()

サンプルプログラム(3)

face_detection_opencv_dnn_caffe.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(1, 1, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	

net = cv2.dnn.readNetFromCaffe("deploy.prototxt", "res10_300x300_ssd_iter_140000_fp16.caffemodel")
	
image = cv2.imread("test_face_detection.jpg")
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), [104., 117., 123.], False, False)
	
net.setInput(blob)
detections = net.forward()
	
detected_faces = 0
for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]
	
    if confidence > 0.7:
        detected_faces += 1
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
	
        # Draw the detection and the confidence:
        text = "{0:.3f}%".format(confidence * 100)
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(image, (startX, startY), (endX, endY), (255, 0, 0), 3)
        cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
	
fig = plt.figure(figsize=(10, 5))
plt.suptitle("Face detection using OpenCV DNN face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(image, "DNN face detector: " + str(detected_faces), 1)
	
plt.show()

サンプルプログラム(4)

face_detection_opencv_dnn_tf.py
import cv2
import numpy as np
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(1, 1, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	

net = cv2.dnn.readNetFromTensorflow("opencv_face_detector_uint8.pb", "opencv_face_detector.pbtxt")
	
image = cv2.imread("test_face_detection.jpg")
(h, w) = image.shape[:2]
blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300), [104., 117., 123.], False, False)
	
net.setInput(blob)
detections = net.forward()
	
detected_faces = 0
for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]
	
    if confidence > 0.7:
        detected_faces += 1
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
	
        # Draw the detection and the confidence:
        text = "{0:.3f}%".format(confidence * 100)
        y = startY - 10 if startY - 10 > 10 else startY + 10
        cv2.rectangle(image, (startX, startY), (endX, endY), (255, 0, 0), 3)
        cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
	
fig = plt.figure(figsize=(10, 5))
plt.suptitle("Face detection using OpenCV DNN face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(image, "DNN face detector: " + str(detected_faces), 1)
	
plt.show()

サンプルプログラム(5)

face_detection_dlib_hog.py
import cv2
import dlib
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(1, 2, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	

def show_detection(image, faces):
    for face in faces:
        cv2.rectangle(image, (face.left(), face.top()), (face.right(), face.bottom()), (255, 0, 0), 10)
    return image
	

img = cv2.imread("test_face_detection.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	
detector = dlib.get_frontal_face_detector()
rects_1 = detector(gray, 0)
rects_2 = detector(gray, 1)
	
img_faces_1 = show_detection(img.copy(), rects_1)
img_faces_2 = show_detection(img.copy(), rects_2)
	
fig = plt.figure(figsize=(10, 4))
plt.suptitle("Face detection using dlib frontal face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(img_faces_1, "detector(gray, 0): " + str(len(rects_1)), 1)
show_img_with_matplotlib(img_faces_2, "detector(gray, 1): " + str(len(rects_2)), 2)
	
plt.show()

サンプルプログラム(6)

face_detection_dlib_cnn.py
import cv2
import dlib
from matplotlib import pyplot as plt
	

def show_img_with_matplotlib(color_img, title, pos):
    img_RGB = color_img[:, :, ::-1]
    ax = plt.subplot(1, 1, pos)
    plt.imshow(img_RGB)
    plt.title(title)
    plt.axis('off')
	

def show_detection(image, faces):
    for face in faces:
        cv2.rectangle(image, (face.rect.left(), face.rect.top()), (face.rect.right(), face.rect.bottom()), (255, 0, 0), 10)
    return image
	

img = cv2.imread("test_face_detection.jpg")
cnn_face_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")
rects = cnn_face_detector(img, 0)
img_faces = show_detection(img.copy(), rects)
	
fig = plt.figure(figsize=(10, 5))
plt.suptitle("Face detection using dlib CNN face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(img_faces, "cnn_face_detector(img, 0): " + str(len(rects)), 1)
	
plt.show()