Spaces:
Running
Running
Added new video features
Browse files
app.py
CHANGED
|
@@ -5,21 +5,35 @@ from models import UNet
|
|
| 5 |
from test_functions import process_image
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
|
|
|
| 8 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
os.makedirs("model", exist_ok=True)
|
|
|
|
| 13 |
|
| 14 |
-
if
|
|
|
|
| 15 |
print("Starting model download at", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 16 |
-
path = hf_hub_download(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
print(f"Model downloaded to {path}")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
model = UNet()
|
| 20 |
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu"), weights_only=False))
|
| 21 |
model.eval()
|
| 22 |
|
|
|
|
| 23 |
def age_image(image: Image.Image, source_age: int, target_age: int) -> Image.Image:
|
| 24 |
if image.mode not in ["RGB", "L"]:
|
| 25 |
print(f"Converting image from {image.mode} to RGB")
|
|
@@ -27,11 +41,99 @@ def age_image(image: Image.Image, source_age: int, target_age: int) -> Image.Ima
|
|
| 27 |
processed_image = process_image(model, image, source_age, target_age)
|
| 28 |
return processed_image
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Pre-load the example images as PIL objects
|
| 31 |
example1 = Image.open("examples/girl.jpg")
|
| 32 |
-
example2 = Image.open("examples/
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
fn=age_image,
|
| 36 |
inputs=[
|
| 37 |
gr.Image(type="pil", label="Input Image"),
|
|
@@ -41,11 +143,48 @@ iface = gr.Interface(
|
|
| 41 |
outputs=gr.Image(type="pil", label="Aged Image"),
|
| 42 |
examples=[
|
| 43 |
[example1, 14, 50],
|
| 44 |
-
[example2,
|
|
|
|
|
|
|
| 45 |
],
|
| 46 |
-
|
| 47 |
description="Upload an image along with a source age approximation and a target age to generate an aged version of the face."
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if __name__ == "__main__":
|
| 51 |
iface.launch(server_name="0.0.0.0", server_port=7000)
|
|
|
|
| 5 |
from test_functions import process_image
|
| 6 |
from PIL import Image
|
| 7 |
import gradio as gr
|
| 8 |
+
from gradio_client import Client, handle_file
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
+
import tempfile
|
| 11 |
+
import requests
|
| 12 |
|
| 13 |
+
# Ensure model directory exists
|
|
|
|
| 14 |
os.makedirs("model", exist_ok=True)
|
| 15 |
+
MODEL_PATH = "model/best_unet_model.pth"
|
| 16 |
|
| 17 |
+
# Download model if missing
|
| 18 |
+
def download_model():
|
| 19 |
print("Starting model download at", time.strftime("%Y-%m-%d %H:%M:%S"))
|
| 20 |
+
path = hf_hub_download(
|
| 21 |
+
repo_id="Robys01/face-aging",
|
| 22 |
+
filename="best_unet_model.pth",
|
| 23 |
+
local_dir="model",
|
| 24 |
+
cache_dir="model"
|
| 25 |
+
)
|
| 26 |
print(f"Model downloaded to {path}")
|
| 27 |
|
| 28 |
+
if not os.path.exists(MODEL_PATH):
|
| 29 |
+
download_model()
|
| 30 |
+
|
| 31 |
+
# Load model
|
| 32 |
model = UNet()
|
| 33 |
model.load_state_dict(torch.load(MODEL_PATH, map_location=torch.device("cpu"), weights_only=False))
|
| 34 |
model.eval()
|
| 35 |
|
| 36 |
+
|
| 37 |
def age_image(image: Image.Image, source_age: int, target_age: int) -> Image.Image:
|
| 38 |
if image.mode not in ["RGB", "L"]:
|
| 39 |
print(f"Converting image from {image.mode} to RGB")
|
|
|
|
| 41 |
processed_image = process_image(model, image, source_age, target_age)
|
| 42 |
return processed_image
|
| 43 |
|
| 44 |
+
|
| 45 |
+
def age_video(image: Image.Image, source_age: int, target_age: int, duration: int, fps: int) -> str:
|
| 46 |
+
orig_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 47 |
+
orig_path = orig_tmp.name
|
| 48 |
+
image.save(orig_path)
|
| 49 |
+
orig_tmp.close()
|
| 50 |
+
|
| 51 |
+
aged_img = age_image(image, source_age, target_age)
|
| 52 |
+
aged_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 53 |
+
aged_path = aged_tmp.name
|
| 54 |
+
aged_img.save(aged_path)
|
| 55 |
+
aged_tmp.close()
|
| 56 |
+
|
| 57 |
+
client = Client("Robys01/Face-Morphing")
|
| 58 |
+
try:
|
| 59 |
+
result = client.predict(
|
| 60 |
+
image_files=[handle_file(orig_path), handle_file(aged_path)],
|
| 61 |
+
duration=duration,
|
| 62 |
+
fps=fps,
|
| 63 |
+
method="Dlib",
|
| 64 |
+
align_resize=False,
|
| 65 |
+
order_images=False,
|
| 66 |
+
guideline=False,
|
| 67 |
+
api_name="/predict"
|
| 68 |
+
)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise gr.Error(f"Error during video generation: {e}")
|
| 71 |
+
|
| 72 |
+
# Unpack response for video path
|
| 73 |
+
video_path = None
|
| 74 |
+
# handle (data, msg) tuple
|
| 75 |
+
if isinstance(result, tuple):
|
| 76 |
+
data, msg = result
|
| 77 |
+
video_path = data.get('video') if isinstance(data, dict) else None
|
| 78 |
+
print(f"Response message: {msg}")
|
| 79 |
+
|
| 80 |
+
if not video_path or not os.path.exists(video_path):
|
| 81 |
+
raise gr.Error(f"Video file not found: {video_path}")
|
| 82 |
+
|
| 83 |
+
return video_path
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def age_timelapse(image: Image.Image, source_age: int) -> str:
|
| 87 |
+
target_ages = [10, 20, 30, 50, 70]
|
| 88 |
+
# Filter out ages too close to source
|
| 89 |
+
filtered = [age for age in target_ages if abs(age - source_age) >= 3]
|
| 90 |
+
# Combine with source and sort
|
| 91 |
+
ages = sorted(set(filtered + [source_age]))
|
| 92 |
+
temp_handles = []
|
| 93 |
+
|
| 94 |
+
for age in ages:
|
| 95 |
+
if age == source_age:
|
| 96 |
+
img = image
|
| 97 |
+
else:
|
| 98 |
+
img = age_image(image, source_age, age)
|
| 99 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
| 100 |
+
path = tmp.name
|
| 101 |
+
img.save(path)
|
| 102 |
+
tmp.close()
|
| 103 |
+
temp_handles.append(handle_file(path))
|
| 104 |
+
|
| 105 |
+
client = Client("Robys01/Face-Morphing")
|
| 106 |
+
try:
|
| 107 |
+
result = client.predict(
|
| 108 |
+
image_files=temp_handles,
|
| 109 |
+
duration=3,
|
| 110 |
+
fps=20,
|
| 111 |
+
method="Dlib",
|
| 112 |
+
align_resize=False,
|
| 113 |
+
order_images=False,
|
| 114 |
+
guideline=False,
|
| 115 |
+
api_name="/predict"
|
| 116 |
+
)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
raise gr.Error(f"Error generating timelapse video: {e}")
|
| 119 |
+
|
| 120 |
+
video_path = None
|
| 121 |
+
if isinstance(result, tuple):
|
| 122 |
+
data, msg = result
|
| 123 |
+
video_path = data.get('video') if isinstance(data, dict) else None
|
| 124 |
+
print(f"Response message: {msg}")
|
| 125 |
+
|
| 126 |
+
if not video_path or not os.path.exists(video_path):
|
| 127 |
+
raise gr.Error(f"Timelapse video not found: {video_path}")
|
| 128 |
+
return video_path
|
| 129 |
+
|
| 130 |
+
|
| 131 |
# Pre-load the example images as PIL objects
|
| 132 |
example1 = Image.open("examples/girl.jpg")
|
| 133 |
+
example2 = Image.open("examples/man.jpg")
|
| 134 |
+
example3 = Image.open("examples/trump.jpg")
|
| 135 |
|
| 136 |
+
demo_age_image = gr.Interface(
|
| 137 |
fn=age_image,
|
| 138 |
inputs=[
|
| 139 |
gr.Image(type="pil", label="Input Image"),
|
|
|
|
| 143 |
outputs=gr.Image(type="pil", label="Aged Image"),
|
| 144 |
examples=[
|
| 145 |
[example1, 14, 50],
|
| 146 |
+
[example2, 45, 70],
|
| 147 |
+
[example2, 45, 20],
|
| 148 |
+
[example3, 74, 30],
|
| 149 |
],
|
| 150 |
+
cache_examples=True,
|
| 151 |
description="Upload an image along with a source age approximation and a target age to generate an aged version of the face."
|
| 152 |
)
|
| 153 |
|
| 154 |
+
demo_age_video = gr.Interface(
|
| 155 |
+
fn=age_video,
|
| 156 |
+
inputs=[
|
| 157 |
+
gr.Image(type="pil", label="Input Image"),
|
| 158 |
+
gr.Slider(10, 90, value=20, step=1, label="Current age", info="Choose the current age"),
|
| 159 |
+
gr.Slider(10, 90, value=70, step=1, label="Target age", info="Choose the desired age"),
|
| 160 |
+
gr.Slider(label="Duration (seconds)", minimum=1, maximum=10, step=1, value=3),
|
| 161 |
+
gr.Slider(label="Frames per second (fps)", minimum=2, maximum=60, step=1, value=30),
|
| 162 |
+
],
|
| 163 |
+
outputs=gr.Video(label="Aged Video", format="mp4"),
|
| 164 |
+
|
| 165 |
+
examples=[
|
| 166 |
+
[example1, 14, 50, 3, 30],
|
| 167 |
+
[example2, 45, 70, 3, 30],
|
| 168 |
+
[example2, 45, 20, 3, 30],
|
| 169 |
+
],
|
| 170 |
+
cache_examples=True,
|
| 171 |
+
description="Generate a video of the aging process."
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
demo_age_timelapse = gr.Interface(
|
| 175 |
+
fn=age_timelapse,
|
| 176 |
+
inputs=[gr.Image(type="pil", label="Input Image"), gr.Slider(10, 90, value=20, step=1, label="Current age")],
|
| 177 |
+
outputs=[gr.Video(label="Aging Timelapse", format="mp4")],
|
| 178 |
+
examples=[[example1, 14], [example2, 45]],
|
| 179 |
+
cache_examples=True,
|
| 180 |
+
description="Generate a timelapse video showing the aging process at different ages."
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
iface = gr.TabbedInterface(
|
| 184 |
+
[demo_age_image, demo_age_video, demo_age_timelapse],
|
| 185 |
+
tab_names=["Face Aging", "Aging Video", "Aging Timelapse"],
|
| 186 |
+
title="Face Aging Demo",
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
if __name__ == "__main__":
|
| 190 |
iface.launch(server_name="0.0.0.0", server_port=7000)
|