-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSV3d_recon_img.py
53 lines (48 loc) · 1.58 KB
/
SV3d_recon_img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import math
import os
import sys
from glob import glob
from pathlib import Path
from PIL import Image
import cv2
import numpy as np
import torch
from torchvision.transforms import ToTensor
from rembg import remove
def recon_img(input_img_path, image_frame_ratio):
image = Image.open(input_img_path)
if image.mode == "RGBA":
pass
else:
# remove bg
image.thumbnail([768, 768], Image.Resampling.LANCZOS)
image = remove(image.convert("RGBA"), alpha_matting=True)
# resize object in frame
image_arr = np.array(image)
in_w, in_h = image_arr.shape[:2]
ret, mask = cv2.threshold(
np.array(image.split()[-1]), 0, 255, cv2.THRESH_BINARY
)
x, y, w, h = cv2.boundingRect(mask)
max_size = max(w, h)
side_len = (
int(max_size / image_frame_ratio)
if image_frame_ratio is not None
else in_w
)
padded_image = np.zeros((side_len, side_len, 4), dtype=np.uint8)
center = side_len // 2
padded_image[
center - h // 2 : center - h // 2 + h,
center - w // 2 : center - w // 2 + w,
] = image_arr[y : y + h, x : x + w]
# resize frame to 576x576
rgba = Image.fromarray(padded_image).resize((576, 576), Image.LANCZOS)
# white bg
rgba_arr = np.array(rgba) / 255.0
rgb = rgba_arr[..., :3] * rgba_arr[..., -1:] + (1 - rgba_arr[..., -1:])
input_image = Image.fromarray((rgb * 255).astype(np.uint8))
return input_image
input_img_path = './object_outputs/000-142_1/orbit_frame_0010.png'
output_img = recon_img(input_img_path, None)
output_img.save("recon_out.png", "PNG")