def render_plot(img, poses, bboxes): renderer = Renderer(vertices_path="./pose_references/vertices_trans.npy", triangles_path="./pose_references/triangles.npy") (w, h) = img.size image_intrinsics = np.array([[w + h, 0, w // 2], [0, w + h, h // 2], [0, 0, 1]]) trans_vertices = renderer.transform_vertices(img, poses) img = renderer.render(img, trans_vertices, alpha=1) # for bbox in bboxes: # bbox = bbox.astype(np.uint8) # print(bbox) # img = cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255,0,0), 2) return img
class Scene: def __init__(self, light_variability=(20,8), gridlines_on=None, gridlines_width=None, gridlines_spacing=None): if gridlines_on or gridlines_width or gridlines_spacing: assert not (gridlines_on is None\ or gridlines_width is None\ or gridlines_spacing is None),\ "All gridlines variables must be set if any are" self.rend = Renderer() self.shapes = [] self.grid_shapes = [] self.center = np.array((0, 140, 300)) self.light_variability = light_variability self.background_prims = [] background_lower_bound = -1e3 background_upper_bound = 1e3 wall_bound = 1e3 self.background_prims.append( Tri([(-wall_bound, 0, wall_bound), (wall_bound, 0, wall_bound), (-wall_bound, 0, -wall_bound)])) self.background_prims.append( Tri([(-wall_bound, 0, -wall_bound), (wall_bound, 0, wall_bound), (wall_bound, 0, -wall_bound)])) self.background_prims.append( Tri([(-wall_bound, -50, wall_bound), (0, wall_bound, wall_bound), (wall_bound, -50, wall_bound)])) if gridlines_on: for i in range(int((background_upper_bound - background_lower_bound) / (gridlines_width + gridlines_spacing))): offset = i * (gridlines_width + gridlines_spacing) self.grid_shapes.append(Tri([(background_lower_bound + offset, 0.01, background_lower_bound), (background_lower_bound + offset, 0.01, background_upper_bound), (background_lower_bound + gridlines_width + offset, 0.01, background_lower_bound)])) self.grid_shapes.append(Tri([(background_lower_bound + offset, 0.01, background_upper_bound), (background_lower_bound + gridlines_width + offset, 0.01, background_upper_bound), (background_lower_bound + gridlines_width + offset, 0.01, background_lower_bound)])) self.grid_shapes.append(Tri([(background_lower_bound, 0.01, background_lower_bound + gridlines_width + offset), (background_upper_bound, 0.01, background_lower_bound + offset), (background_lower_bound, 0.01, background_lower_bound + offset)])) self.grid_shapes.append(Tri([(background_upper_bound, 0.01, background_lower_bound + offset), (background_lower_bound, 0.01, background_lower_bound + gridlines_width + offset), (background_upper_bound, 0.01, background_lower_bound + gridlines_width + offset)])) self.default_light = np.array((400, 300, -800)) self.default_intensity = 1000000 self.camera = Cam((0, 140, 300), (128, 128)) def calc_center(self): return mean([shape.center for shape in self.shapes]) def add_object(self, i=-1): if i<0: i = randint(0, 7) shape = [Sphere(self.center, 0.5), Tetrahedron(self.center), Cuboid(self.center), Cylinder(self.center, 50), Pyramid(self.center), Cone(self.center, 50), Torus(self.center, 0.5, 50, 0.15), HollowCuboid(self.center, 0.15)][i] shape.scale(35) self.shapes.append(shape) def mutate_object(self, shape): shape.scale(randint(25, 40)) self.__rotate_object(shape) self.__translate_object(shape) def mutate_all_objects(self): for shape in self.shapes: self.__scale_object(shape) self.__rotate_object(shape) self.__translate_object(shape) def crossover(self, scene): offspring = Scene() offspring.shapes = self.shapes + scene.shapes shuffle(offspring.shapes) offspring.shapes = offspring.shapes[:len(offspring.shapes)//2] return offspring def mutate(self): if randint(0,1) == 0: self.add_object() else: shape = self.shapes[randint(0, len(self.shapes) - 1)] mutation = [self.__scale_object, self.__translate_object, self.__rotate_object][randint(0,2)] mutation(shape) def new_light(self, theta = 60, phi=8): d = norm(self.default_light - self.center) x_trans = d * math.sin(math.radians(theta)) z_trans = d * math.cos(math.radians(theta)) y_trans = d * math.sin(math.radians(phi)) translation = np.array((x_trans, y_trans, -z_trans)) return Lit(self.center + translation, self.default_intensity) def __scale_object(self, shape): for i in range(3): shape.scale(uniform(0.8, 1.2), axis=i) def __translate_object(self, shape): shape.translate((randint(-50, 50), 0, randint(-50, 50))) def ground_mesh(self): for shape in self.shapes: lowest_y = shape.lowest_y() shape.translate((0, -lowest_y, 0)) def __rotate_object(self, shape): shape.rotate(randint(0, 359), randint(0, 359), randint(0, 359)) def refocus_camera(self): self.camera.location = self.calc_center() def render(self): surface_prims = [] light = self.new_light(*self.light_variability)#Lit(self.default_light, self.default_intensity)# for shape in self.shapes: surface_prims += shape.render() views = [self.camera.view_from(-30, 0, 200)] res_x, res_y = self.camera.resolution return self.rend.render(views, light, surface_prims, self.background_prims, res_x, res_y, self.grid_shapes, grid_color=(0.7,0.7,0.7))
plt.gca().invert_yaxis() subplot_count += 1 # plot SMPL predicted verts plt.subplot(1, 3, subplot_count) plt.scatter(pred_vertices_smpl[:, 0], pred_vertices_smpl[:, 1], s=0.6) plt.gca().set_aspect('equal', adjustable='box') plt.gca().invert_yaxis() subplot_count += 1 plt.savefig(outfile + "_verts_plot.png", bbox_inches='tight') # plt.show() # Render non-parametric shape img_gcnn = renderer.render(pred_vertices, mesh.faces.cpu().numpy(), camera_t=camera_translation, img=img, use_bg=True, body_color='pink') # Render parametric shape img_smpl = renderer.render(pred_vertices_smpl, mesh.faces.cpu().numpy(), camera_t=camera_translation, img=img, use_bg=True, body_color='light_blue') # Render side views aroundy = cv2.Rodrigues(np.array([0, np.radians(90.), 0]))[0] center = pred_vertices.mean(axis=0) center_smpl = pred_vertices.mean(axis=0)
from utils.renderer import Renderer, Cam, Lit, Tri import cv2 rend = Renderer() sp = Sphere((0, 0, 0), 25) shapes = sp.render() cuboid = Cuboid((0, 0, -100)) cuboid.scale(25, axis=0) cuboid.scale(50, axis=1) cuboid.scale(75, axis=2) cuboid.rotate(90, 90) shapes.extend(cuboid.render()) tetrahedron = Tetrahedron((100, 0, 0)) tetrahedron.scale(25, axis=0) tetrahedron.scale(50, axis=1) tetrahedron.scale(20, axis=2) tetrahedron.rotate(45, 180) shapes.extend(tetrahedron.render()) background_prims = [] background_prims.append(Tri([(-1000.00,-40.00,1000.00), (1000.00,-40.00, 1000.00), (-1000.00,-40.00,-1000.00)])) background_prims.append(Tri([(-1000.00,-40.00,-1000.00), (1000.00,-40.00, 1000.00), (1000.00,-40.00,-1000.00)])) light = Lit((125,300,35),79000) camera = [Cam((200,222,83),( -.5,-.7,-.5), (640,480))] shadow, noshadow = rend.render(camera, light, shapes, background_prims) cv2.imwrite("shadow.png", shadow) cv2.imwrite("noshadow.png", noshadow)
# Preprocess input image and generate predictions img, norm_img = process_image(args.img, args.bbox, args.openpose, input_res=cfg.INPUT_RES) with torch.no_grad(): out_dict = model(norm_img.to(model.device)) pred_vertices = out_dict['pred_vertices'] pred_camera = out_dict['camera'] # Calculate camera parameters for rendering camera_translation = torch.stack([pred_camera[:,1], pred_camera[:,2], 2*cfg.FOCAL_LENGTH/(cfg.INPUT_RES * pred_camera[:,0] +1e-9)],dim=-1) camera_translation = camera_translation[0].cpu().numpy() pred_vertices = pred_vertices[0].cpu().numpy() img = img.permute(1,2,0).cpu().numpy() # Render non-parametric shape img_render = renderer.render(pred_vertices, camera_t=camera_translation, img=img, use_bg=True, body_color='pink') # Render side views aroundy = cv2.Rodrigues(np.array([0, np.radians(90.), 0]))[0] center = pred_vertices.mean(axis=0) center_smpl = pred_vertices.mean(axis=0) rot_vertices = np.dot((pred_vertices - center), aroundy) + center # Render non-parametric shape img_render_side = renderer.render(rot_vertices, camera_t=camera_translation, img=np.ones_like(img), use_bg=True, body_color='pink') # Render parametric shape outfile = args.img.split('.')[0] if args.outfile is None else args.outfile