Spaces:
Running
Running
Fixed gr.Error bug not showing error message.
Browse files- app.py +115 -94
- test_functions.py +9 -1
app.py
CHANGED
@@ -39,105 +39,125 @@ model.eval()
|
|
39 |
|
40 |
|
41 |
def age_image(image_path: str, source_age: int, target_age: int) -> Image.Image:
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
def age_video(image_path: str, source_age: int, target_age: int, duration: int, fps: int) -> str:
|
52 |
-
image = Image.open(image_path)
|
53 |
-
|
54 |
-
orig_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
55 |
-
orig_path = orig_tmp.name
|
56 |
-
image.save(orig_path)
|
57 |
-
orig_tmp.close()
|
58 |
-
|
59 |
-
aged_img = age_image(image_path, source_age, target_age)
|
60 |
-
aged_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
61 |
-
aged_path = aged_tmp.name
|
62 |
-
aged_img.save(aged_path)
|
63 |
-
aged_tmp.close()
|
64 |
-
imagine(image_path, source_age)
|
65 |
-
|
66 |
-
client = Client("Robys01/Face-Morphing")
|
67 |
try:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
except Exception as e:
|
79 |
-
raise gr.Error(f"
|
80 |
-
|
81 |
-
# Unpack response for video path
|
82 |
-
video_path = None
|
83 |
-
# handle (data, msg) tuple
|
84 |
-
if isinstance(result, tuple):
|
85 |
-
data, msg = result
|
86 |
-
video_path = data.get('video') if isinstance(data, dict) else None
|
87 |
-
print(f"Response message: {msg}")
|
88 |
-
|
89 |
-
if not video_path or not os.path.exists(video_path):
|
90 |
-
raise gr.Error(f"Video file not found: {video_path}")
|
91 |
-
|
92 |
-
return video_path
|
93 |
|
94 |
|
95 |
def age_timelapse(image_path: str, source_age: int) -> str:
|
96 |
-
image = Image.open(image_path)
|
97 |
-
|
98 |
-
target_ages = [10, 20, 30, 50, 70]
|
99 |
-
# Filter out ages too close to source
|
100 |
-
filtered = [age for age in target_ages if abs(age - source_age) >= 4]
|
101 |
-
# Combine with source and sort
|
102 |
-
ages = sorted(set(filtered + [source_age]))
|
103 |
-
temp_handles = []
|
104 |
-
|
105 |
-
for age in ages:
|
106 |
-
if age == source_age:
|
107 |
-
img = image
|
108 |
-
else:
|
109 |
-
img = age_image(image_path, source_age, age)
|
110 |
-
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
111 |
-
path = tmp.name
|
112 |
-
img.save(path)
|
113 |
-
tmp.close()
|
114 |
-
temp_handles.append(handle_file(path))
|
115 |
-
imagine(image_path, source_age)
|
116 |
-
|
117 |
-
client = Client("Robys01/Face-Morphing")
|
118 |
try:
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
except Exception as e:
|
130 |
-
raise gr.Error(f"
|
131 |
-
|
132 |
-
video_path = None
|
133 |
-
if isinstance(result, tuple):
|
134 |
-
data, msg = result
|
135 |
-
video_path = data.get('video') if isinstance(data, dict) else None
|
136 |
-
print(f"Response message: {msg}")
|
137 |
-
|
138 |
-
if not video_path or not os.path.exists(video_path):
|
139 |
-
raise gr.Error(f"Timelapse video not found: {video_path}")
|
140 |
-
return video_path
|
141 |
|
142 |
|
143 |
demo_age_image = gr.Interface(
|
@@ -190,11 +210,12 @@ demo_age_timelapse = gr.Interface(
|
|
190 |
description="Generate a timelapse video showing the aging process at different ages."
|
191 |
)
|
192 |
|
193 |
-
iface = gr.TabbedInterface(
|
194 |
-
[demo_age_image, demo_age_video, demo_age_timelapse],
|
195 |
-
tab_names=["Face Aging", "Aging Video", "Aging Timelapse"],
|
196 |
-
title="Face Aging Demo",
|
197 |
-
)
|
198 |
|
199 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
iface.launch(server_name="0.0.0.0", server_port=7000)
|
|
|
39 |
|
40 |
|
41 |
def age_image(image_path: str, source_age: int, target_age: int) -> Image.Image:
|
42 |
+
try:
|
43 |
+
image = Image.open(image_path)
|
44 |
+
if image.mode not in ["RGB", "L"]:
|
45 |
+
print(f"Converting image from {image.mode} to RGB")
|
46 |
+
image = image.convert("RGB")
|
47 |
+
processed_image = process_image(model, image, source_age, target_age)
|
48 |
+
imagine(image_path, source_age)
|
49 |
+
return processed_image
|
50 |
+
except ValueError as e:
|
51 |
+
if "No faces detected" in str(e):
|
52 |
+
raise gr.Error("No faces detected in the image. Please upload an image with a clear, visible face.")
|
53 |
+
else:
|
54 |
+
raise gr.Error(f"Error processing image: {str(e)}")
|
55 |
+
except Exception as e:
|
56 |
+
raise gr.Error(f"Unexpected error: {str(e)}")
|
57 |
|
58 |
|
59 |
def age_video(image_path: str, source_age: int, target_age: int, duration: int, fps: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
try:
|
61 |
+
image = Image.open(image_path)
|
62 |
+
|
63 |
+
orig_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
64 |
+
orig_path = orig_tmp.name
|
65 |
+
image.save(orig_path)
|
66 |
+
orig_tmp.close()
|
67 |
+
|
68 |
+
aged_img = age_image(image_path, source_age, target_age)
|
69 |
+
aged_tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
70 |
+
aged_path = aged_tmp.name
|
71 |
+
aged_img.save(aged_path)
|
72 |
+
aged_tmp.close()
|
73 |
+
imagine(image_path, source_age)
|
74 |
+
|
75 |
+
client = Client("Robys01/Face-Morphing")
|
76 |
+
try:
|
77 |
+
result = client.predict(
|
78 |
+
image_files=[handle_file(orig_path), handle_file(aged_path)],
|
79 |
+
duration=duration,
|
80 |
+
fps=fps,
|
81 |
+
method="Dlib",
|
82 |
+
align_resize=False,
|
83 |
+
order_images=False,
|
84 |
+
guideline=False,
|
85 |
+
api_name="/predict"
|
86 |
+
)
|
87 |
+
except Exception as e:
|
88 |
+
raise gr.Error(f"Error during video generation: {e}")
|
89 |
+
|
90 |
+
# Unpack response for video path
|
91 |
+
video_path = None
|
92 |
+
# handle (data, msg) tuple
|
93 |
+
if isinstance(result, tuple):
|
94 |
+
data, msg = result
|
95 |
+
video_path = data.get('video') if isinstance(data, dict) else None
|
96 |
+
print(f"Response message: {msg}")
|
97 |
+
|
98 |
+
if not video_path or not os.path.exists(video_path):
|
99 |
+
raise gr.Error(f"Video file not found: {video_path}")
|
100 |
+
|
101 |
+
return video_path
|
102 |
+
except gr.Error:
|
103 |
+
# Re-raise Gradio errors as-is
|
104 |
+
raise
|
105 |
except Exception as e:
|
106 |
+
raise gr.Error(f"Unexpected error in video generation: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
|
109 |
def age_timelapse(image_path: str, source_age: int) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
try:
|
111 |
+
image = Image.open(image_path)
|
112 |
+
|
113 |
+
target_ages = [10, 20, 30, 50, 70]
|
114 |
+
# Filter out ages too close to source
|
115 |
+
filtered = [age for age in target_ages if abs(age - source_age) >= 4]
|
116 |
+
# Combine with source and sort
|
117 |
+
ages = sorted(set(filtered + [source_age]))
|
118 |
+
temp_handles = []
|
119 |
+
|
120 |
+
for age in ages:
|
121 |
+
if age == source_age:
|
122 |
+
img = image
|
123 |
+
else:
|
124 |
+
img = age_image(image_path, source_age, age)
|
125 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
126 |
+
path = tmp.name
|
127 |
+
img.save(path)
|
128 |
+
tmp.close()
|
129 |
+
temp_handles.append(handle_file(path))
|
130 |
+
imagine(image_path, source_age)
|
131 |
+
|
132 |
+
client = Client("Robys01/Face-Morphing")
|
133 |
+
try:
|
134 |
+
result = client.predict(
|
135 |
+
image_files=temp_handles,
|
136 |
+
duration=3,
|
137 |
+
fps=20,
|
138 |
+
method="Dlib",
|
139 |
+
align_resize=False,
|
140 |
+
order_images=False,
|
141 |
+
guideline=False,
|
142 |
+
api_name="/predict"
|
143 |
+
)
|
144 |
+
except Exception as e:
|
145 |
+
raise gr.Error(f"Error generating timelapse video: {e}")
|
146 |
+
|
147 |
+
video_path = None
|
148 |
+
if isinstance(result, tuple):
|
149 |
+
data, msg = result
|
150 |
+
video_path = data.get('video') if isinstance(data, dict) else None
|
151 |
+
print(f"Response message: {msg}")
|
152 |
+
|
153 |
+
if not video_path or not os.path.exists(video_path):
|
154 |
+
raise gr.Error(f"Timelapse video not found: {video_path}")
|
155 |
+
return video_path
|
156 |
+
except gr.Error:
|
157 |
+
# Re-raise Gradio errors as-is
|
158 |
+
raise
|
159 |
except Exception as e:
|
160 |
+
raise gr.Error(f"Unexpected error in timelapse generation: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
|
163 |
demo_age_image = gr.Interface(
|
|
|
210 |
description="Generate a timelapse video showing the aging process at different ages."
|
211 |
)
|
212 |
|
|
|
|
|
|
|
|
|
|
|
213 |
|
214 |
if __name__ == "__main__":
|
215 |
+
|
216 |
+
iface = gr.TabbedInterface(
|
217 |
+
[demo_age_image, demo_age_video, demo_age_timelapse],
|
218 |
+
tab_names=["Face Aging", "Aging Video", "Aging Timelapse"],
|
219 |
+
title="Face Aging Demo",
|
220 |
+
).queue()
|
221 |
iface.launch(server_name="0.0.0.0", server_port=7000)
|
test_functions.py
CHANGED
@@ -57,7 +57,15 @@ def process_image(your_model, image, source_age, target_age=0,
|
|
57 |
# image = face_recognition.load_image_file(filename)
|
58 |
image = np.array(image)
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# calculate margins
|
63 |
margin_y_t = int((fl[2] - fl[0]) * .63 * .85) # larger as the forehead is often cut off
|
|
|
57 |
# image = face_recognition.load_image_file(filename)
|
58 |
image = np.array(image)
|
59 |
|
60 |
+
# Detect faces in the image
|
61 |
+
face_locations = face_recognition.face_locations(image)
|
62 |
+
|
63 |
+
# Check if any faces were detected
|
64 |
+
if not face_locations:
|
65 |
+
raise ValueError("No faces detected in the image. Please ensure the image contains a clear, visible face.")
|
66 |
+
|
67 |
+
# Use the first detected face
|
68 |
+
fl = face_locations[0]
|
69 |
|
70 |
# calculate margins
|
71 |
margin_y_t = int((fl[2] - fl[0]) * .63 * .85) # larger as the forehead is often cut off
|