def save_scene(scene_id): """ Saves the specified scene. """ global scene_info global scene_gt # Collect poses expressed in the world coordinate system ref_obj_poses = [] for obj in bpy.data.objects: if is_obj(obj.name): obj_id = obj.name.split('obj_')[1].split('.')[0] # Get object ID e, t = get_pose(obj.name) R = transform.euler_matrix(e[0], e[1], e[2], axes='sxyz')[:3, :3] ref_obj_poses.append({ 'obj_id': int(obj_id), 'cam_R_m2w': R, 'cam_t_m2w': np.array(t).reshape((3, 1)) }) # Load models of objects present in the scene obj_ids = set([p['obj_id'] for p in ref_obj_poses]) models = {} for obj_id in obj_ids: models[obj_id] = inout.load_ply(par['model_mpath'].format(obj_id)) # Transform the poses to the camera coordinate systems using the known # camera-to-world transformations for im_id in scene_gt.keys(): scene_gt[im_id] = [] K = scene_info[im_id]['cam_K'] R_w2c = scene_info[im_id]['cam_R_w2c'] t_w2c = scene_info[im_id]['cam_t_w2c'] for pose in ref_obj_poses: R_m2c_new = R_w2c.dot(pose['cam_R_m2w']) t_m2c_new = R_w2c.dot(pose['cam_t_m2w']) + t_w2c # Get 2D bounding box of the projection of the object model at # the refined ground truth pose pts_im = misc.project_pts(models[int(obj_id)]['pts'], K, R_m2c_new, t_m2c_new) pts_im = np.round(pts_im).astype(np.int) ys, xs = pts_im[:, 1], pts_im[:, 0] obj_bb = misc.calc_2d_bbox(xs, ys, par['test_im_size']) scene_gt[im_id].append({ 'obj_id': int(obj_id), 'obj_bb': obj_bb, 'cam_R_m2c': R_m2c_new, 'cam_t_m2c': t_m2c_new }) # Save the updated ground truth poses scene_gt_path = par['scene_gt_mpath'].format(scene_id) print('Saving GT poses: ' + scene_gt_path) inout.save_gt(scene_gt_path, scene_gt)
def render_many(self, obj_ids, W, H, K, Rs, ts, near, far, random_light=True, phong={'ambient':0.4,'diffuse':0.8, 'specular':0.3}): assert W <= Renderer.MAX_FBO_WIDTH and H <= Renderer.MAX_FBO_HEIGHT glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glViewport(0, 0, W, H) if random_light: self.set_light_pose( 1000.*np.random.random(3) ) self.set_ambient_light(phong['ambient'] + 0.1*(2*np.random.rand()-1)) self.set_diffuse_light(phong['diffuse'] + 0.1*(2*np.random.rand()-1)) self.set_specular_light(phong['specular'] + 0.1*(2*np.random.rand()-1)) # self.set_ambient_light(phong['ambient']) # self.set_diffuse_light(0.7) # self.set_specular_light(0.3) else: self.set_light_pose( np.array([400., 400., 400]) ) self.set_ambient_light(phong['ambient']) self.set_diffuse_light(phong['diffuse']) self.set_specular_light(phong['specular']) bbs = [] for i in xrange(len(obj_ids)): o = obj_ids[i] R = Rs[i] t = ts[i] camera = gu.Camera() camera.realCamera(W, H, K, R, t, near, far) self._scene_buffer.update(camera.data) self._fbo.bind() glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, ctypes.c_void_p(o*4*5)) self._fbo_depth.bind() glViewport(0, 0, W, H) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, ctypes.c_void_p(o*4*5)) glNamedFramebufferReadBuffer(self._fbo_depth.id, GL_COLOR_ATTACHMENT1) depth_flipped = glReadPixels(0, 0, W, H, GL_RED, GL_FLOAT).reshape(H,W) depth = np.flipud(depth_flipped).copy() ys, xs = np.nonzero(depth > 0) obj_bb = misc.calc_2d_bbox(xs, ys, (W,H)) bbs.append(obj_bb) glBindFramebuffer(GL_FRAMEBUFFER, self._fbo.id) glNamedFramebufferReadBuffer(self._fbo.id, GL_COLOR_ATTACHMENT0) bgr_flipped = np.frombuffer( glReadPixels(0, 0, W, H, GL_BGR, GL_UNSIGNED_BYTE), dtype=np.uint8 ).reshape(H,W,3) bgr = np.flipud(bgr_flipped).copy() glNamedFramebufferReadBuffer(self._fbo.id, GL_COLOR_ATTACHMENT1) depth_flipped = glReadPixels(0, 0, W, H, GL_RED, GL_FLOAT).reshape(H,W) depth = np.flipud(depth_flipped).copy() return bgr, depth, bbs
clip_near, clip_far, texture=model_texture, ambient_weight=ambient_weight, shading=shading, mode='rgb') # The OpenCV function was used for rendering of the training images # provided for the SIXD Challenge 2017. rgb = cv2.resize(rgb, par['cam']['im_size'], interpolation=cv2.INTER_AREA) #rgb = scipy.misc.imresize(rgb, par['cam']['im_size'][::-1], 'bicubic') # Save the rendered images inout.save_im(out_rgb_mpath.format(obj_id, im_id), rgb) inout.save_depth(out_depth_mpath.format(obj_id, im_id), depth) # Get 2D bounding box of the object model at the ground truth pose ys, xs = np.nonzero(depth > 0) obj_bb = misc.calc_2d_bbox(xs, ys, par['cam']['im_size']) obj_info[im_id] = { 'cam_K': par['cam']['K'].flatten().tolist(), 'view_level': int(views_level[view_id]), #'sphere_radius': float(radius) } obj_gt[im_id] = [{ 'cam_R_m2c': view['R'].flatten().tolist(), 'cam_t_m2c': view['t'].flatten().tolist(), 'obj_bb': [int(x) for x in obj_bb], 'obj_id': int(obj_id) }] im_id += 1
# Bounding box of the object mask # bbox_all = [-1, -1, -1, -1] # if px_count_all > 0: # ys, xs = obj_mask_gt.nonzero() # bbox_all = misc.calc_2d_bbox(xs, ys, im_size) # Bounding box of the object projection bbox_obj = misc.calc_pose_2d_bbox(models[gt['obj_id']], im_size, K, gt['cam_R_m2c'], gt['cam_t_m2c']) # Bounding box of the visible surface part bbox_visib = [-1, -1, -1, -1] if px_count_visib > 0: ys, xs = visib_gt.nonzero() bbox_visib = misc.calc_2d_bbox(xs, ys, im_size) gt_stats[im_id].append({ 'px_count_all': int(px_count_all), 'px_count_visib': int(px_count_visib), 'px_count_valid': int(px_count_valid), 'visib_fract': float(visib_fract), 'bbox_obj': [int(e) for e in bbox_obj], 'bbox_visib': [int(e) for e in bbox_visib] }) # mask_below_delta_sum = float(mask_below_delta.sum()) # if mask_below_delta_sum > 0: # visib_to_below_delta_fracs.append({ # 'data_id': data_id, # 'im_id': im_id,
# Bounding box of the object mask # bbox_all = [-1, -1, -1, -1] # if px_count_all > 0: # ys, xs = obj_mask_gt.nonzero() # bbox_all = misc.calc_2d_bbox(xs, ys, im_size) # Bounding box of the object projection bbox_obj = misc.calc_pose_2d_bbox(models[gt['obj_id']], im_size, K, gt['cam_R_m2c'], gt['cam_t_m2c']) # Bounding box of the visible surface part bbox_visib = [-1, -1, -1, -1] if px_count_visib > 0: ys, xs = visib_gt.nonzero() bbox_visib = misc.calc_2d_bbox(xs, ys, im_size) gt_stats[im_id].append({ 'px_count_all': int(px_count_all), 'px_count_visib': int(px_count_visib), 'px_count_valid': int(px_count_valid), 'visib_fract': float(visib_fract), 'bbox_obj': [int(e) for e in bbox_obj], 'bbox_visib': [int(e) for e in bbox_visib] }) # mask_below_delta_sum = float(mask_below_delta.sum()) # if mask_below_delta_sum > 0: # visib_to_below_delta_fracs.append({ # 'data_id': data_id, # 'im_id': im_id,
clip_near, clip_far, texture=model_texture, ambient_weight=ambient_weight, shading=shading, mode='rgb') # The OpenCV function was used for rendering of the training images # provided for the SIXD Challenge 2017. rgb = cv2.resize(rgb, p['cam']['im_size'], interpolation=cv2.INTER_AREA) # rgb = scipy.misc.imresize(rgb, par['cam']['im_size'][::-1], 'bicubic') # Save the rendered images inout.save_im(out_rgb_mpath.format(obj_id, im_id), rgb) inout.save_depth(out_depth_mpath.format(obj_id, im_id), depth) # Get 2D bounding box of the object model at the ground truth pose ys, xs = np.nonzero(depth > 0) obj_bb = misc.calc_2d_bbox(xs, ys, p['cam']['im_size']) obj_info[im_id] = { 'cam_K': p['cam']['K'].flatten().tolist(), 'view_level': int(views_level[view_id]), # 'sphere_radius': float(radius) } obj_gt[im_id] = [{ 'cam_R_m2c': view['R'].flatten().tolist(), 'cam_t_m2c': view['t'].flatten().tolist(), 'obj_bb': [int(x) for x in obj_bb], 'obj_id': int(obj_id) }] im_id += 1