Spaces:
Runtime error
Runtime error
File size: 8,977 Bytes
f670afc 92c3c2a f670afc 92c3c2a f670afc |
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import gradio as gr
import numpy as np
import os
from PIL import Image
import torch
import torchvision.transforms as transforms
import options
import test
import importlib
from scipy.interpolate import interp1d, splev, splprep
import cv2
import subprocess
subprocess.run(["bash", "install_imaginaire.sh"])
def get_single(sat_img, style_img, x_offset, y_offset):
name = ''
for i in [name for name in os.listdir('demo_img') if 'case' in name]:
style = Image.open('demo_img/{}/groundview.image.png'.format(i)).convert('RGB')
style =np.array(style)
if (style == style_img).all():
name = i
break
input_dict = {}
trans = transforms.ToTensor()
input_dict['sat'] = trans(sat_img)
input_dict['pano'] = trans(style_img)
input_dict['paths'] = "demo.png"
sky = trans(Image.open('demo_img/{}/groundview.sky.png'.format(name)).convert("L"))
input_a = input_dict['pano']*sky
sky_histc = torch.cat([input_a[i].histc()[10:] for i in reversed(range(3))])
input_dict['sky_histc'] = sky_histc
input_dict['sky_mask'] = sky
for key in input_dict.keys():
if isinstance(input_dict[key], torch.Tensor):
input_dict[key] = input_dict[key].unsqueeze(0)
args = ["--yaml=sat2density_cvact", "--test_ckpt_path=wandb/run-20230219_141512-2u87bj8w/files/checkpoint/model.pth", "--task=test_vid", "--demo_img=demo_img/case1/satview-input.png",
"--sty_img=demo_img/case1/groundview.image.png", "--save_dir=output"]
opt_cmd = options.parse_arguments(args=args)
opt = options.set(opt_cmd=opt_cmd)
opt.isTrain = False
opt.name = opt.yaml if opt.name is None else opt.name
opt.batch_size = 1
m = importlib.import_module("model.{}".format(opt.model))
model = m.Model(opt)
# m.load_dataset(opt)
model.build_networks(opt)
ckpt = torch.load(opt.test_ckpt_path, map_location='cpu')
model.netG.load_state_dict(ckpt['netG'])
model.netG.eval()
model.set_input(input_dict)
model.style_temp = model.sky_histc
opt.origin_H_W = [-(y_offset*256-128)/128, (x_offset*256-128)/128] # TODO: hard code should be removed in the future
model.forward(opt)
rgb = model.out_put.pred[0].clamp(min=0,max=1.0).cpu().detach().numpy().transpose((1,2,0))
rgb = np.array(rgb*255, dtype=np.uint8)
return rgb
def get_video(sat_img, style_img, positions):
name = ''
for i in [name for name in os.listdir('demo_img') if 'case' in name]:
style = Image.open('demo_img/{}/groundview.image.png'.format(i)).convert('RGB')
style =np.array(style)
if (style == style_img).all():
name = i
break
input_dict = {}
trans = transforms.ToTensor()
input_dict['sat'] = trans(sat_img)
input_dict['pano'] = trans(style_img)
input_dict['paths'] = "demo.png"
sky = trans(Image.open('demo_img/{}/groundview.sky.png'.format(name)).convert("L"))
input_a = input_dict['pano']*sky
sky_histc = torch.cat([input_a[i].histc()[10:] for i in reversed(range(3))])
input_dict['sky_histc'] = sky_histc
input_dict['sky_mask'] = sky
for key in input_dict.keys():
if isinstance(input_dict[key], torch.Tensor):
input_dict[key] = input_dict[key].unsqueeze(0)
args = ["--yaml=sat2density_cvact", "--test_ckpt_path=wandb/run-20230219_141512-2u87bj8w/files/checkpoint/model.pth", "--task=test_vid", "--demo_img=demo_img/case1/satview-input.png",
"--sty_img=demo_img/case1/groundview.image.png", "--save_dir=output"]
opt_cmd = options.parse_arguments(args=args)
opt = options.set(opt_cmd=opt_cmd)
opt.isTrain = False
opt.name = opt.yaml if opt.name is None else opt.name
opt.batch_size = 1
m = importlib.import_module("model.{}".format(opt.model))
model = m.Model(opt)
# m.load_dataset(opt)
model.build_networks(opt)
ckpt = torch.load(opt.test_ckpt_path, map_location='cpu')
model.netG.load_state_dict(ckpt['netG'])
model.netG.eval()
model.set_input(input_dict)
model.style_temp = model.sky_histc
unique_lst = list(dict.fromkeys(positions))
pixels = []
for x in positions:
if x in unique_lst:
if x not in pixels:
pixels.append(x)
pixels = np.array(pixels)
tck, u = splprep(pixels.T, s=25, per=0)
u_new = np.linspace(u.min(), u.max(), 80)
x_new, y_new = splev(u_new, tck)
smooth_path = np.array([x_new,y_new]).T
rendered_image_list = []
rendered_depth_list = []
for i, (x,y) in enumerate(smooth_path):
opt.origin_H_W = [(y-128)/128, (x-128)/128] # TODO: hard code should be removed in the future
print('Rendering at ({}, {})'.format(x,y))
model.forward(opt)
rgb = model.out_put.pred[0].clamp(min=0,max=1.0).cpu().detach().numpy().transpose((1,2,0))
rgb = np.array(rgb*255, dtype=np.uint8)
rendered_image_list.append(rgb)
rendered_depth_list.append(
model.out_put.depth[0,0].cpu().detach().numpy()
)
output_video_path = 'output_video.mp4'
frame_rate = 15
frame_width = 512
frame_height = 128
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_video_path, fourcc, frame_rate, (frame_width, frame_height))
for image_np in rendered_image_list:
image_np = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
out.write(image_np)
out.release()
return "output_video.mp4"
def copy_image(image):
return image
def show_image_and_point(image, x, y):
x = int(x*image.shape[1])
y = image.shape[0]-int(y*image.shape[0])
mask = np.zeros(image.shape[:2])
radius = min(image.shape[0], image.shape[1])//60
for i in range(x-radius-2, x+radius+2):
for j in range(y-radius-2, y+radius+2):
if (i-x)**2+(j-y)**2<=radius**2:
mask[j, i] = 1
return (image, [(mask, 'render point')])
def add_select_point(image, evt: gr.SelectData, state1):
if state1 == None:
state1 = []
x, y = evt.index
state1.append((x, y))
print(state1)
radius = min(image.shape[0], image.shape[1])//60
for i in range(x-radius-2, x+radius+2):
for j in range(y-radius-2, y+radius+2):
if (i-x)**2+(j-y)**2<=radius**2:
image[j, i, :] = 0
return image, state1
def reset_select_points(image):
return image, []
with gr.Blocks() as demo:
gr.Markdown("# Sat2Density Demos")
gr.Markdown("### select/upload the satllite image and select the style image")
with gr.Row():
with gr.Column():
sat_img = gr.Image(source='upload', shape=[256, 256], interactive=True)
img_examples = gr.Examples(examples=['demo_img/{}/satview-input.png'.format(i) for i in os.listdir('demo_img') if 'case' in i],
inputs=sat_img, outputs=None, examples_per_page=20)
with gr.Column():
style_img = gr.Image()
style_examples = gr.Examples(examples=['demo_img/{}/groundview.image.png'.format(i) for i in os.listdir('demo_img') if 'case' in i],
inputs=style_img, outputs=None, examples_per_page=20)
gr.Markdown("### select a certain point to generate single groundview image")
with gr.Row():
with gr.Column():
with gr.Row():
with gr.Column():
slider_x = gr.Slider(0.2, 0.8, 0.5, label="x-axis position")
slider_y = gr.Slider(0.2, 0.8, 0.5, label="y-axis position")
btn_single = gr.Button(label="demo1")
annotation_image = gr.AnnotatedImage()
out_single = gr.Image()
gr.Markdown("### draw a trajectory on the map to generate video")
state_select_points = gr.State()
with gr.Row():
with gr.Column():
draw_img = gr.Image(shape=[256, 256], interactive=True)
with gr.Column():
out_video = gr.Video()
reset_btn =gr.Button(value="Reset")
btn_video = gr.Button(label="demo1")
sat_img.change(copy_image, inputs = sat_img, outputs=draw_img)
draw_img.select(add_select_point, [draw_img, state_select_points], [draw_img, state_select_points])
sat_img.change(show_image_and_point, inputs = [sat_img, slider_x, slider_y], outputs = annotation_image)
slider_x.change(show_image_and_point, inputs = [sat_img, slider_x, slider_y], outputs = annotation_image, show_progress='hidden')
slider_y.change(show_image_and_point, inputs = [sat_img, slider_x, slider_y], outputs = annotation_image, show_progress='hidden')
btn_single.click(get_single, inputs = [sat_img, style_img, slider_x, slider_y], outputs=out_single)
reset_btn.click(reset_select_points, [sat_img], [draw_img, state_select_points])
btn_video.click(get_video, inputs=[sat_img, style_img, state_select_points], outputs=out_video) # 触发
demo.launch() |