diff --git a/README.md b/README.md index 4950d38..35cef0e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # ProjectBabble A landmark based mouth tracking method for VR + +**To download the current model, use the link below:** +https://drive.google.com/file/d/1rb3uVFeROwSs7uvuYlO9LpwvDmIfV4x5/view?usp=sharing \ No newline at end of file diff --git a/model_3.dat b/model_3.dat deleted file mode 100644 index a8c8932..0000000 Binary files a/model_3.dat and /dev/null differ diff --git a/mouth-inferer.py b/mouth-inferer.py index f402e93..677807c 100644 --- a/mouth-inferer.py +++ b/mouth-inferer.py @@ -5,6 +5,8 @@ import dlib import cv2 import numpy as np import time +import math +from oneeurofilter import OneEuroFilter def nothing1(x): print(x) def nothing2(y): @@ -13,12 +15,10 @@ def nothing3(z): print(z) def nothing4(y): print(y) -# construct the argument parser and parse the arguments -# initialize dlib's face detector (HOG-based) and then load our -# trained shape predictor -print("[INFO] loading facial landmark predictor...") -detector = dlib.get_frontal_face_detector() -predictor = dlib.shape_predictor("model_3.dat") +min_cutoff = 0.07 +beta = 0.03 +#print noisy point shape +predictor = dlib.shape_predictor("2model.dat") # initialize the video stream and allow the cammera sensor to warmup print("[INFO] camera sensor warming up...") vs = VideoStream(0).start() @@ -28,7 +28,12 @@ cv2.createTrackbar('1','Frame',1,1000,nothing1) cv2.createTrackbar('2','Frame',1,1000,nothing2) cv2.createTrackbar('3','Frame',1,1000,nothing3) cv2.createTrackbar('4','Frame',1,1000,nothing4) -# loop over the frames from the video stream +shape_array = np.zeros((32, 2), dtype="int") +one_euro_filter = OneEuroFilter( + shape_array, + min_cutoff=min_cutoff, + beta=beta + ) while True: a = 100 b = 200 @@ -43,13 +48,16 @@ while True: (x, y, w, h) = face_utils.rect_to_bb(rect) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) start = time.time() - shape = predictor(gray, rect) - shape = face_utils.shape_to_np(shape) - # loop over the (x, y)-coordinates from our dlib shape - # predictor model draw them on the image - - for (sX, sY) in shape: - cv2.circle(frame, (sX, sY), 1, (0, 0, 255), -1) + points = predictor(gray, rect) + points = face_utils.shape_to_np(points) + # loop over the (x, y)-coordinates for the facial landmarks + #and filter each point + points = one_euro_filter(points) + for (x, y) in points: + #make ints + x = int(x) + y = int(y) + cv2.circle(frame, (x, y), 1, (255, 0, 0), -1) end = time.time() try: fps = 1/(end - start) diff --git a/oneeurofilter.py b/oneeurofilter.py new file mode 100644 index 0000000..c672ab9 --- /dev/null +++ b/oneeurofilter.py @@ -0,0 +1,50 @@ +import numpy as np +from time import time + +def smoothing_factor(t_e, cutoff): + r = 2 * np.pi * cutoff * t_e + return r / (r + 1) + + +def exponential_smoothing(a, x, x_prev): + return a * x + (1 - a) * x_prev + + +class OneEuroFilter: + def __init__(self, x0, dx0=0.0, min_cutoff=1.0, beta=0.0, + d_cutoff=1.0): + """Initialize the one euro filter.""" + # The parameters. + self.data_shape = x0.shape + self.min_cutoff = np.full(x0.shape, min_cutoff) + self.beta = np.full(x0.shape, beta) + self.d_cutoff = np.full(x0.shape, d_cutoff) + # Previous values. + self.x_prev = x0.astype(np.float) + self.dx_prev = np.full(x0.shape, dx0) + self.t_prev = time() + + def __call__(self, x): + """Compute the filtered signal.""" + assert x.shape == self.data_shape + + t = time() + t_e = t - self.t_prev + t_e = np.full(x.shape, t_e) + + # The filtered derivative of the signal. + a_d = smoothing_factor(t_e, self.d_cutoff) + dx = (x - self.x_prev) / t_e + dx_hat = exponential_smoothing(a_d, dx, self.dx_prev) + + # The filtered signal. + cutoff = self.min_cutoff + self.beta * np.abs(dx_hat) + a = smoothing_factor(t_e, cutoff) + x_hat = exponential_smoothing(a, x, self.x_prev) + + # Memorize the previous values. + self.x_prev = x_hat + self.dx_prev = dx_hat + self.t_prev = t + + return x_hat \ No newline at end of file