山内セミナー(2019/06/12)

関連サイトと資料

サンプル画像


test_face_detection.jpg


face_test.png

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

face_detection_fr_cnn.py
import cv2
import face_recognition
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:
        top, right, bottom, left = face
        cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 3)
    return image
	
img = cv2.imread("test_face_detection.jpg")
img = cv2.resize(img, (0, 0), fx=0.3, fy=0.3)
rgb = img[:, :, ::-1]
	
rects_1 = face_recognition.face_locations(rgb, 0, "cnn")
rects_2 = face_recognition.face_locations(rgb, 1, "cnn")
	
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 face_recognition CNN face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(img_faces_1, "face_locations(rgb, 0, cnn): " + str(len(rects_1)), 1)
show_img_with_matplotlib(img_faces_2, "face_locations(rgb, 1, cnn): " + str(len(rects_2)), 2)
	
plt.show()

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

face_detection_fr_hog.py
import cv2
import face_recognition
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:
        top, right, bottom, left = face
        cv2.rectangle(image, (left, top), (right, bottom), (255, 0, 0), 10)
    return image
	
img = cv2.imread("test_face_detection.jpg")
rgb = img[:, :, ::-1]
rects_1 = face_recognition.face_locations(rgb, 0, "hog")
rects_2 = face_recognition.face_locations(rgb, 1, "hog")
	
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 face_recognition frontal face detector", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(img_faces_1, "face_locations(rgb, 0, hog): " + str(len(rects_1)), 1)
show_img_with_matplotlib(img_faces_2, "face_locations(rgb, 1, hog): " + str(len(rects_2)), 2)
	
plt.show()

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

landmarks_detection_dlib_still.py
import cv2
import dlib
import numpy as np
	
JAWLINE_POINTS = list(range(0, 17))
RIGHT_EYEBROW_POINTS = list(range(17, 22))
LEFT_EYEBROW_POINTS = list(range(22, 27))
NOSE_BRIDGE_POINTS = list(range(27, 31))
LOWER_NOSE_POINTS = list(range(31, 36))
RIGHT_EYE_POINTS = list(range(36, 42))
LEFT_EYE_POINTS = list(range(42, 48))
MOUTH_OUTLINE_POINTS = list(range(48, 61))
MOUTH_INNER_POINTS = list(range(61, 68))
ALL_POINTS = list(range(0, 68))
	
def draw_shape_lines_all(np_shape, image):
    draw_shape_lines_range(np_shape, image, JAWLINE_POINTS)
    draw_shape_lines_range(np_shape, image, RIGHT_EYEBROW_POINTS)
    draw_shape_lines_range(np_shape, image, LEFT_EYEBROW_POINTS)
    draw_shape_lines_range(np_shape, image, NOSE_BRIDGE_POINTS)
    draw_shape_lines_range(np_shape, image, LOWER_NOSE_POINTS)
    draw_shape_lines_range(np_shape, image, RIGHT_EYE_POINTS, True)
    draw_shape_lines_range(np_shape, image, LEFT_EYE_POINTS, True)
    draw_shape_lines_range(np_shape, image, MOUTH_OUTLINE_POINTS, True)
    draw_shape_lines_range(np_shape, image, MOUTH_INNER_POINTS, True)
	
def draw_shape_lines_range(np_shape, image, range_points, is_closed=False):
    np_shape_display = np_shape[range_points]
    points = np.array(np_shape_display, dtype=np.int32)
    cv2.polylines(image, [points], is_closed, (255, 255, 0), thickness=1, lineType=cv2.LINE_8)
	
def draw_shape_points_pos_range(np_shape, image, points):
    np_shape_display = np_shape[points]
    draw_shape_points_pos(np_shape_display, image)
	
def draw_shape_points_pos(np_shape, image):
    for idx, (x, y) in enumerate(np_shape):
        cv2.putText(image, str(idx + 1), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255))
        cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
	
def draw_shape_points_range(np_shape, image, points):
    np_shape_display = np_shape[points]
    draw_shape_points(np_shape_display, image)
	
def draw_shape_points(np_shape, image):
    for (x, y) in np_shape:
        cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
	
def shape_to_np(dlib_shape, dtype="int"):
    coordinates = np.zeros((dlib_shape.num_parts, 2), dtype=dtype)
	
    for i in range(0, dlib_shape.num_parts):
        coordinates[i] = (dlib_shape.part(i).x, dlib_shape.part(i).y)
	
    return coordinates
	
#p = "shape_predictor_68_face_landmarks.dat"
p = "shape_predictor_5_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
	
test_face = cv2.imread("face_test.png")
	
frame = test_face.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
	
