In [11]:
from imutils.video import VideoStream
from imutils import face_utils
import datetime
import math
import argparse
import imutils
import time
import dlib
import cv2
def calcDist(a,b):
return math.sqrt((a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]))
def findDist(mat):
oo = calcDist(shape[0], shape[16])
om = (calcDist(shape[0], shape[8]) + calcDist(shape[8], shape[16]) )/ 2
egeg = calcDist(shape[36], shape[39])
eded = calcDist(shape[42], shape[45])
bgbd = calcDist(shape[48], shape[54])
ngnd = calcDist(shape[31], shape[35])
return [oo, om, egeg, eded, bgbd, ngnd]
def dist2vect(vect1, vect2):
tmp = 0
for a in range(len(vect1)):
tmp += int((vect1[a] - vect2[a])) * int((vect1[a] - vect2[a]))
return tmp
ap = argparse.ArgumentParser()
args = vars(ap.parse_args())
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
val = []
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
tmp = []
for rect in rects:
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
tmp = findDist(shape)
for (x, y) in shape:
cv2.circle(frame, (x, y), 1, (0, 0, 255), -1)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("s"):
val = tmp
if key == ord("a"):
try:
a = dist2vect(val, tmp)
if a < 100:
for i in range(0, 16):
cv2.line(image,(shape[i][0],shape[i][1]),(shape[i+1][0], shape[i+1][1]), (255,255,0), 2)
except:
pass
if key == ord("q"):
break
cv2.destroyAllWindows()
vs.stop()
In [ ]:
In [ ]: