|
| 1 | +import gradio as gr |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import time |
| 6 | + |
| 7 | + |
| 8 | +def predict(image): |
| 9 | + # Ensure data directory exists |
| 10 | + os.makedirs("/app/data", exist_ok=True) |
| 11 | + |
| 12 | + input_path = "/app/data/input.jpg" |
| 13 | + |
| 14 | + # Save/Copy input image |
| 15 | + # image provided by gradio (type='filepath') is a temp path |
| 16 | + shutil.copy(image, input_path) |
| 17 | + |
| 18 | + # Run sharp command |
| 19 | + # sharp predict -i /app/data/input.jpg -o /app/data/output --render |
| 20 | + cmd = [ |
| 21 | + "sharp", |
| 22 | + "predict", |
| 23 | + "-i", |
| 24 | + input_path, |
| 25 | + "-o", |
| 26 | + "/app/data/output", |
| 27 | + "--render", |
| 28 | + ] |
| 29 | + |
| 30 | + # Execute command |
| 31 | + try: |
| 32 | + t = time.time() |
| 33 | + print("Sharp started") |
| 34 | + subprocess.run(cmd, check=True, capture_output=True) |
| 35 | + print(f"Sharp command took {round(time.time() - t, 3)} seconds") |
| 36 | + except subprocess.CalledProcessError as e: |
| 37 | + print(f"Error running sharp: {e}") |
| 38 | + print(f"Stdout: {e.stdout.decode()}") |
| 39 | + print(f"Stderr: {e.stderr.decode()}") |
| 40 | + return None |
| 41 | + |
| 42 | + # Find output videos |
| 43 | + rgb_video = "/app/data/output/input.mp4" |
| 44 | + depth_video = "/app/data/output/input.depth.mp4" |
| 45 | + |
| 46 | + if os.path.exists(rgb_video) and os.path.exists(depth_video): |
| 47 | + return rgb_video, depth_video |
| 48 | + elif os.path.exists(rgb_video): |
| 49 | + return rgb_video, None |
| 50 | + |
| 51 | + return None, None |
| 52 | + |
| 53 | + |
| 54 | +demo = gr.Interface( |
| 55 | + fn=predict, |
| 56 | + inputs=gr.Image(type="filepath", label="Input Image"), |
| 57 | + outputs=[gr.Video(label="RGB Video"), gr.Video(label="Depth Video")], |
| 58 | + title="Sharp 3D View Synthesis", |
| 59 | + description="Upload an image to generate a 3D view synthesis video.", |
| 60 | +) |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + print( |
| 64 | + "Sharp Monocular View Synthesis in Less Than a Second (https://github.com/apple/ml-sharp)" |
| 65 | + ) |
| 66 | + demo.launch(server_name="0.0.0.0", server_port=7860) |
0 commit comments