dlibで「おでこ」を推定

dlibで顔のパーツを検出すると眉毛のあたりで切れちゃうので、 おでこというか 頭頂部も ざっくり推定。
こちらのプログラムを参考というか だいぶ コピペしました

qiita.com

facelandmark.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import dlib
import cv2
import time
import copy
import numpy as np

resize_rate=1
npoints=85

predictor_path = "./shape_predictor_68_face_landmarks.dat"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

class NoFaceException(Exception):pass

def landmark(frame,nfaces=1,zero2one=True):
if isinstance (frame ,str):
frame=cv2.imread(frame)
height,width,c=frame.shape

# 顔検出
dets = detector(frame, 1)
detlist=list(dets)
if len(detlist)==0:
raise NoFaceException
if nfaces==0 or nfaces>=3:
nfaces=2
faces=[]
for k, d in enumerate(detlist[:nfaces]):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(), d.top(), d.right(), d.bottom()))

# 顔器官検出
shape = predictor(frame, d)
px=np.zeros(npoints,dtype=int)
py=np.zeros(npoints,dtype=int)
for shape_point_count in range(shape.num_parts):
shape_point = shape.part(shape_point_count)
px[shape_point_count]=shape_point.x
py[shape_point_count]=shape_point.y
px[68]=(px[0]+px[16])//2
py[68]=(py[0]+py[16])//2
px[69]=px[68]+(px[68]-px[8])
py[69]=py[68]+(py[68]-py[8])
px[70]=(px[21]+px[22])//2
py[70]=(py[21]+py[22])//2
#
mirror=[ (71,15), (72,14),(73,13), (74,12),(75,11),(76,10),(77,9),
(78,7),(79,6),(80,5),(81,4),(82,3),(83,2),(84,1)]
ox=px[68]
oy=py[68]
for i1,i2 in mirror:
px[i1]=px[68]+(px[68]-px[i2])
py[i1]=py[68]+(py[68]-py[i2])
if zero2one:
px=px/width
py=py/height
faces.append([ px,py])
return faces

if __name__=="__main__":
import glob
flist=glob.glob("face/*.jpg")
np.random.shuffle(flist)
fn=np.random.choice(flist)
frame=cv2.imread(fn)

faces=landmark(frame,nfaces=1,zero2one=False)
px,py=faces[0]
h,w,c=frame.shape

frame[:,:,:]=255
for shape_point_count in range(npoints):
text=str(shape_point_count)
org=(px[shape_point_count],py[shape_point_count])
fontFace=cv2.FONT_HERSHEY_SIMPLEX
color=(0,0,0)
fontScale=0.45
cv2.putText(frame, text, org, fontFace, fontScale, color, thickness=1, lineType=cv2.LINE_8)
cv2.imshow('face landmark detector',frame)
cv2.imwrite("test.png",frame)



f:id:boxheadroom:20181019215926p:plain