Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,6 @@ import pytz
|
|
12 |
import time
|
13 |
import shutil
|
14 |
import numpy as np
|
15 |
-
import cv2
|
16 |
from PIL import Image, ImageEnhance, ImageFilter
|
17 |
|
18 |
# Get API key from Hugging Face Spaces secrets
|
@@ -156,37 +155,66 @@ def make_younger(input_image, youth_level=50):
|
|
156 |
if input_image is None:
|
157 |
return None, "No image uploaded. Please upload an image first."
|
158 |
|
|
|
|
|
|
|
159 |
# Convert to PIL Image if necessary
|
160 |
if isinstance(input_image, np.ndarray):
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
|
163 |
# Youth level should be between 0 and 100
|
164 |
youth_level = max(0, min(100, youth_level))
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
# Apply a series of transformations to make the person look younger
|
167 |
|
168 |
# 1. Smooth skin (reduce wrinkles)
|
169 |
smoothing_factor = youth_level / 100
|
170 |
-
|
|
|
171 |
|
172 |
# 2. Enhance brightness slightly (younger skin tends to be brighter)
|
|
|
173 |
brightness_enhancer = ImageEnhance.Brightness(smoothed)
|
174 |
-
brightened = brightness_enhancer.enhance(1 + (smoothing_factor * 0.
|
175 |
|
176 |
# 3. Enhance color (more vibrant)
|
|
|
177 |
color_enhancer = ImageEnhance.Color(brightened)
|
178 |
-
colored = color_enhancer.enhance(1 + (smoothing_factor * 0.
|
179 |
|
180 |
# 4. Adjust contrast (younger skin has better contrast)
|
|
|
181 |
contrast_enhancer = ImageEnhance.Contrast(colored)
|
182 |
-
contrasted = contrast_enhancer.enhance(1 + (smoothing_factor * 0.
|
183 |
|
184 |
# 5. Sharpen to maintain some details
|
|
|
185 |
sharpened = contrasted.filter(ImageFilter.SHARPEN)
|
186 |
|
|
|
187 |
return sharpened, f"Image processed successfully! Youth level applied: {youth_level}%"
|
188 |
|
189 |
except Exception as e:
|
|
|
|
|
|
|
190 |
return None, f"Error processing image: {str(e)}"
|
191 |
|
192 |
# Function to handle the case when no PDF is found
|
@@ -313,19 +341,43 @@ def main():
|
|
313 |
gr.Markdown("<p style='text-align: center; color: black;'>Upload an image to make the person look younger.</p>")
|
314 |
|
315 |
with gr.Row():
|
|
|
316 |
input_image = gr.Image(label="Upload Image", type="pil")
|
317 |
-
output_image = gr.Image(label="Younger Version",
|
318 |
|
319 |
with gr.Row():
|
320 |
youth_slider = gr.Slider(minimum=0, maximum=100, value=50, step=5, label="Youth Level (%)")
|
321 |
-
process_button = gr.Button("Make Younger")
|
322 |
|
|
|
323 |
result_text = gr.Textbox(label="Processing Result", interactive=False)
|
324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
process_button.click(
|
326 |
-
fn=
|
327 |
inputs=[input_image, youth_slider],
|
328 |
-
outputs=[output_image, result_text]
|
329 |
)
|
330 |
|
331 |
# Launch the interface
|
|
|
12 |
import time
|
13 |
import shutil
|
14 |
import numpy as np
|
|
|
15 |
from PIL import Image, ImageEnhance, ImageFilter
|
16 |
|
17 |
# Get API key from Hugging Face Spaces secrets
|
|
|
155 |
if input_image is None:
|
156 |
return None, "No image uploaded. Please upload an image first."
|
157 |
|
158 |
+
# Debug info
|
159 |
+
print(f"Input image type: {type(input_image)}")
|
160 |
+
|
161 |
# Convert to PIL Image if necessary
|
162 |
if isinstance(input_image, np.ndarray):
|
163 |
+
print("Converting numpy array to PIL Image")
|
164 |
+
# Ensure the array is in the correct format (RGB, uint8)
|
165 |
+
if input_image.dtype != np.uint8:
|
166 |
+
input_image = input_image.astype(np.uint8)
|
167 |
+
input_image = Image.fromarray(input_image)
|
168 |
+
|
169 |
+
# Ensure we have a valid PIL Image
|
170 |
+
if not isinstance(input_image, Image.Image):
|
171 |
+
return None, f"Expected PIL Image or numpy array, got {type(input_image)}"
|
172 |
+
|
173 |
+
# Make a copy to avoid modifying the original
|
174 |
+
img = input_image.copy()
|
175 |
|
176 |
# Youth level should be between 0 and 100
|
177 |
youth_level = max(0, min(100, youth_level))
|
178 |
+
print(f"Applying youth level: {youth_level}")
|
179 |
+
|
180 |
+
# Convert to RGB if in another mode
|
181 |
+
if img.mode != 'RGB':
|
182 |
+
print(f"Converting image from {img.mode} to RGB")
|
183 |
+
img = img.convert('RGB')
|
184 |
|
185 |
# Apply a series of transformations to make the person look younger
|
186 |
|
187 |
# 1. Smooth skin (reduce wrinkles)
|
188 |
smoothing_factor = youth_level / 100
|
189 |
+
print(f"Applying smoothing with factor: {smoothing_factor}")
|
190 |
+
smoothed = img.filter(ImageFilter.GaussianBlur(radius=smoothing_factor * 1.2))
|
191 |
|
192 |
# 2. Enhance brightness slightly (younger skin tends to be brighter)
|
193 |
+
print("Enhancing brightness")
|
194 |
brightness_enhancer = ImageEnhance.Brightness(smoothed)
|
195 |
+
brightened = brightness_enhancer.enhance(1 + (smoothing_factor * 0.15))
|
196 |
|
197 |
# 3. Enhance color (more vibrant)
|
198 |
+
print("Enhancing color")
|
199 |
color_enhancer = ImageEnhance.Color(brightened)
|
200 |
+
colored = color_enhancer.enhance(1 + (smoothing_factor * 0.2))
|
201 |
|
202 |
# 4. Adjust contrast (younger skin has better contrast)
|
203 |
+
print("Adjusting contrast")
|
204 |
contrast_enhancer = ImageEnhance.Contrast(colored)
|
205 |
+
contrasted = contrast_enhancer.enhance(1 + (smoothing_factor * 0.08))
|
206 |
|
207 |
# 5. Sharpen to maintain some details
|
208 |
+
print("Applying sharpening")
|
209 |
sharpened = contrasted.filter(ImageFilter.SHARPEN)
|
210 |
|
211 |
+
print("Image processing completed successfully")
|
212 |
return sharpened, f"Image processed successfully! Youth level applied: {youth_level}%"
|
213 |
|
214 |
except Exception as e:
|
215 |
+
import traceback
|
216 |
+
error_details = traceback.format_exc()
|
217 |
+
print(f"Error in make_younger: {str(e)}\n{error_details}")
|
218 |
return None, f"Error processing image: {str(e)}"
|
219 |
|
220 |
# Function to handle the case when no PDF is found
|
|
|
341 |
gr.Markdown("<p style='text-align: center; color: black;'>Upload an image to make the person look younger.</p>")
|
342 |
|
343 |
with gr.Row():
|
344 |
+
# Use a specific type parameter that's compatible
|
345 |
input_image = gr.Image(label="Upload Image", type="pil")
|
346 |
+
output_image = gr.Image(label="Younger Version", type="pil")
|
347 |
|
348 |
with gr.Row():
|
349 |
youth_slider = gr.Slider(minimum=0, maximum=100, value=50, step=5, label="Youth Level (%)")
|
|
|
350 |
|
351 |
+
process_button = gr.Button("Make Younger")
|
352 |
result_text = gr.Textbox(label="Processing Result", interactive=False)
|
353 |
|
354 |
+
# Add debugging output
|
355 |
+
debug_output = gr.Textbox(label="Debug Info", visible=True)
|
356 |
+
|
357 |
+
def process_with_debug(image, level):
|
358 |
+
if image is None:
|
359 |
+
return None, "No image uploaded.", "Error: No image provided"
|
360 |
+
|
361 |
+
try:
|
362 |
+
debug_info = f"Processing image of type: {type(image)}, youth level: {level}"
|
363 |
+
|
364 |
+
if isinstance(image, dict) and 'image' in image:
|
365 |
+
debug_info += f"\nImage is a dictionary with keys: {list(image.keys())}"
|
366 |
+
image = image['image']
|
367 |
+
debug_info += f"\nExtracted image of type: {type(image)}"
|
368 |
+
|
369 |
+
result_img, result_msg = make_younger(image, level)
|
370 |
+
debug_info += f"\nResult: {result_msg}"
|
371 |
+
return result_img, result_msg, debug_info
|
372 |
+
except Exception as e:
|
373 |
+
import traceback
|
374 |
+
error_details = traceback.format_exc()
|
375 |
+
return None, f"Error: {str(e)}", f"Exception: {str(e)}\n{error_details}"
|
376 |
+
|
377 |
process_button.click(
|
378 |
+
fn=process_with_debug,
|
379 |
inputs=[input_image, youth_slider],
|
380 |
+
outputs=[output_image, result_text, debug_output]
|
381 |
)
|
382 |
|
383 |
# Launch the interface
|