Spaces:
Running
Running
Added better preprocessing logic
Browse files- app.py +13 -4
- examples/1.jpg +0 -0
- examples/10.jpg +0 -0
- examples/2.jpg +0 -0
- examples/3.jpg +0 -0
- examples/4.jpg +0 -0
- examples/5.jpg +0 -0
- examples/6.jpg +0 -0
- examples/7.jpg +0 -0
- examples/8.jpg +0 -0
- examples/9.jpg +0 -0
- src/utils/align_images.py +8 -10
app.py
CHANGED
@@ -12,16 +12,18 @@ def transition(image_files, duration, fps, method, align_resize, order_images, g
|
|
12 |
is_dlib = method == "Dlib"
|
13 |
|
14 |
debug_messages = []
|
|
|
15 |
|
16 |
try:
|
|
|
|
|
|
|
|
|
17 |
# Align and resize images
|
18 |
if align_resize:
|
19 |
aligned_dir = "aligned_images"
|
20 |
image_files = align_images(image_files, aligned_dir)
|
21 |
|
22 |
-
# Sort images by age
|
23 |
-
if order_images:
|
24 |
-
image_files = sort_images(image_files)
|
25 |
|
26 |
morph(image_files, duration, fps, output_name, guideline, is_dlib)
|
27 |
debug_messages.append("Video generation successful")
|
@@ -48,7 +50,14 @@ if __name__ == "__main__":
|
|
48 |
],
|
49 |
outputs=[gr.Video(), gr.Textbox(label="Output Message")],
|
50 |
examples=[
|
51 |
-
[["examples/1.png", "examples/2.png", "examples/3.png"], 3, 30, "Dlib", False, False, False]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
],
|
53 |
title="Face Morphing",
|
54 |
description="Upload multiple images containing faces to create a transition video between them."
|
|
|
12 |
is_dlib = method == "Dlib"
|
13 |
|
14 |
debug_messages = []
|
15 |
+
print(f"Properties: Duration: {duration}, FPS: {fps}, Method: {method}, Align and Resize: {align_resize}, Order Images: {order_images}, Guideline: {guideline}")
|
16 |
|
17 |
try:
|
18 |
+
# Sort images by age
|
19 |
+
if order_images:
|
20 |
+
image_files = sort_images(image_files)
|
21 |
+
|
22 |
# Align and resize images
|
23 |
if align_resize:
|
24 |
aligned_dir = "aligned_images"
|
25 |
image_files = align_images(image_files, aligned_dir)
|
26 |
|
|
|
|
|
|
|
27 |
|
28 |
morph(image_files, duration, fps, output_name, guideline, is_dlib)
|
29 |
debug_messages.append("Video generation successful")
|
|
|
50 |
],
|
51 |
outputs=[gr.Video(), gr.Textbox(label="Output Message")],
|
52 |
examples=[
|
53 |
+
[["examples/1.png", "examples/2.png", "examples/3.png"], 3, 30, "Dlib", False, False, False],
|
54 |
+
[["examples/1.png", "examples/2.png", "examples/3.png"], 5, 30, "MediaPipe", False, False, True],
|
55 |
+
[["examples/1.jpg", "examples/2.jpg", "examples/3.jpg",
|
56 |
+
"examples/4.jpg", "examples/5.jpg", "examples/6.jpg", "examples/7.jpg",
|
57 |
+
"examples/8.jpg", "examples/9.jpg", "examples/10.jpg"], 3, 20, "Dlib", True, True, False],
|
58 |
+
# [["examples/big_1.png", "examples/big_2.png", "examples/big_3.png",
|
59 |
+
# "examples/big_4.png", "examples/big_5.png", "examples/big_6.png"], 3, 30, "Dlib", False, False, False]
|
60 |
+
|
61 |
],
|
62 |
title="Face Morphing",
|
63 |
description="Upload multiple images containing faces to create a transition video between them."
|
examples/1.jpg
ADDED
![]() |
examples/10.jpg
ADDED
![]() |
examples/2.jpg
ADDED
![]() |
examples/3.jpg
ADDED
![]() |
examples/4.jpg
ADDED
![]() |
examples/5.jpg
ADDED
![]() |
examples/6.jpg
ADDED
![]() |
examples/7.jpg
ADDED
![]() |
examples/8.jpg
ADDED
![]() |
examples/9.jpg
ADDED
![]() |
src/utils/align_images.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
|
3 |
-
import argparse
|
4 |
from src.utils.face_alignment import image_align
|
5 |
from src.landmark_detector import LandmarksDetector
|
6 |
|
@@ -21,27 +20,26 @@ def align_images(img_files, aligned_dir, output_size=1024, x_scale=1, y_scale=1,
|
|
21 |
os.remove(os.path.join(ALIGNED_IMAGES_DIR, file))
|
22 |
|
23 |
landmarks_detector = LandmarksDetector()
|
24 |
-
for img_path in img_files:
|
25 |
img_name = os.path.basename(img_path)
|
26 |
print('Aligning %s ...' % img_name)
|
27 |
try:
|
28 |
raw_img_path = img_path
|
29 |
-
fn = face_img_name = '
|
30 |
-
if os.path.isfile(fn):
|
31 |
continue
|
32 |
print('Getting landmarks...')
|
33 |
-
|
34 |
for i, face_landmarks in enumerate(landmarks_detector.get_landmarks(raw_img_path), start=1):
|
35 |
try:
|
36 |
print('Starting face alignment...')
|
37 |
-
face_img_name = '
|
38 |
aligned_face_path = os.path.join(ALIGNED_IMAGES_DIR, face_img_name)
|
39 |
image_align(raw_img_path, aligned_face_path, face_landmarks, output_size=output_size, x_scale=x_scale, y_scale=y_scale, em_scale=em_scale, alpha=use_alpha)
|
40 |
print('Wrote result %s\n' % aligned_face_path)
|
41 |
except Exception as e:
|
42 |
-
|
43 |
-
except:
|
44 |
-
|
45 |
|
46 |
# return absolute paths of aligned images
|
47 |
-
return [os.path.join(ALIGNED_IMAGES_DIR, img) for img in os.listdir(ALIGNED_IMAGES_DIR)]
|
|
|
1 |
import os
|
2 |
|
|
|
3 |
from src.utils.face_alignment import image_align
|
4 |
from src.landmark_detector import LandmarksDetector
|
5 |
|
|
|
20 |
os.remove(os.path.join(ALIGNED_IMAGES_DIR, file))
|
21 |
|
22 |
landmarks_detector = LandmarksDetector()
|
23 |
+
for idx, img_path in enumerate(img_files, start=1):
|
24 |
img_name = os.path.basename(img_path)
|
25 |
print('Aligning %s ...' % img_name)
|
26 |
try:
|
27 |
raw_img_path = img_path
|
28 |
+
fn = face_img_name = f'{idx:04d}_{os.path.splitext(img_name)[0]}_01.png'
|
29 |
+
if os.path.isfile(os.path.join(ALIGNED_IMAGES_DIR, fn)):
|
30 |
continue
|
31 |
print('Getting landmarks...')
|
|
|
32 |
for i, face_landmarks in enumerate(landmarks_detector.get_landmarks(raw_img_path), start=1):
|
33 |
try:
|
34 |
print('Starting face alignment...')
|
35 |
+
face_img_name = f'{idx:04d}_{os.path.splitext(img_name)[0]}_{i:02d}.png'
|
36 |
aligned_face_path = os.path.join(ALIGNED_IMAGES_DIR, face_img_name)
|
37 |
image_align(raw_img_path, aligned_face_path, face_landmarks, output_size=output_size, x_scale=x_scale, y_scale=y_scale, em_scale=em_scale, alpha=use_alpha)
|
38 |
print('Wrote result %s\n' % aligned_face_path)
|
39 |
except Exception as e:
|
40 |
+
print(f"Exception in face alignment for {img_name}: {e}")
|
41 |
+
except Exception as e:
|
42 |
+
print(f"Exception in landmark detection for {img_name}: {e}")
|
43 |
|
44 |
# return absolute paths of aligned images
|
45 |
+
return [os.path.join(ALIGNED_IMAGES_DIR, img) for img in os.listdir(ALIGNED_IMAGES_DIR)]
|