for (i, rect) in enumerate(rects):
    cv2.rectangle(frame, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 0), 1)
    shape = predictor(gray, rect)
	
    shape = shape_to_np(shape)
    #draw_shape_lines_all(shape, frame)
    #draw_shape_lines_range(shape, frame, JAWLINE_POINTS)
    #draw_shape_points_pos_range(shape, frame, LEFT_EYE_POINTS + RIGHT_EYE_POINTS + NOSE_BRIDGE_POINTS)
    draw_shape_points(shape, frame)
	
cv2.imshow("Landmarks detection using dlib", frame)
	
cv2.waitKey(0)
cv2.destroyAllWindows()

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

camera1.py
import cv2
    
capture = cv2.VideoCapture(0)
    
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
    
print('横幅: {0}'.format(frame_width))
print('高さ: {0}'.format(frame_height))
print('fps: {0}'.format(fps))
    
if capture.isOpened() is False:
    print('カメラの起動に失敗しました!')
	
while capture.isOpened():
    ret, frame = capture.read()
    
    if ret is True:
        cv2.imshow('Image from Camera', frame)
        
        if cv2.waitKey(20) & 0xFF == ord('q'):
            break
    else:
        break
	
capture.release()
cv2.destroyAllWindows()

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

landmarks_detection_dlib_camera.py
import cv2
import dlib
import numpy as np
	
JAWLINE_POINTS = list(range(0, 17))
RIGHT_EYEBROW_POINTS = list(range(17, 22))
LEFT_EYEBROW_POINTS = list(range(22, 27))
NOSE_BRIDGE_POINTS = list(range(27, 31))
LOWER_NOSE_POINTS = list(range(31, 36))
RIGHT_EYE_POINTS = list(range(36, 42))
LEFT_EYE_POINTS = list(range(42, 48))
MOUTH_OUTLINE_POINTS = list(range(48, 61))
MOUTH_INNER_POINTS = list(range(61, 68))
ALL_POINTS = list(range(0, 68))
	
def draw_shape_lines_all(np_shape, image):
        draw_shape_lines_range(np_shape, image, JAWLINE_POINTS)
        draw_shape_lines_range(np_shape, image, RIGHT_EYEBROW_POINTS)
        draw_shape_lines_range(np_shape, image, LEFT_EYEBROW_POINTS)
        draw_shape_lines_range(np_shape, image, NOSE_BRIDGE_POINTS)
        draw_shape_lines_range(np_shape, image, LOWER_NOSE_POINTS)
        draw_shape_lines_range(np_shape, image, RIGHT_EYE_POINTS, True)
        draw_shape_lines_range(np_shape, image, LEFT_EYE_POINTS, True)
        draw_shape_lines_range(np_shape, image, MOUTH_OUTLINE_POINTS, True)
        draw_shape_lines_range(np_shape, image, MOUTH_INNER_POINTS, True)
	
def draw_shape_lines_range(np_shape, image, range_points, is_closed=False):
        np_shape_display = np_shape[range_points]
        points = np.array(np_shape_display, dtype=np.int32)
        cv2.polylines(image, [points], is_closed, (255, 255, 0), thickness=1, lineType=cv2.LINE_8)
	
def draw_shape_points_pos_range(np_shape, image, points):
        np_shape_display = np_shape[points]
        draw_shape_points_pos(np_shape_display, image)
	
def draw_shape_points_pos(np_shape, image):
        for idx, (x, y) in enumerate(np_shape):
                cv2.putText(image, str(idx + 1), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 0, 255))
                cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
	
def draw_shape_points_range(np_shape, image, points):
        np_shape_display = np_shape[points]
        draw_shape_points(np_shape_display, image)
	
def draw_shape_points(np_shape, image):
        for (x, y) in np_shape:
                cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
	
def shape_to_np(dlib_shape, dtype="int"):
        coordinates = np.zeros((dlib_shape.num_parts, 2), dtype=dtype)
        for i in range(0, dlib_shape.num_parts):
                coordinates[i] = (dlib_shape.part(i).x, dlib_shape.part(i).y)
        return coordinates
	
#p = "shape_predictor_68_face_landmarks.dat"
p = "shape_predictor_5_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)
	
video_capture = cv2.VideoCapture(0)
	
