-
Notifications
You must be signed in to change notification settings - Fork 3
/
windows-face-stabiliziation-with-DLIB.py
53 lines (44 loc) · 1.16 KB
/
windows-face-stabiliziation-with-DLIB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import cv2
import dlib
import numpy as np
from PIL import Image
def shapePoints(shape, dtype="int"):
coords = np.zeros((68, 2), dtype=dtype)
for i in range(0, 68):
coords[i] = (shape.part(i).x, shape.part(i).y)
return coords
def rectPoints(rect):
x = rect.left()
y = rect.top()
w = rect.right() - x
h = rect.bottom() - y
return (x, y, w, h)
def stabilization(cx, cy, frame):
x = 1000
y = 450
return x-cx, y-cy
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat')
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
rects = detector(frame, 0)
for rect in rects:
shape = predictor(frame, rect)
points = shapePoints(shape)
(x, y, w, h) = rectPoints(rect)
resized = frame[y: y+h, x:x+w]
cy = int((2*y+h)/2)
cx = int((2*x+w)/2)
color = (255,255,255)
cv2.circle(frame, (cx, cy), 4, color, 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
cv2.imshow('frame', frame)
x,y = stabilization(cx,cy,frame)
cv2.moveWindow('frame',int(x),int(y))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()