def retargetJointLocation(cls, loc): L.info('Retarget the joint location to fit the human model') newLoc = {} newLoc['root'] = loc['root'] # Start from here # Do normalization for each bone refArmature = Retarget.getReferenceArmature() for bone in Retarget.bones: boneLength = refArmature[bone] joint1 = Retarget.posepriorJointMapping[bone][ 0] # TODO: change this ugly code later joint2 = Retarget.posepriorJointMapping[bone][1] src = loc[joint1] tgt = loc[joint2] vec = tgt - src # print('%.2f' % vec.length) vec.normalize() newSrc = newLoc[joint1] newTgt = newSrc + vec * boneLength newLoc[joint2] = newTgt return newLoc
def apply(self): ''' Apply constraint to connect bone and empty control objects ''' self.constraintName = '%s_%s' % (self.type, self.target) model = models.humanModel() L.info('Setting up constraint %s, %s' % (self.boneName, self.target)) bone = model.pose.bones.get(self.boneName) if not bone: L.error('The bone %s can not be found' % self.boneName) c = bone.constraints.get(self.constraintName) if c: L.info('Constraint already exist') else: c = bone.constraints.new(self.type) targetObject = bpy.data.objects.get(self.target) if not targetObject: L.error('The target object %s can not be found' % self.target) c.target = targetObject for k in self.parameters: L.debug('Setup parameter %s:%s' % (k, self.parameters[k])) if k not in dir(c): L.error('Parameter %s is not valid' % k) else: setattr(c, k, self.parameters[k])
def addTail(cls, loc): # Use this to add a tail bone to structure # Create a tail for rotation control # vec1 = loc['shoulder.l'] - loc['shoulder.r'] L.info('Add the tail position to the armature') vec1 = loc['shoulder.r'] - loc['shoulder.l'] vec2 = loc['neck'] - loc['root'] vec = vec1.cross(vec2) # It is not necessary to check the tail orientation, since we know the left and right of the joints # orintationVec = [ # loc['hip.r'] - loc['root'], # loc['hip.l'] - loc['root'], # # loc['knee.l'] - loc['hip.l'], # # loc['knee.r'] - loc['hip.r'] # loc['foot.l'] - loc['ankle.l'], # loc['foot.r'] - loc['ankle.r'] # ] # sameDirection = [dot(v, vec)/(v.length * vec.length) for v in orintationVec] # if sum(sameDirection) > 0: # vec = -vec vec.normalize() loc['tail'] = loc['root'] + 5 * vec
def main(): import random import tenon.logging as L # L.fileLevel(L.DEBUG) # default is ERROR cachedir = os.path.abspath( os.path.join(rootdir, 'cache/examples/random_light')) # Set up the light configuration radius = 12 nLight = 16 z = 10 lamps = [tenon.obj.Lamp.create('light%d' % v) for v in range(10)] for j in range(10): for i in range(len(lamps)): lamp = lamps[i] # Compute the location of light source, put the light evenly lampObj = tenon.obj.get(lamp.name) lampObj.location = tenon.util.sphereLocation( radius, 360 / nLight * i, 0) lampObj.location[2] += z # Set the z of the light source lamp.energy = random.gauss(1, 1.5) filename = os.path.join(cachedir, 'random_lighting_%d.png' % j) L.info('Rendered file: %s', filename) tenon.render.write(filename)
def retargetJointLocation(cls, loc): L.info('Retarget the joint location to fit the human model') newLoc = {} newLoc['root'] = loc['root'] # Start from here # Do normalization for each bone refArmature = Retarget.getReferenceArmature() for bone in Retarget.bones: boneLength = refArmature[bone] joint1 = Retarget.posepriorJointMapping[bone][0] # TODO: change this ugly code later joint2 = Retarget.posepriorJointMapping[bone][1] src = loc[joint1] tgt = loc[joint2] vec = tgt - src # print('%.2f' % vec.length) vec.normalize() newSrc = newLoc[joint1] newTgt = newSrc + vec * boneLength newLoc[joint2] = newTgt return newLoc
def readJointLocCSV(cls, csvFile, retarget=True): ''' Return a dictionary containing location for each joint ''' csvFile = os.path.expanduser(csvFile) if not os.path.isfile(csvFile): L.error('Joint file %s does not exist.' % csvFile) return None L.info('Read joint information from csv file %s' % csvFile) px = [] py = [] pz = [] jointNames = [] with open(csvFile) as f: headline = f.readline() assert (headline.strip().lower() == 'name,x,y,z') line = f.readline() while line: [name, x, y, z] = line.strip().split(',') px.append(float(x)) py.append(float(y)) pz.append(float(z)) jointNames.append(name) line = f.readline() # Load joint location from csv file. loc = {} for i in range(len(px)): jointName = jointNames[i] # Swap y an z axis x = px[i] - px[0] y = pz[i] - pz[0] z = py[i] - py[0] # swap y and z z = -z loc[jointName] = mathutils.Vector((x, y, z)) # Add tail Retarget.addTail(loc) # Move the shoulder down # Do this before length normalization, because this operation can affect bone length vec = loc['neck'] - loc['root'] offset = vec * 0.32 # offset = -vec * 0 # offJoints = ['shoulder.l', 'shoulder.r', 'elbow.l', 'elbow.r', 'wrist.l', 'wrist.r'] offJoints = ['neck', 'headTop'] for j in offJoints: loc[j] += offset # Make the bone length fit the human model if retarget: loc = Retarget.retargetJointLocation(loc) return loc
def readJointLocCSV(cls, csvFile, retarget=True): ''' Return a dictionary containing location for each joint ''' csvFile = os.path.expanduser(csvFile) if not os.path.isfile(csvFile): L.error('Joint file %s does not exist.' % csvFile) return None L.info('Read joint information from csv file %s' % csvFile) px = []; py = []; pz = []; jointNames = [] with open(csvFile) as f: headline = f.readline() assert(headline.strip().lower() == 'name,x,y,z') line = f.readline() while line: [name,x,y,z] = line.strip().split(',') px.append(float(x)) py.append(float(y)) pz.append(float(z)) jointNames.append(name) line = f.readline() # Load joint location from csv file. loc = {} for i in range(len(px)): jointName = jointNames[i] # Swap y an z axis x = px[i] - px[0] y = pz[i] - pz[0] z = py[i] - py[0] # swap y and z z = -z loc[jointName] = mathutils.Vector((x, y, z)) # Add tail Retarget.addTail(loc) # Move the shoulder down # Do this before length normalization, because this operation can affect bone length vec = loc['neck'] - loc['root'] offset = vec * 0.32 # offset = -vec * 0 # offJoints = ['shoulder.l', 'shoulder.r', 'elbow.l', 'elbow.r', 'wrist.l', 'wrist.r'] offJoints = ['neck', 'headTop'] for j in offJoints: loc[j] += offset # Make the bone length fit the human model if retarget: loc = Retarget.retargetJointLocation(loc) return loc
def run(taskfile, scenefile): if not tenon.inblender(): import tenon.logging as L blender = tenon.setting.blender # cmd = '%s %s --background --python %s > /dev/null 2>&1' % (blender, scenefile, taskfile) cmd = '%s %s --background --python %s >> blender_stdout.log' % (blender, scenefile, taskfile) # Redirect the output of blender. The default output is not very useful L.info('Start tenon on scene file %s', 'file://%s' % os.path.abspath(scenefile)) print(cmd) os.system(cmd)
def main(): import tenon.logging as L import bpy camera = tenon.obj.get('Camera') # check demo.blend to get the camera name for i in range(10): camera.location.x += 1 # L.info(camera.location) figname = './data/demo/%04d.png' % i tenon.render.write(figname) # TODO: Generate html doc from source code L.info('Write to %s successful' % figname)
def check_texture(): import bpy, os texs = bpy.data.textures def check_exist(file): return os.path.isfile(file) for t in texs: L.info('Check texture {}'.format(t)) if isinstance(t, bpy.types.ImageTexture) and t.image.source == 'FILE': filepath = bpy.path.abspath(t.image.filepath) exist = check_exist(filepath) L.info('File {} exist {}'.format(t.image.filepath, exist))
def postprocess_img(i, az): import matplotlib.pyplot as plt import numpy as np import tenon.logging as L filename_no_ext = '%04d_az%d' % (i, az) joint_file = os.path.join(output_dir, 'full', 'joints', '%s.csv' % filename_no_ext) fullimg_filename = os.path.join(output_dir, 'full', 'imgs', '%s.png' % filename_no_ext) # cropped_filename = os.path.join(output_dir, 'cropped', '%s.png' % filename_no_ext) depth_filename = os.path.join(output_dir, 'full', 'depth', '%s.png' % filename_no_ext) X = []; Y = []; labels = [] with open(joint_file) as f: for l in f: [label, x, y] = l.strip().split(',') X.append(int(x)) Y.append(int(y)) labels.append(label) L.debug('label %s, x: %s, y: %s', label, x, y) # Get scale and translation parameters depth = plt.imread(depth_filename); depth = depth[:,:,0] fullimg = plt.imread(fullimg_filename) orig_coords = np.array([X, Y]).T cropper = lsppose.Cropper() [cropped_img, cropped_coords] = cropper.crop_img(fullimg, depth, orig_coords, labels) combine_img = cropper.add_bg(cropped_img) L.info('Crop file %s', fullimg_filename) if L.isDebug() and False: plt.subplot(1,2,1) plt.imshow(cropped_img) plt.subplot(1,2,2) plt.imshow(combine_img) plt.show() cropped_filename = os.path.join(output_dir, 'crop', 'nobg', '%s.png' % filename_no_ext) cropped_filename_bg = os.path.join(output_dir, 'crop', 'imgs', '%s.png' % filename_no_ext) cropped_joints = os.path.join(output_dir, 'crop', 'joints', '%s.csv' % filename_no_ext) plt.imsave(cropped_filename, cropped_img) plt.imsave(cropped_filename_bg, combine_img) L.info('Cropped image %s\nCropped joint info %s', L.prettify_filename(cropped_filename_bg), \ L.prettify_filename(cropped_joints)) L.debug('Shape of cropped_coords %s', cropped_coords.shape) with open(cropped_joints, 'w') as f: for i in range(cropped_coords.shape[0]): f.write('%.2f,%.2f\n' % (cropped_coords[i,0], cropped_coords[i,1]))
def render_scene(self, output_dir, filename_no_ext): imgfilename = os.path.join(output_dir, 'imgs/%s.png' % filename_no_ext) L.info('Render file to %s', L.prettify_filename(imgfilename)) tenon.render.write(imgfilename) depth_filename = os.path.join(output_dir, 'depth/%s.png' % filename_no_ext) tenon.render.DepthMode.enable() tenon.render.write(depth_filename) tenon.render.DepthMode.disable() paint_filename = os.path.join(output_dir, 'parts/%s.png' % filename_no_ext) tenon.render.PaintMode.enable(models.humanModel()) tenon.render.write(paint_filename) # Also save the joint annotation and part annotation joint_filename = os.path.join(output_dir, 'joints/%s.csv' % filename_no_ext) joints = JointInfo.export() JointInfo.serializeJointInfo(joint_filename, joints)
def blender(): lsppose_ = lsppose.Util() lsppose_.rootdir = rootdir import tenon.util as U L.info('Switch logging level to INFO') camera = tenon.obj.get('Camera') # Unused in this demo scene = lsppose_.setup_scene() radius = camera.location.length # Keep the radius fixed el = 0 for i in range(143, 2001): lsppose_.update_scene(scene, i) for az in range(0, 360, 90): loc = U.sphere_location(radius, az, el) camera.location = loc filename_no_ext = '%04d_az%d' % (i, az) lsppose_.render_scene(os.path.join(output_dir, 'full'), filename_no_ext)
def main(): util = lsppose.Util() models = lsppose.Models() util.rootdir = rootdir import tenon.logging as L # outputdir = '//../cache/lsp_synthesized' L.setLevel(tenon.logging.INFO) L.info('Switch logging level to INFO') tenon.render.write('init.png') camera = tenon.obj.get('Camera') # Unused in this demo scene = util.setup_scene() objs = [ # models.humanModel(), models.bodyMesh(), models.upperCloth(), models.lowerCloth(), models.hair(), models.eye() ] schedule_idx = range(1, 20001) schedule_idx = [73, 2] for i in schedule_idx: util.update_scene(scene, i) imgfilename = os.path.join(outputdir, 'imgs/%04d.png' % i) tenon.render.write(imgfilename) L.info('Synthetic image: %s' % L.prettify_filename(imgfilename)) depth_filename = os.path.join(outputdir, 'depth/%04d.png' % i) tenon.render.DepthMode.enable() tenon.render.write(depth_filename) L.info('Depth: %s' % L.prettify_filename(depth_filename)) tenon.render.DepthMode.disable() paint_filename = os.path.join(outputdir, 'parts/%04d.png' % i) for obj in objs: tenon.render.PaintMode.enable(obj) tenon.render.write(paint_filename) L.info('Semantic parts: %s' % L.prettify_filename(paint_filename)) for obj in objs: tenon.render.PaintMode.disable(obj) # Also save the joint annotation and part annotation joint_filename = os.path.join(outputdir, 'joints/%04d.csv' % i) joints = lsppose.JointInfo.export() lsppose.JointInfo.serializeJointInfo(joint_filename, joints)
def main(): import random import tenon.logging as L # L.fileLevel(L.DEBUG) # default is ERROR cachedir = os.path.abspath(os.path.join(rootdir, 'cache/examples/random_light')) # Set up the light configuration radius = 12 nLight = 16 z = 10 lamps = [tenon.obj.Lamp.create('light%d' % v) for v in range(10)] for j in range(10): for i in range(len(lamps)): lamp = lamps[i] # Compute the location of light source, put the light evenly lampObj = tenon.obj.get(lamp.name) lampObj.location = tenon.util.sphereLocation(radius, 360 / nLight * i, 0) lampObj.location[2] += z # Set the z of the light source lamp.energy = random.gauss(1, 1.5) filename = os.path.join(cachedir, 'random_lighting_%d.png' % j) L.info('Rendered file: %s', filename) tenon.render.write(filename)
import sys, os rootdir = os.path.expanduser('~/Dropbox/workspace/graphics_for_vision/tenon') tenonpath = os.path.join(rootdir, 'code/tenon') pwd = os.path.join(rootdir, 'code/tenon/examples/demo_lsp') for v in [tenonpath, pwd]: sys.path.append(os.path.expanduser(v)) import tenon import lsppose # output_dir = os.path.join(rootdir, 'cache/lsp_synthesized/circular_demo') output_dir = os.path.expanduser('~/nosync/circular_demo/') import tenon.logging as L L.setLevel(L.INFO) # L.setLevel(L.DEBUG) L.info('Output folder is %s', L.prettify_filename(output_dir)) def blender(): lsppose_ = lsppose.Util() lsppose_.rootdir = rootdir import tenon.util as U L.info('Switch logging level to INFO') camera = tenon.obj.get('Camera') # Unused in this demo scene = lsppose_.setup_scene() radius = camera.location.length # Keep the radius fixed el = 0
def main(): util = lsppose.Util() models = lsppose.Models() util.rootdir = rootdir import tenon.logging as L # outputdir = '//../cache/lsp_synthesized' L.setLevel(tenon.logging.INFO) L.info('Switch logging level to INFO') tenon.render.write('init.png') camera = tenon.obj.get('Camera') # Unused in this demo scene = util.setup_scene() objs = [ # models.humanModel(), models.bodyMesh(), models.upperCloth(), models.lowerCloth(), models.hair(), models.eye() ] schedule_idx = range(1, 20001) schedule_idx = [2] for i in schedule_idx: util.update_scene(scene, i) # imgfilename = os.path.join(outputdir, 'imgs/%04d.png' % i) # tenon.render.write(imgfilename) # L.info('Synthetic image: %s' % L.prettify_filename(imgfilename)) # depth_filename = os.path.join(outputdir, 'depth/%04d.png' % i) # tenon.render.DepthMode.enable() # tenon.render.write(depth_filename) # L.info('Depth: %s' % L.prettify_filename(depth_filename)) # tenon.render.DepthMode.disable() # paint_filename = os.path.join(outputdir, 'parts/%04d.png' % i) # for obj in objs: # tenon.render.PaintMode.enable(obj) # tenon.render.write(paint_filename) # L.info('Semantic parts: %s' % L.prettify_filename(paint_filename)) # for obj in objs: # tenon.render.PaintMode.disable(obj) # # Also save the joint annotation and part annotation # joint_filename = os.path.join(outputdir, 'joints/%04d.csv' % i) # joints = lsppose.JointInfo.export() # lsppose.JointInfo.serializeJointInfo(joint_filename, joints) import tenon.util as U radius = camera.location.length # Keep the radius fixed el = 0 # for angle in [0, 90, 180, 270]: # for angle in range(0, 360, 10): for angle in [240]: for el in range(0, 90, 10): loc = U.sphere_location(radius, angle, el) camera.location = loc imgfilename = os.path.join(outputdir, 'imgs/%04d_%d_%d.png' % (i, angle, el)) tenon.render.write(imgfilename) L.info('Synthetic image: %s' % L.prettify_filename(imgfilename))
def main(): out_folders = { 'image': './lsp/images', 'depth': './lsp/depth', 'semantic': './lsp/semantic', '2dpose': './lsp/2dpose' } for path in out_folders.values(): if not os.path.exists(path): os.makedirs(path) util = lsppose.Util() models = lsppose.Models() L.setLevel(tenon.logging.INFO) L.info('Switch logging level to INFO') L.info('Write a test image to init.png') tenon.render.write('lsp/init.png') camera = tenon.obj.get('Camera') # Unused in this demo scene = util.setup_scene() objs = [ # models.humanModel(), models.bodyMesh(), models.upperCloth(), models.lowerCloth(), models.hair(), models.eye() ] schedule_idx = range(1, 2001) schedule_idx = [73] for pose_id in schedule_idx: posefolder = '../data/2015101415_v2' util.update_scene(posefolder, scene, pose_id) imgfilename = os.path.join(out_folders['image'], '%04d.png' % pose_id) tenon.render.write(imgfilename) L.info('Synthetic image: %s' % L.prettify_filename(imgfilename)) depth_filename = os.path.join(out_folders['depth'], '%04d.png' % pose_id) tenon.render.DepthMode.enable() tenon.render.write(depth_filename) L.info('Depth: %s' % L.prettify_filename(depth_filename)) tenon.render.DepthMode.disable() paint_filename = os.path.join(out_folders['semantic'], '%04d.png' % pose_id) for obj in objs: tenon.render.PaintMode.enable(obj) tenon.render.write(paint_filename) L.info('Semantic parts: %s' % L.prettify_filename(paint_filename)) for obj in objs: tenon.render.PaintMode.disable(obj) # Also save the joint annotation and part annotation joint_filename = os.path.join(out_folders['2dpose'], '%04d.csv' % pose_id) joints = lsppose.JointInfo.export() lsppose.JointInfo.serializeJointInfo(joint_filename, joints) L.info('2D joint location: %s' % L.prettify_filename(joint_filename))