Skip to content

Commit

Permalink
video
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhuikai committed Feb 21, 2020
1 parent 1bc8cc4 commit 5ec02e3
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 33,910 deletions.
12 changes: 0 additions & 12 deletions config.py

This file was deleted.

197 changes: 0 additions & 197 deletions face_detect_and_track.py

This file was deleted.

59 changes: 59 additions & 0 deletions face_detection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import cv2
import dlib
import numpy as np

## Face detection
def face_detection(img,upsample_times=1):
Expand All @@ -9,3 +11,60 @@ def face_detection(img,upsample_times=1):
faces = detector(img, upsample_times)

return faces

PREDICTOR_PATH = 'models/shape_predictor_68_face_landmarks.dat'
predictor = dlib.shape_predictor(PREDICTOR_PATH)
## Face and points detection
def face_points_detection(img, bbox:dlib.rectangle):
# Get the landmarks/parts for the face in box d.
shape = predictor(img, bbox)

# loop over the 68 facial landmarks and convert them
# to a 2-tuple of (x, y)-coordinates
coords = np.asarray(list([p.x, p.y] for p in shape.parts()), dtype=np.int)

# return the array of (x, y)-coordinates
return coords

def select_face(im, r=10, choose=True):
faces = face_detection(im)

if len(faces) == 0:
return None, None, None

if len(faces) == 1 or not choose:
idx = np.argmax([(face.right() - face.left()) * (face.bottom() - face.top()) for face in faces])
bbox = faces[idx]
else:
bbox = []

def click_on_face(event, x, y, flags, params):
if event != cv2.EVENT_LBUTTONDOWN:
return

for face in faces:
if face.left() < x < face.right() and face.top() < y < face.bottom():
bbox.append(face)
break

im_copy = im.copy()
for face in faces:
# draw the face bounding box
cv2.rectangle(im_copy, (face.left(), face.top()), (face.right(), face.bottom()), (0, 0, 255), 1)
cv2.imshow('Click the Face:', im_copy)
cv2.setMouseCallback('Click the Face:', click_on_face)
while len(bbox) == 0:
cv2.waitKey(1)
cv2.destroyAllWindows()
bbox = bbox[0]

points = np.asarray(face_points_detection(im, bbox))

im_w, im_h = im.shape[:2]
left, top = np.min(points, 0)
right, bottom = np.max(points, 0)

x, y = max(0, left - r), max(0, top - r)
w, h = min(right + r, im_h) - x, min(bottom + r, im_w) - y

return points - np.asarray([[x, y]]), (x, y, w, h), im[y:y + h, x:x + w]
17 changes: 0 additions & 17 deletions face_points_detection.py

This file was deleted.

39 changes: 39 additions & 0 deletions face_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,42 @@ def check_points(img,points):
else:
return True
return False


def face_swap(src_face, dst_face, src_points, dst_points, dst_shape, dst_img, args, end=48):
h, w = dst_face.shape[:2]

## 3d warp
warped_src_face = warp_image_3d(src_face, src_points[:end], dst_points[:end], (h, w))
## Mask for blending
mask = mask_from_points((h, w), dst_points)
mask_src = np.mean(warped_src_face, axis=2) > 0
mask = np.asarray(mask * mask_src, dtype=np.uint8)
## Correct color
if args.correct_color:
warped_src_face = apply_mask(warped_src_face, mask)
dst_face_masked = apply_mask(dst_face, mask)
warped_src_face = correct_colours(dst_face_masked, warped_src_face, dst_points)
## 2d warp
if args.warp_2d:
unwarped_src_face = warp_image_3d(warped_src_face, dst_points[:end], src_points[:end], src_face.shape[:2])
warped_src_face = warp_image_2d(unwarped_src_face, transformation_from_points(dst_points, src_points),
(h, w, 3))

mask = mask_from_points((h, w), dst_points)
mask_src = np.mean(warped_src_face, axis=2) > 0
mask = np.asarray(mask * mask_src, dtype=np.uint8)

## Shrink the mask
kernel = np.ones((10, 10), np.uint8)
mask = cv2.erode(mask, kernel, iterations=1)
##Poisson Blending
r = cv2.boundingRect(mask)
center = ((r[0] + int(r[2] / 2), r[1] + int(r[3] / 2)))
output = cv2.seamlessClone(warped_src_face, dst_face, mask, center, cv2.NORMAL_CLONE)

x, y, w, h = dst_shape
dst_img_cp = dst_img.copy()
dst_img_cp[y:y + h, x:x + w] = output

return dst_img_cp
Binary file removed imgs/realtime0.gif
Binary file not shown.
Loading

0 comments on commit 5ec02e3

Please sign in to comment.