Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,19 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
parser = argparse.ArgumentParser()
|
| 21 |
-
parser.add_argument("--output_dir", default="out/", type=str, help="Output filepath")
|
| 22 |
-
parser.add_argument("--image_path", default="assets/img1.jpg", type=str, help="Image filepath")
|
| 23 |
-
parser.add_argument("--elevation", default=10, type=float, help="Camera elevation of the input image")
|
| 24 |
-
parser.add_argument("--half_precision", action="store_true", help="Use fp16 half precision")
|
| 25 |
-
parser.add_argument("--seed", default=-1, type=int, help="Random seed")
|
| 26 |
-
args = parser.parse_args()
|
| 27 |
-
|
| 28 |
-
unet = SV3DUNetSpatioTemporalConditionModel.from_pretrained(SV3D_DIFFUSERS, subfolder="unet")
|
| 29 |
-
vae = AutoencoderKL.from_pretrained(SV3D_DIFFUSERS, subfolder="vae")
|
| 30 |
-
scheduler = EulerDiscreteScheduler.from_pretrained(SV3D_DIFFUSERS, subfolder="scheduler")
|
| 31 |
-
image_encoder = CLIPVisionModelWithProjection.from_pretrained(SV3D_DIFFUSERS, subfolder="image_encoder")
|
| 32 |
-
feature_extractor = CLIPImageProcessor.from_pretrained(SV3D_DIFFUSERS, subfolder="feature_extractor")
|
| 33 |
-
|
| 34 |
-
pipeline = StableVideo3DDiffusionPipeline(
|
| 35 |
-
image_encoder=image_encoder, feature_extractor=feature_extractor,
|
| 36 |
-
unet=unet, vae=vae,
|
| 37 |
-
scheduler=scheduler,
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
num_frames, sv3d_res = 21, 576
|
| 41 |
-
elevations_deg = [args.elevation] * num_frames
|
| 42 |
-
polars_rad = [np.deg2rad(90 - e) for e in elevations_deg]
|
| 43 |
-
azimuths_deg = np.linspace(0, 360, num_frames + 1)[1:] % 360
|
| 44 |
-
azimuths_rad = [np.deg2rad((a - azimuths_deg[-1]) % 360) for a in azimuths_deg]
|
| 45 |
-
azimuths_rad[:-1].sort()
|
| 46 |
-
pipeline = pipeline.to("cuda")
|
| 47 |
-
with torch.no_grad():
|
| 48 |
-
with torch.autocast("cuda", dtype=torch.float16 if args.half_precision else torch.float32, enabled=True):
|
| 49 |
-
image = Image.open(args.image_path)
|
| 50 |
-
image.load() # required for `.split()`
|
| 51 |
-
if len(image.split()) == 4: # RGBA
|
| 52 |
-
input_image = Image.new("RGB", image.size, (255, 255, 255)) # pure white bg
|
| 53 |
-
input_image.paste(image, mask=image.split()[3]) # 3rd is the alpha channel
|
| 54 |
-
else:
|
| 55 |
-
input_image = image
|
| 56 |
-
video_frames = pipeline(
|
| 57 |
-
input_image.resize((sv3d_res, sv3d_res)),
|
| 58 |
-
height=sv3d_res,
|
| 59 |
-
width=sv3d_res,
|
| 60 |
-
num_frames=num_frames,
|
| 61 |
-
decode_chunk_size=8, # smaller to save memory
|
| 62 |
-
polars_rad=polars_rad,
|
| 63 |
-
azimuths_rad=azimuths_rad,
|
| 64 |
-
generator=torch.manual_seed(args.seed) if args.seed >= 0 else None,
|
| 65 |
-
).frames[0]
|
| 66 |
-
|
| 67 |
-
os.makedirs(args.output_dir, exist_ok=True)
|
| 68 |
-
name = os.path.basename(args.image_path).split(".")[0]
|
| 69 |
-
export_to_gif(video_frames, f"{args.output_dir}/{name}.gif", fps=7)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if __name__ == "__main__":
|
| 73 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from infer import generate_video
|
| 3 |
+
|
| 4 |
+
def gradio_generate_video(image, elevation=10):
|
| 5 |
+
image_path = image.name # Save the image to a temporary file
|
| 6 |
+
image.save(image_path) # Save uploaded image to disk
|
| 7 |
+
output_gif = generate_video(image_path=image_path, output_dir="out/", elevation=elevation)
|
| 8 |
+
return output_gif # Return the generated video path
|
| 9 |
+
|
| 10 |
+
iface = gr.Interface(
|
| 11 |
+
fn=gradio_generate_video,
|
| 12 |
+
inputs=[
|
| 13 |
+
gr.inputs.Image(type="filepath"),
|
| 14 |
+
gr.inputs.Slider(minimum=0, maximum=90, default=10, label="Camera Elevation"),
|
| 15 |
+
],
|
| 16 |
+
outputs=gr.outputs.Video(),
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|