Spaces:
Running
Running
zhang-ziang
commited on
Commit
•
864becb
1
Parent(s):
d44e357
image post resize and light refine
Browse files- app.py +0 -10
- render/core.py +9 -5
- utils.py +28 -14
app.py
CHANGED
@@ -5,8 +5,6 @@ from vision_tower import DINOv2_MLP
|
|
5 |
from transformers import AutoImageProcessor
|
6 |
import torch
|
7 |
import os
|
8 |
-
import matplotlib.pyplot as plt
|
9 |
-
import io
|
10 |
from PIL import Image
|
11 |
|
12 |
import torch.nn.functional as F
|
@@ -78,14 +76,6 @@ def get_3angle_infer_aug(origin_img, rm_bkg_img):
|
|
78 |
angles[3] = confidence
|
79 |
return angles
|
80 |
|
81 |
-
|
82 |
-
def figure_to_img(fig):
|
83 |
-
with io.BytesIO() as buf:
|
84 |
-
fig.savefig(buf, format='JPG', bbox_inches='tight')
|
85 |
-
buf.seek(0)
|
86 |
-
image = Image.open(buf).copy()
|
87 |
-
return image
|
88 |
-
|
89 |
def infer_func(img, do_rm_bkg, do_infer_aug):
|
90 |
origin_img = Image.fromarray(img)
|
91 |
if do_infer_aug:
|
|
|
5 |
from transformers import AutoImageProcessor
|
6 |
import torch
|
7 |
import os
|
|
|
|
|
8 |
from PIL import Image
|
9 |
|
10 |
import torch.nn.functional as F
|
|
|
76 |
angles[3] = confidence
|
77 |
return angles
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def infer_func(img, do_rm_bkg, do_infer_aug):
|
80 |
origin_img = Image.fromarray(img)
|
81 |
if do_infer_aug:
|
render/core.py
CHANGED
@@ -195,14 +195,18 @@ def dot_product(a: Vec3d, b: Vec3d):
|
|
195 |
def cross_product(a: Vec3d, b: Vec3d):
|
196 |
return Vec3d(*speedup.cross_product(*a.arr, *b.arr))
|
197 |
|
198 |
-
BASE_LIGHT = 0.
|
199 |
def get_light_intensity(face) -> float:
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
203 |
v1, v2, v3 = face
|
204 |
up = normalize(cross_product(v2 - v1, v3 - v1))
|
205 |
-
|
|
|
|
|
|
|
206 |
|
207 |
|
208 |
def look_at(eye: Vec3d, target: Vec3d, up: Vec3d = Vec3d(0, -1, 0)) -> Mat4d:
|
|
|
195 |
def cross_product(a: Vec3d, b: Vec3d):
|
196 |
return Vec3d(*speedup.cross_product(*a.arr, *b.arr))
|
197 |
|
198 |
+
BASE_LIGHT = 0.9
|
199 |
def get_light_intensity(face) -> float:
|
200 |
+
# lights = [Vec3d(-2, 4, -10), Vec3d(10, 4, -2), Vec3d(8, 8, -8), Vec3d(0, 0, -8)]
|
201 |
+
lights = [Vec3d(-2, 4, -10)]
|
202 |
+
# lights = []
|
203 |
+
|
204 |
v1, v2, v3 = face
|
205 |
up = normalize(cross_product(v2 - v1, v3 - v1))
|
206 |
+
intensity = BASE_LIGHT
|
207 |
+
for light in lights:
|
208 |
+
intensity += dot_product(up, normalize(light))*0.2
|
209 |
+
return intensity
|
210 |
|
211 |
|
212 |
def look_at(eye: Vec3d, target: Vec3d, up: Vec3d = Vec3d(0, -1, 0)) -> Mat4d:
|
utils.py
CHANGED
@@ -2,10 +2,11 @@ import rembg
|
|
2 |
import random
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
-
from PIL import Image
|
6 |
import PIL
|
7 |
from typing import Any
|
8 |
import matplotlib.pyplot as plt
|
|
|
9 |
|
10 |
def resize_foreground(
|
11 |
image: Image,
|
@@ -232,8 +233,16 @@ def matplotlib_2D_arrow(angles, rm_bkg_img):
|
|
232 |
ax.set_xlim(-5, 5)
|
233 |
ax.set_ylim(-5, 5)
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
from render import render, Model
|
236 |
import math
|
|
|
237 |
def render_3D_axis(phi, theta, gamma):
|
238 |
radius = 240
|
239 |
# camera_location = [radius * math.cos(phi), radius * math.sin(phi), radius * math.tan(theta)]
|
@@ -241,7 +250,7 @@ def render_3D_axis(phi, theta, gamma):
|
|
241 |
camera_location = [-1*radius * math.cos(phi), -1*radius * math.tan(theta), radius * math.sin(phi)]
|
242 |
img = render(
|
243 |
# Model("res/jinx.obj", texture_filename="res/jinx.tga"),
|
244 |
-
|
245 |
height=512,
|
246 |
width=512,
|
247 |
filename="tmp_render.png",
|
@@ -269,22 +278,27 @@ def overlay_images_with_scaling(center_image: Image.Image, background_image, tar
|
|
269 |
|
270 |
# 缩放背景图像,确保其适合前景图像的尺寸
|
271 |
bg_width, bg_height = background_image.size
|
272 |
-
target_width, target_height = target_size
|
273 |
|
274 |
# 按宽度或高度等比例缩放背景
|
275 |
-
scale =
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
#
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
285 |
|
286 |
# 将前景图像叠加到背景图像上
|
287 |
-
result =
|
288 |
result.paste(center_image, (0, 0), mask=center_image)
|
289 |
|
290 |
return result
|
|
|
2 |
import random
|
3 |
import torch
|
4 |
import numpy as np
|
5 |
+
from PIL import Image, ImageOps
|
6 |
import PIL
|
7 |
from typing import Any
|
8 |
import matplotlib.pyplot as plt
|
9 |
+
import io
|
10 |
|
11 |
def resize_foreground(
|
12 |
image: Image,
|
|
|
233 |
ax.set_xlim(-5, 5)
|
234 |
ax.set_ylim(-5, 5)
|
235 |
|
236 |
+
def figure_to_img(fig):
|
237 |
+
with io.BytesIO() as buf:
|
238 |
+
fig.savefig(buf, format='JPG', bbox_inches='tight')
|
239 |
+
buf.seek(0)
|
240 |
+
image = Image.open(buf).copy()
|
241 |
+
return image
|
242 |
+
|
243 |
from render import render, Model
|
244 |
import math
|
245 |
+
axis_model = Model("./axis.obj", texture_filename="./axis.png")
|
246 |
def render_3D_axis(phi, theta, gamma):
|
247 |
radius = 240
|
248 |
# camera_location = [radius * math.cos(phi), radius * math.sin(phi), radius * math.tan(theta)]
|
|
|
250 |
camera_location = [-1*radius * math.cos(phi), -1*radius * math.tan(theta), radius * math.sin(phi)]
|
251 |
img = render(
|
252 |
# Model("res/jinx.obj", texture_filename="res/jinx.tga"),
|
253 |
+
axis_model,
|
254 |
height=512,
|
255 |
width=512,
|
256 |
filename="tmp_render.png",
|
|
|
278 |
|
279 |
# 缩放背景图像,确保其适合前景图像的尺寸
|
280 |
bg_width, bg_height = background_image.size
|
|
|
281 |
|
282 |
# 按宽度或高度等比例缩放背景
|
283 |
+
scale = target_size[0] / max(bg_width, bg_height)
|
284 |
+
new_width = int(bg_width * scale)
|
285 |
+
new_height = int(bg_height * scale)
|
286 |
+
resized_background = background_image.resize((new_width, new_height))
|
287 |
+
# 计算需要的填充量
|
288 |
+
pad_width = target_size[0] - new_width
|
289 |
+
pad_height = target_size[0] - new_height
|
290 |
+
|
291 |
+
# 计算上下左右的 padding
|
292 |
+
left = pad_width // 2
|
293 |
+
right = pad_width - left
|
294 |
+
top = pad_height // 2
|
295 |
+
bottom = pad_height - top
|
296 |
+
|
297 |
+
# 添加 padding
|
298 |
+
resized_background = ImageOps.expand(resized_background, border=(left, top, right, bottom), fill=(255,255,255,255))
|
299 |
|
300 |
# 将前景图像叠加到背景图像上
|
301 |
+
result = resized_background.copy()
|
302 |
result.paste(center_image, (0, 0), mask=center_image)
|
303 |
|
304 |
return result
|