while True:
        ret, frame = video_capture.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rects = detector(gray, 0)
	
        for (i, rect) in enumerate(rects):
                cv2.rectangle(frame, (rect.left(), rect.top()), (rect.right(), rect.bottom()), (0, 255, 0), 1)
                shape = predictor(gray, rect)
            
                shape = shape_to_np(shape)
                #draw_shape_lines_all(shape, frame)
                #draw_shape_lines_range(shape, frame, JAWLINE_POINTS)
                #draw_shape_points_pos_range(shape, frame, LEFT_EYE_POINTS + RIGHT_EYE_POINTS + NOSE_BRIDGE_POINTS)
                draw_shape_points(shape, frame)
        
        cv2.imshow("Landmarks detection using dlib", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
                break
	
video_capture.release()
cv2.destroyAllWindows()

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

landmarks_detection_fr.py
import cv2
import face_recognition
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')
	
image = cv2.imread("face_test.png")
image_68 = image.copy()
image_5 = image.copy()
	
rgb = image[:, :, ::-1]
face_landmarks_list_68 = face_recognition.face_landmarks(rgb)
print(face_landmarks_list_68)
	
for face_landmarks in face_landmarks_list_68:
    for facial_feature in face_landmarks.keys():
        for p in face_landmarks[facial_feature]:
            cv2.circle(image_68, p, 2, (0, 255, 0), -1)
	
face_landmarks_list_5 = face_recognition.face_landmarks(rgb, None, "small")
print(face_landmarks_list_5)
	
for face_landmarks in face_landmarks_list_5:
    for facial_feature in face_landmarks.keys():
        for p in face_landmarks[facial_feature]:
            cv2.circle(image_5, p, 2, (0, 255, 0), -1)
	
fig = plt.figure(figsize=(10, 5))
plt.suptitle("Facial landmarks detection using face_recognition", fontsize=14, fontweight='bold')
fig.patch.set_facecolor('silver')
show_img_with_matplotlib(image_68, "68 facial landmarks", 1)
show_img_with_matplotlib(image_5, "5 facial landmarks", 2)
	
plt.show()

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

face_tracking_correlation_filters.py
import cv2
import dlib
	
def draw_text_info():
    menu_pos_1 = (10, 20)
    menu_pos_2 = (10, 40)
    cv2.putText(frame, "Use '1' to re-initialize tracking", menu_pos_1, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    if tracking_face:
        cv2.putText(frame, "tracking the face", menu_pos_2, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
    else:
        cv2.putText(frame, "detecting a face to initialize tracking...", menu_pos_2, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
	
capture = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
tracker = dlib.correlation_tracker()
tracking_face = False
	
while True:
    ret, frame = capture.read()
	
    draw_text_info()
	
    if tracking_face is False:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        rects = detector(gray, 0)
        if len(rects) > 0:
            tracker.start_track(frame, rects[0])
            tracking_face = True
	
    if tracking_face is True:
        print(tracker.update(frame))
        pos = tracker.get_position()
        cv2.rectangle(frame, (int(pos.left()), int(pos.top())), (int(pos.right()), int(pos.bottom())), (0, 255, 0), 3)
	
    key = 0xFF & cv2.waitKey(1)
	
    if key == ord("1"):
        tracking_face = False
	
    if key == ord('q'):
        break
	
    cv2.imshow("Face tracking using dlib frontal face detector and correlation filters for tracking", frame)
	
capture.release()
cv2.destroyAllWindows()

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

object_tracking_correlation_filters.py
import cv2
import dlib
	
def draw_text_info():
    menu_pos = (10, 20)
    menu_pos_2 = (10, 40)
    menu_pos_3 = (10, 60)
    info_1 = "Use left click of the mouse to select the object to track"
    info_2 = "Use '1' to start tracking, '2' to reset tracking and 'q' to exit"
	
    cv2.putText(frame, info_1, menu_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    cv2.putText(frame, info_2, menu_pos_2, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
    if tracking_state:
        cv2.putText(frame, "tracking", menu_pos_3, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
    else:
        cv2.putText(frame, "not tracking", menu_pos_3, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
	
points = []
	
def mouse_event_handler(event, x, y, flags, param):
    global points
	
    if event == cv2.EVENT_LBUTTONDOWN:
        points = [(x, y)]
    elif event == cv2.EVENT_LBUTTONUP:
        points.append((x, y))
	
capture = cv2.VideoCapture(0)
window_name = "Object tracking using dlib correlation filter algorithm"
cv2.namedWindow(window_name)
cv2.setMouseCallback(window_name, mouse_event_handler)
tracker = dlib.correlation_tracker()
tracking_state = False
	
while True:
    ret, frame = capture.read()
	
    draw_text_info()
	
    if len(points) == 2:
        cv2.rectangle(frame, points[0], points[1], (0, 0, 255), 3)
        dlib_rectangle = dlib.rectangle(points[0][0], points[0][1], points[1][0], points[1][1])
	
    if tracking_state == True:
        tracker.update(frame)
        pos = tracker.get_position()
        cv2.rectangle(frame, (int(pos.left()), int(pos.top())), (int(pos.right()), int(pos.bottom())), (0, 255, 0), 3)
	
    key = 0xFF & cv2.waitKey(1)
	
    if key == ord("1"):
        if len(points) == 2:
            tracker.start_track(frame, dlib_rectangle)
            tracking_state = True
            points = []
	
    if key == ord("2"):
        points = []
        tracking_state = False
	
    if key == ord('q'):
        break
	
    cv2.imshow(window_name, frame)
	
capture.release()
cv2.destroyAllWindows()