Skip to content

Commit

Permalink
* fix wrongly modified parts in previous rebase
Browse files Browse the repository at this point in the history
* fix lint
  • Loading branch information
ly015 committed Aug 23, 2021
1 parent 1398454 commit e3c00f4
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/top_down_img_demo_with_mmdet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings
from argparse import ArgumentParser

from mmpose.apis import (inference_top_down_pose_model, init_pose_model,
Expand Down
88 changes: 88 additions & 0 deletions mmpose/core/visualization/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,91 @@ def imshow_keypoints_3d(
plt.close(fig)

return img_vis


def imshow_mesh_3d(img,
vertices,
faces,
camera_center,
focal_length,
colors=(76, 76, 204)):
"""Render 3D meshes on background image.
Args:
img(np.ndarray): Background image.
vertices (list of np.ndarray): Vetrex coordinates in camera space.
faces (list of np.ndarray): Faces of meshes.
camera_center ([2]): Center pixel.
focal_length ([2]): Focal length of camera.
colors (list[str or tuple or Color]): A list of mesh colors.
"""

H, W, C = img.shape

if not has_pyrender:
warnings.warn('pyrender package is not installed.')
return img

try:
renderer = pyrender.OffscreenRenderer(
viewport_width=W, viewport_height=H)
except RuntimeError:
warnings.warn('pyrender package is not installed correctly.')
return img

if not isinstance(colors, list):
colors = [colors for _ in range(len(vertices))]
colors = [color_val(c) for c in colors]

depth_map = np.ones([H, W]) * np.inf
output_img = img
for idx in range(len(vertices)):
color = colors[idx]
color = [c / 255.0 for c in color]
color.append(1.0)
vert = vertices[idx]
face = faces[idx]

material = pyrender.MetallicRoughnessMaterial(
metallicFactor=0.2, alphaMode='OPAQUE', baseColorFactor=color)

mesh = trimesh.Trimesh(vert, face)
rot = trimesh.transformations.rotation_matrix(
np.radians(180), [1, 0, 0])
mesh.apply_transform(rot)
mesh = pyrender.Mesh.from_trimesh(mesh, material=material)

scene = pyrender.Scene(ambient_light=(0.5, 0.5, 0.5))
scene.add(mesh, 'mesh')

camera_pose = np.eye(4)
camera = pyrender.IntrinsicsCamera(
fx=focal_length[0],
fy=focal_length[1],
cx=camera_center[0],
cy=camera_center[1],
zfar=1e5)
scene.add(camera, pose=camera_pose)

light = pyrender.DirectionalLight(color=[1.0, 1.0, 1.0], intensity=1)
light_pose = np.eye(4)

light_pose[:3, 3] = np.array([0, -1, 1])
scene.add(light, pose=light_pose)

light_pose[:3, 3] = np.array([0, 1, 1])
scene.add(light, pose=light_pose)

light_pose[:3, 3] = np.array([1, 1, 2])
scene.add(light, pose=light_pose)

color, rend_depth = renderer.render(
scene, flags=pyrender.RenderFlags.RGBA)

valid_mask = (rend_depth < depth_map) * (rend_depth > 0)
depth_map[valid_mask] = rend_depth[valid_mask]
valid_mask = valid_mask[:, :, None]
output_img = (
valid_mask * color[:, :, :3] + (1 - valid_mask) * output_img)

return output_img
7 changes: 3 additions & 4 deletions mmpose/models/detectors/pose_lifter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mmcv
import numpy as np

from mmpose.core import imshow_keypoints, imshow_keypoints_3d
from mmpose.core import imshow_bboxes, imshow_keypoints, imshow_keypoints_3d
from .. import builder
from ..builder import POSENETS
from .base import BasePose
Expand Down Expand Up @@ -259,9 +259,7 @@ def forward_test(self, input, metas, **kwargs):
return results

def forward_dummy(self, input):
"""Used for computing network FLOPs.
See ``tools/get_flops.py``.
"""Used for computing network FLOPs. See ``tools/get_flops.py``.
Args:
input (torch.Tensor): Input pose
Expand Down Expand Up @@ -293,6 +291,7 @@ def show_result(self,
radius=8,
thickness=2,
vis_height=400,
num_instances=-1,
win_name='',
show=False,
wait_time=0,
Expand Down

0 comments on commit e3c00f4

Please sign in to comment.