Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip photos without a face #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions face-movie/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

cache = dict()

class NoFaceException(Exception):
pass


def prompt_user_to_choose_face(im, rects):
im = im.copy()
h, w = im.shape[:2]
Expand All @@ -51,7 +55,8 @@ def get_landmarks(im):
rects = DETECTOR(im, 1)
if len(rects) == 0 and len(DETECTOR(im, 0)) > 0:
rects = DETECTOR(im, 0)
assert len(rects) > 0, "No faces found!"
if len(rects) == 0:
raise NoFaceException("No faces found!")
target_rect = rects[0]
if len(rects) > 1:
target_rect = prompt_user_to_choose_face(im, rects)
Expand Down Expand Up @@ -137,7 +142,12 @@ def warp_im(im, M, dshape, prev):

def align_images(impath1, impath2, border, prev=None):
im1, landmarks1 = read_im_and_landmarks(impath1)
im2, landmarks2 = read_im_and_landmarks(impath2)
filename = os.path.basename(impath2).split('.')[0]
try:
im2, landmarks2 = read_im_and_landmarks(impath2)
except NoFaceException:
print("No face in {}, Skipped".format(filename))
return

T = transformation_from_points(landmarks1[ALIGN_POINTS],
landmarks2[ALIGN_POINTS])
Expand All @@ -150,7 +160,6 @@ def align_images(impath1, impath2, border, prev=None):

warped_im2 = warp_im(im2, M, im1.shape, prev)

filename = os.path.basename(impath2).split('.')[0]
cv2.imwrite("{}/{}.jpg".format(OUTPUT_DIR, filename), warped_im2)
print("Aligned {}".format(filename))
return warped_im2
Expand Down Expand Up @@ -182,7 +191,9 @@ def align_images(impath1, impath2, border, prev=None):
im_files = sorted(im_files, key=lambda x: x.split('/'))
for im in im_files:
if overlay:
prev = align_images(target, im_dir + '/' + im, border, prev)
new_image = align_images(target, im_dir + '/' + im, border, prev)
if new_image is not None:
prev = new_image
else:
align_images(target, im_dir + '/' + im, border)