Spaces:
Running
Running
| import gradio as gr | |
| from datetime import datetime | |
| from src.face_morp import morph | |
| from src.utils.align_images import align_images | |
| from src.utils.sort_images import sort_images | |
| def transition(image_files, duration, fps, method, align_resize, order_images, guideline): | |
| time = datetime.now().strftime("%d.%m.%Y_%H.%M.%S") | |
| output_name = f"output_{time}_{fps}fps.mp4" | |
| is_dlib = method == "Dlib" | |
| debug_messages = [] | |
| print(f"Properties: Duration: {duration}, FPS: {fps}, Method: {method}, Align and Resize: {align_resize}, Order Images: {order_images}, Guideline: {guideline}") | |
| try: | |
| # Sort images by age | |
| if order_images: | |
| image_files = sort_images(image_files) | |
| # Align and resize images | |
| if align_resize: | |
| aligned_dir = "aligned_images" | |
| image_files = align_images(image_files, aligned_dir) | |
| morph(image_files, duration, fps, output_name, guideline, is_dlib) | |
| debug_messages.append("Video generation successful") | |
| return output_name, "\n".join(debug_messages) | |
| except Exception as e: | |
| error_message = f"Error: {str(e)}" | |
| print(error_message) | |
| debug_messages.append(error_message) | |
| return None, "\n".join(debug_messages) | |
| if __name__ == "__main__": | |
| gr.Interface( | |
| fn=transition, | |
| inputs=[ | |
| gr.File(file_count="multiple", type="filepath"), | |
| gr.Slider(label="Duration (seconds) between images", minimum=1, maximum=10, step=1, value=3), | |
| gr.Slider(label="Frames per second (fps)", minimum=2, maximum=60, step=1, value=30), | |
| gr.Dropdown(label="Landmarks detection method", choices=["Dlib", "MediaPipe"], value="Dlib"), | |
| gr.Checkbox(label="Align and Resize Images", value=True), | |
| gr.Checkbox(label="Order Images by Age"), | |
| gr.Checkbox(label="Guideline") | |
| ], | |
| outputs=[gr.Video(), gr.Textbox(label="Output Message")], | |
| examples=[ | |
| [["examples/1.png", "examples/2.png", "examples/3.png"], 3, 30, "Dlib", False, False, False], | |
| [["examples/1.png", "examples/2.png", "examples/3.png"], 5, 30, "MediaPipe", False, False, True], | |
| [["examples/1.jpg", "examples/2.jpg", "examples/3.jpg", | |
| "examples/4.jpg", "examples/5.jpg", "examples/6.jpg", "examples/7.jpg", | |
| "examples/8.jpg", "examples/9.jpg", "examples/10.jpg"], 3, 20, "Dlib", True, True, False], | |
| [["examples/big_1.png", "examples/big_2.png", "examples/big_3.png", | |
| "examples/big_4.png", "examples/big_5.png", "examples/big_6.png"], 3, 30, "Dlib", False, False, False] | |
| ], | |
| title="Face Morphing", | |
| description="Upload multiple images containing faces to create a transition video between them." | |
| ).launch(share=False) | |