def retrack_remap_rbfn(): grip_dir = os.environ['GRIP_DATA'] movie_fn,_ = QApp.app.loadFilename('Choose a movie to open', grip_dir, 'Movie Files (*.mp4 *.mov *.avi *.flv *.mpg)') txt_fn,_ = QApp.app.loadFilename('Choose a text file of frame indices to open', grip_dir, 'Text Files (*.txt)') md = MovieReader.open_file(movie_fn, audio=False) lines = map(str.strip,(open(txt_fn,'r').readlines())) mapping_file = {} for l in lines: pose_name,frame_number,group_names = l.split(':') for gn in group_names.split(','): mapping_file.setdefault(gn,{})[pose_name] = int(frame_number) print mapping_file.keys() print mapping_file update_rbfn(md, mapping_file=mapping_file)
def dirty_cb(dirty): if '/root/ui/attrs/movie_filename' in dirty: fn = State.getKey('/root/ui/attrs/movie_filename') global g_md g_md = MovieReader.open_file(fn) QApp.app.qtimeline.setRange(0,g_md['vmaxframe']) for dk in dirty: if dk.startswith('/root/ui/attrs/'): QApp.app.refresh() global g_mode, g_frame, g_rbfn if g_mode == 1 and not '/root/sliders/attrs' in dirty: # RBFN view; changing frame sets all the sliders; we avoid that case for key in dirty: if key.startswith('/root/sliders/attrs'): si = g_rbfn['slider_names'].index(key[len('/root/sliders/attrs/'):]) group,gn,pn,slider_indices,slider_names,pose_splits = rbfn_info_from_frame(g_frame[g_mode]) print 'rbfn slider value changed:',key,si,'from',group['slider_data'][pn][si],'to',State.getKey(key) group['slider_data'][pn][si] = State.getKey(key) rbfn_view_cb(g_frame[g_mode]) # TODO, force an update of the geo
def import_movie_frames(): movie_fn, _ = QApp.app.loadFilename( 'Choose a movie to open', cwd(), 'Movie Files (*.mp4 *.mov *.avi *.flv *.mpg)') if movie_fn == '': return # cancel set_cwd(movie_fn) txt_fn, _ = QApp.app.loadFilename( 'Choose a text file of frame indices to open', cwd(), 'Text Files (*.txt)') md = MovieReader.open_file(movie_fn, audio=False) images, shapes = [], [] if txt_fn == '': frames = range(0, md['vmaxframe'], 100) #if txt_fn == '': frames = range(30000, 38300, 100) else: frames = [int(l.split(':')[1]) for l in open(txt_fn, 'r').readlines()] for fi in frames: print fi, '/', frames[-1] MovieReader.readFrame(md, fi) add_image( np.frombuffer(md['vbuffer'], dtype=np.uint8).reshape(md['vheight'], md['vwidth'], 3).copy()) State.push('Import movie frames')
def generateSkeleton(cacheId=622, x3d_filename='', perc=0.9, triangleThreshold=1000, thresholdDistance=25., useFrames=range(0, 514), numComps=30, labelGraphThreshold=4, stepSize=1): directory = os.path.join(os.environ['GRIP_DATA'], '140113_A2_GRIP_GenPeople') c3d_filename = 'ROM.c3d' cameraB = 7292, 34, (47, 2.3, -0.2), (67, 788, 79) cameraA = 5384, 49.8, (5.4, 1.1, -0.7), (4, 1135, 0 ) #5045,52,(5.6,0.9,-0.7),(0,1130,0) camera = cameraA startFrame = 0 tempDir = os.environ['GRIP_TEMP'] #import logging #logging.basicConfig(level=logging.DEBUG) from IO import C3D graph_out_fn = None # labelGraphThreshold = 4 # stepSize = 1 if True: # bo data c = C3D.read(os.path.join(directory, c3d_filename)) c3d_frames, c3d_fps = c['frames'], c['fps'] pointLabels = c['labels'] print 'c3d fps = ', c3d_fps numFramesVisiblePerPoint = np.sum(c3d_frames[:, :, 3] == 0, axis=0) numPointsVisiblePerFrame = np.sum(c3d_frames[:, :, 3] == 0, axis=1) print 'threshold', 0.40 * len(c3d_frames) goodPoints = np.where( numFramesVisiblePerPoint > 0.90 * len(c3d_frames))[0] goodFrames = np.where( np.sum(c3d_frames[:, goodPoints, 3] == 0, axis=1) == len(goodPoints))[0] print len(goodPoints), len( goodFrames) # 290 x 6162 (80%), 283 x 8729 (90%), 275x10054 (96%) frames = c3d_frames[goodFrames, :, :][:, goodPoints, :][:, :, :3] pointLabels = [pointLabels[g] for g in goodPoints] #badPoint = pointLabels.index('BoDense:A_Neck_1') data_fn = 'W90-28-10.IO' skel_out_fn = None triangleThreshold = 1000. else: # orn data # cacheId = 622 # perc = 0.9 # triangleThreshold = 1000. # Bone threshold # thresholdDistance = 25. # Joint threshold # useFrames = range(2370, 3500) # useFrames = range(2650, 3500) # useFrames = range(2600, 3480) # useFrames = range(2650, 3480) # useFrames = range(0, 2000) # useFrames = range(0, 514) #useFrames = [] #range(0, 1000) #useFrames.extend(range(2650, 3480)) #useFrames.extend(range(4824, 5253)) # numComps = 30 # useFrames = range(0, 333) # useFrames = range(4824, 5253) print 'CacheId:', cacheId print 'Good point percentage:', perc print 'Triangle threshold:', triangleThreshold print 'Distance threshold:', thresholdDistance print 'Frames:', useFrames[0], '-', useFrames[-1] _, x3d_data = IO.load(x3d_filename) data_fn = 'W90-28-8.romtracks_T%d.IO' % cacheId location = '/root/tracks' # location = '/root/skeleton/reconstruction/collection/c3ds' c3d_frames = x3d_data[location]['x3ds'] print c3d_frames.shape c3d_frames = np.transpose(c3d_frames, axes=(1, 0, 2)) #frames = frames[:, blueIds, :] print c3d_frames.shape pointLabels = x3d_data[location]['x3ds_labels'] if False: goodPoints = np.arange(c3d_frames.shape[1]) goodFrames = np.arange(len(c3d_frames)) else: numFramesVisiblePerPoint = np.sum(c3d_frames[useFrames, :, 3] == 0, axis=0) numPointsVisiblePerFrame = np.sum(c3d_frames[useFrames, :, 3] == 0, axis=1) goodPoints = np.where( numFramesVisiblePerPoint > perc * len(useFrames))[0] goodFrames = np.where( np.sum(c3d_frames[:, goodPoints, 3] == 0, axis=1) == len(goodPoints))[0] print '# Good points: %d | # Good frames: %d' % (len(goodPoints), len(goodFrames)) print goodFrames[:4] frames = c3d_frames[goodFrames, :, :][:, goodPoints, :][:, :, :3] pointLabels = [int(pointLabels[g]) for g in goodPoints] skel_out_fn = None graph_out_fn = None data = frames[::stepSize, :, :].copy() first_time_only = not os.path.exists(os.path.join(tempDir, data_fn)) if first_time_only: # generate the file M = ASFReader.greedyTriangles( data, numComps, triangleThreshold=triangleThreshold, thresholdDistance=thresholdDistance**2) # only every Nth frame IO.save(os.path.join(tempDir, 'M90_T%d.IO' % cacheId), M) _, M = IO.load(os.path.join(tempDir, 'M90_T%d.IO' % cacheId)) stabilizedPointToGroup, stabilizedPointResiduals, stabilizedFrames = ASFReader.assignAndStabilize( data, M['RTs'][M['triIndices'][:28]], thresholdDistance=thresholdDistance**2) W = { 'stabilizedPointToGroup': stabilizedPointToGroup, 'stabilizedPointResiduals': stabilizedPointResiduals, 'stabilizedFrames': stabilizedFrames } IO.save(os.path.join(tempDir, data_fn), W) else: _data = IO.load(os.path.join(tempDir, data_fn))[1] stabilizedPointToGroup = _data['stabilizedPointToGroup'] stabilizedPointResiduals = _data['stabilizedPointResiduals'] stabilizedFrames = _data['stabilizedFrames'] print 'numFrames = %d' % len(stabilizedFrames) print 'number of labelled points %d' % np.sum(stabilizedPointToGroup != -1) print 'RMS of labelled points %fmm' % np.sqrt( np.mean( stabilizedPointResiduals[np.where(stabilizedPointToGroup != -1)])) first_time_only = True print stabilizedPointToGroup num_groups = max(stabilizedPointToGroup) + 1 stabilized_groups = [ np.where(stabilizedPointToGroup == gi)[0] for gi in range(num_groups) ] if first_time_only: if True: # tighten the fit # thresh = [10,10,9,9] #,10,10,9,7,9,9,6,9,9,9,] thresh = [ thresholdDistance, thresholdDistance, thresholdDistance - 1, thresholdDistance - 1, thresholdDistance - 2, thresholdDistance - 2 ] # thresh = [20, 20, 19, 19, 10, 10, 9, 9] for t in thresh: #stabilizedPointToGroup[badPoint] = -1 # unlabel RTs = ASFReader.stabilizeAssignment(data, stabilizedPointToGroup) stabilizedPointToGroup, stabilizedPointResiduals, stabilizedFrames = ASFReader.assignAndStabilize( data, RTs, thresholdDistance=float(t)**2) print 'number of labelled points %d' % np.sum( stabilizedPointToGroup != -1) print 'RMS of labelled points %fmm' % np.sqrt( np.mean(stabilizedPointResiduals[np.where( stabilizedPointToGroup != -1)])) else: RTs = ASFReader.stabilizeAssignment(data, stabilizedPointToGroup) stabilizedPointToGroup, stabilizedPointResiduals, stabilizedFrames = ASFReader.assignAndStabilize( data, RTs, thresholdDistance=10.**2) global animJoints, stablePointsGroups, displayFrames, groupRepresentatives stablePointsData = ASFReader.sharedStablePoints(RTs, threshold=3.**2) stablePointsGroups = [sp[0] for sp in stablePointsData] stablePoints = np.array([sp[2] for sp in stablePointsData], dtype=np.float32) print 'num stable points', len(stablePoints) def residual(gi, leaf_indices, RTs): '''given a group and a list of attachment points, choose the best attachment point and return the residual.''' tmp = [(ASFReader.transform_pair_residual(RTs[gi], RTs[gj]), gj) for gj in leaf_indices] return min(tmp) # make a skeleton from stabilizedPointToGroup root_group = 0 leaf_nodes = set([root_group]) skel_group_indices = [root_group] skel_joint_parents = [-1] groups = set(range(stabilizedPointToGroup.max() + 1)) groups.remove(root_group) RTs = ASFReader.stabilizeAssignment(data, stabilizedPointToGroup) joints = [] joints.append(np.mean(data[0, stabilized_groups[root_group]], axis=0)) bones = [] bones.append([]) G = np.eye(3, 4, dtype=np.float32) G[:, 3] = np.mean(data[0, stabilized_groups[root_group]], axis=0) Gs = [G] while groups: residuals = [(residual(gi, leaf_nodes, RTs), gi) for gi in groups] (((res, O), parent), group) = min(residuals) groups.remove(group) leaf_nodes.add(group) skel_group_indices.append(group) pi = skel_group_indices.index(parent) skel_joint_parents.append(pi) joint_world = np.float32(O) joints.append(joint_world) bones.append([np.mean(data[0, stabilized_groups[group]], axis=0) - O]) bones[pi].append(joint_world - joints[pi]) print group, parent G = np.eye(3, 4, dtype=np.float32) G[:, 3] = O Gs.append(G) print skel_group_indices print skel_joint_parents numJoints = len(skel_joint_parents) jointNames = map(str, skel_group_indices) jointIndex = dict(zip(jointNames, range(len(jointNames)))) jointParents = skel_joint_parents jointChans = [0, 1, 2] + [3, 4, 5] * numJoints jointChanSplits = [0, 3, 6] for x in range(numJoints - 1): jointChanSplits.append(jointChanSplits[-1]) jointChanSplits.append(jointChanSplits[-1] + 3) dofNames = [ jointNames[ji] + [':tx', ':ty', ':tz', ':rx', ':ry', ':rz'][jointChans[di]] for ji in range(numJoints) for di in range(jointChanSplits[2 * ji], jointChanSplits[2 * ji + 2]) ] numDofs = len(dofNames) def mult_inv(Gs_pi, Gs_gi): # Gs_pi^-1 Gs_gi = Ls_gi R = np.linalg.inv(Gs_pi[:3, :3]) ret = np.dot(R, Gs_gi) ret[:, 3] -= np.dot(R, Gs_pi[:, 3]) return ret Ls = np.float32([ mult_inv(Gs[pi], Gs[gi]) if pi != -1 else Gs[gi] for gi, pi in enumerate(skel_joint_parents) ]) Bs = bones print map(len, Bs) markerParents = [ skel_group_indices.index(gi) for gi in stabilizedPointToGroup if gi != -1 ] markerNames = [('%d' % pi) for pi, gi in enumerate(stabilizedPointToGroup) if gi != -1] labelNames = [('%d' % pointLabels[pi]) for pi, gi in enumerate(stabilizedPointToGroup) if gi != -1] markerOffsets = [ np.dot(Gs[skel_group_indices.index(gi)][:3, :3].T, data[0][pi] - Gs[skel_group_indices.index(gi)][:3, 3]) for pi, gi in enumerate(stabilizedPointToGroup) if gi != -1 ] skel_dict = { 'name': 'skeleton', 'numJoints': int(numJoints), 'jointNames': jointNames, # list of strings 'jointIndex': jointIndex, # dict of string:int 'jointParents': np.int32(jointParents), 'jointChans': np.int32(jointChans), # 0 to 5 : tx,ty,tz,rx,ry,rz 'jointChanSplits': np.int32(jointChanSplits), 'chanNames': dofNames, # list of strings 'chanValues': np.zeros(numDofs, dtype=np.float32), 'numChans': int(numDofs), 'Bs': Bs, 'Ls': np.float32(Ls), 'Gs': np.float32(Gs), 'markerParents': np.int32(markerParents), 'markerNames': markerNames, 'markerOffsets': np.float32(markerOffsets), 'markerWeights': np.ones(len(markerNames), dtype=np.float32), 'rootMat': np.eye(3, 4, dtype=np.float32), 'labelNames': labelNames } if graph_out_fn is not None and labelGraphThreshold != -1: print 'Generating labelling graph...' from GCore import Label as GLabel c3d_data = c3d_frames[goodFrames, :, :][:, goodPoints, :][:, :, :] c3d_data = c3d_data[::stepSize, :, :] # graph = GLabel.graph_from_c3ds(skel_dict, markerNames, c3d_data, threshold=3) graph = GLabel.graph_from_c3ds(skel_dict, markerNames, c3d_data, threshold=labelGraphThreshold) IO.save(graph_out_fn, {'/root/graph': {'label_graph': graph}}) print 'Labelling graph saved to:', graph_out_fn if skel_out_fn is not None: IO.save(skel_out_fn, skel_dict) def test_skeleton(sd): '''TODO, write some code to verify that a dict actually is a skeleton.''' assert isinstance(sd['name'], str), 'name key should be a string' numJoints = sd['numJoints'] assert isinstance(numJoints, int), 'numJoints key should be an int' animJoints = None showStabilized = False if showStabilized: displayFrames = stabilizedFrames pointToGroup = stabilizedPointToGroup else: # show animated displayFrames = frames #c3d_frames[:,:,:3] displayLabels = pointLabels if first_time_only: # generate the file framesRTs = ASFReader.stabilizeAssignment(displayFrames, stabilizedPointToGroup) IO.save( os.path.join(tempDir, 'tmp90-28.IO'), { 'framesRTs': framesRTs, 'stabilizedPointToGroup': stabilizedPointToGroup, 'stablePoints': stablePoints, 'stablePointsGroups': stablePointsGroups }) for k, v in IO.load(os.path.join(tempDir, 'tmp90-28.IO'))[1].iteritems(): locals()[k] = v animJoints = ASFReader.unstabilize(stablePoints, framesRTs[stablePointsGroups]) print 'animJoints shape', animJoints.shape pointToGroup = -np.ones(displayFrames.shape[1], dtype=np.int32) print goodPoints.shape, pointToGroup.shape, stabilizedPointToGroup.shape pointToGroup = stabilizedPointToGroup #pointToGroup[goodPoints] = stabilizedPointToGroup # for displayFrames = c3d_frames[:,:,:3] groupRepresentatives = ASFReader.groupRepresentatives( data, stabilizedPointToGroup) numJoints = len(stablePoints) boneEdges = np.array(range(2 * numJoints), dtype=np.int32) boneVertices = np.zeros((numJoints * 2, 3), dtype=np.float32) boneVertices[::2] = stablePoints boneVertices[1::2] = displayFrames[ 0, groupRepresentatives[stablePointsGroups]] #import cv2 #movie = cv2.VideoCapture(directory+movieFilename) #frameOk, frameData = movie.read() #global md #md = {'buffer':frameData, 'height':frameData.shape[0], 'width':frameData.shape[1]} global app, win, view, frame, points, joints, bones app = QtGui.QApplication(sys.argv) app.setStyle('plastique') win = QtGui.QMainWindow() win.setFocusPolicy(QtCore.Qt.StrongFocus) # get keyboard events win.setWindowTitle('Imaginarium Skeleton Reconstruction Test %d' % cacheId) panel = GViewer.QGLPanel() view = panel.view view.setMinimumWidth(640) view.setMinimumHeight(480) win.setCentralWidget(panel) timelineDock = QtGui.QDockWidget('Timeline') timeline = UI.QTimeline(win) timeline.cb = setFrame timeline.setRange(0, goodFrames[-1]) timelineDock.setWidget(timeline) timelineDock.setFeatures(QtGui.QDockWidget.DockWidgetMovable | QtGui.QDockWidget.DockWidgetFloatable) frame = startFrame view.addCamera(UI.QGLViewer.Camera('default')) grid = GLGrid() view.primitives.append(grid) points = GLPoints3D(displayFrames[frame]) from colorsys import hsv_to_rgb colorTable = np.array([ hsv_to_rgb((h * 0.618033988749895) % 1, 0.5, 0.95) for h in xrange(max(pointToGroup) + 2) ], dtype=np.float32) colorTable[-1] = 0 points.colours = colorTable[pointToGroup] #points.names = displayLabels #points.pointSize = 3 view.primitives.append(points) joints = GLPoints3D(stablePoints) joints.names = map(str, xrange(len(stablePoints))) view.primitives.append(joints) bones = GLBones(boneVertices, boneEdges) view.primitives.append(bones) win.addDockWidget(QtCore.Qt.BottomDockWidgetArea, timelineDock) win.show() global md, img, g_detectingDots, g_readingMovie md, img, g_detectingDots = None, None, False g_readingMovie = False if g_readingMovie: md = MovieReader.open_file(os.path.join(directory, movieFilename)) img = np.frombuffer(md['vbuffer'], dtype=np.uint8).reshape(md['vheight'], md['vwidth'], 3) view.setImageData(md['vbuffer'], md['vheight'], md['vwidth'], 3) global allSkels allSkels = [] app.connect(app, QtCore.SIGNAL('lastWindowClosed()'), app.quit) sys.exit(app.exec_())
def import_movie_cb(): grip_dir = os.environ['GRIP_DATA'] movie_fn, _ = QApp.app.loadFilename('Choose a movie to open', grip_dir, 'Movie Files (*.mp4 *.mov *.avi *.flv *.mpg)') global md md = MovieReader.open_file(movie_fn)
def main(): global State, mats, movieFilenames, primitives global movies, primitives2D, deinterlacing, detectingWands import IO import sys, os deinterlacing = False detectingWands = False detectingTiara = False dot_detections = None detections_filename = None frame_offsets = None firstFrame, lastFrame = 0, 5000 drawDotSize = 4.0 fovX, (ox, oy), pan_tilt_roll, tx_ty_tz, distortion = 50., (0, 0), (0, 0, 0), (0, 1250, 0), (0, 0) mats = [] grip_directory = os.environ['GRIP_DATA'] if 0: fovX, (ox, oy), pan_tilt_roll, tx_ty_tz, distortion = 37.9, (0, 0), ( -66.0, 3.5, -0.2), (4850, 1330, 3280), (0, 0) # roughed in K, RT = Calibrate.composeK(fovX, ox, oy), Calibrate.composeRT( Calibrate.composeR(pan_tilt_roll), tx_ty_tz, 0) mat0 = [ K[:3, :3], RT[:3, :4], np.dot(K, RT)[:3, :], distortion, -np.dot(RT[:3, :3].T, RT[:3, 3]), [1920, 1080] ] fovX, (ox, oy), pan_tilt_roll, tx_ty_tz, distortion = 55.8, (0, 0), ( -103.6, 3.5, -0.3), (2980, 1380, -2180), (0, 0) # roughed in K, RT = Calibrate.composeK(fovX, ox, oy), Calibrate.composeRT( Calibrate.composeR(pan_tilt_roll), tx_ty_tz, 0) mat1 = [ K[:3, :3], RT[:3, :4], np.dot(K, RT)[:3, :], distortion, -np.dot(RT[:3, :3].T, RT[:3, 3]), [1920, 1080] ] fovX, (ox, oy), pan_tilt_roll, tx_ty_tz, distortion = 49.3, (0, 0), ( 27.9, 4.0, -0.2), (-5340, 1150, 5030), (0, 0) # roughed in K, RT = Calibrate.composeK(fovX, ox, oy), Calibrate.composeRT( Calibrate.composeR(pan_tilt_roll), tx_ty_tz, 0) mat2 = [ K[:3, :3], RT[:3, :4], np.dot(K, RT)[:3, :], distortion, -np.dot(RT[:3, :3].T, RT[:3, 3]), [1920, 1080] ] fovX, (ox, oy), pan_tilt_roll, tx_ty_tz, distortion = 50.6, (0, 0), ( -156.6, 4.9, 0.2), (-105, 1400, -4430), (0, 0) # roughed in K, RT = Calibrate.composeK(fovX, ox, oy), Calibrate.composeRT( Calibrate.composeR(pan_tilt_roll), tx_ty_tz, 0) mat3 = [ K[:3, :3], RT[:3, :4], np.dot(K, RT)[:3, :], distortion, -np.dot(RT[:3, :3].T, RT[:3, 3]), [1920, 1080] ] mats = [mat0, mat1, mat2, mat3] xcp_filename = '154535_Cal168_Floor_Final.xcp' directory = os.path.join(grip_directory, 'REFRAME') movieFilenames = [ '001E0827_01.MP4', '001F0813_01.MP4', '001G0922_01.MP4', '001H0191_01.MP4' ] #mats,movieFilenames = mats[:1],movieFilenames[:1] # restrict to single-view frame_offsets = [119 + 160, 260, 339, 161] small_blur, large_blur = 1, 25 min_dot_size = 1.0 max_dot_size = 20.0 circularity_threshold = 3.0 threshold_bright, threshold_dark_inv = 250, 250 #135,135 elif 0: xcp_filename = '201401211653-4Pico-32_Quad_Dialogue_01_Col_wip_01.xcp' detections_filename = 'detections.dat' detectingTiara = True pan_tilt_roll = (0, 0, 90) distortion = (0.291979, 0.228389) directory = os.path.join(os.environ['GRIP_DATA'], 'ted') movieFilenames = [ '201401211653-4Pico-32_Quad_Dialogue_01_%d.mpg' % xi for xi in range(1) ] firstFrame = 511 small_blur, large_blur = 1, 20 min_dot_size = 1.0 max_dot_size = 16.0 circularity_threshold = 3.0 threshold_bright, threshold_dark_inv = 0, 170 elif 1: xcp_filename = '50_Grip_RoomCont_AA_02.xcp' detections_filename = 'detections.dat' pan_tilt_roll = (0, 0, 0) distortion = (0.291979, 0.228389) directory = os.path.join(os.environ['GRIP_DATA'], '151110') movieFilenames = ['50_Grip_RoomCont_AA_02.v2.mov'] firstFrame = 0 small_blur, large_blur = 1, 20 min_dot_size = 1.0 max_dot_size = 16.0 circularity_threshold = 3.0 threshold_bright, threshold_dark_inv = 170, 170 attrs = dict([(v, eval(v)) for v in [ 'small_blur', 'large_blur', 'threshold_bright', 'threshold_dark_inv', 'circularity_threshold', 'min_dot_size', 'max_dot_size' ]]) primitives2D = QGLViewer.makePrimitives2D(([], []), ([], [])) primitives = [] if len(movieFilenames) is 1: # TODO: time_base, timecode K, RT = Calibrate.composeK(fovX, ox, oy), Calibrate.composeRT( Calibrate.composeR(pan_tilt_roll), tx_ty_tz, 0) mats = [[ K[:3, :3], RT[:3, :4], np.dot(K, RT)[:3, :], distortion, -np.dot(RT[:3, :3].T, RT[:3, 3]), [1920, 1080] ]] camera_ids = ['video'] movies = [ MovieReader.open_file(os.path.join(directory, movieFilenames[0]), audio=False) ] else: # hard coded cameras if xcp_filename.endswith('.xcp'): if detectingTiara: # gruffalo c3d_filename = os.path.join( directory, '201401211653-4Pico-32_Quad_Dialogue_01_Col_wip_02.c3d') from IO import C3D c3d_dict = C3D.read(c3d_filename) global c3d_frames c3d_frames, c3d_fps, c3d_labels = c3d_dict['frames'], c3d_dict[ 'fps'], c3d_dict['labels'] c3d_subject = '' #'TedFace' which = np.where( [s.startswith(c3d_subject) for s in c3d_labels])[0] c3d_frames = c3d_frames[:, which, :] c3d_labels = [c3d_labels[i] for i in which] print len(c3d_frames) xcp, xcp_data = ViconReader.loadXCP( os.path.join(directory, xcp_filename)) mats.extend(xcp) elif xcp_filename.endswith('.cal'): from IO import OptitrackReader xcp, xcp_data = OptitrackReader.load_CAL( os.path.join(directory, xcp_filename)) mats = xcp print 'mats', len(mats), len(movieFilenames) assert (len(mats) == len(movieFilenames)) camera_ids = [] movies = [] for ci, mf in enumerate(movieFilenames): fo = 0 if frame_offsets is None else frame_offsets[ci] movies.append( MovieReader.open_file(os.path.join(directory, mf), audio=False, frame_offset=fo)) camera_ids = ['cam_%d' % ci for ci in xrange(len(mats))] print len(mats), len(movies), len(camera_ids) primitives.append(GLPoints3D([])) primitives.append(GLPoints3D([])) primitives.append(GLPoints3D([])) primitives[0].colour = (0, 1, 1, 0.5) # back-projected "cyan" points primitives[1].colour = (0, 0, 1, 0.5) primitives[1].pointSize = 5 primitives[2].colour = (1, 0, 0, 0.99) if len(movieFilenames) != 1 and detections_filename != None: try: dot_detections = IO.load(detections_filename)[1] except: numFrames = len(c3d_frames) # TODO HACK HACK dot_detections = movies_to_detections(movies, range(numFrames), deinterlacing, attrs) IO.save(detections_filename, dot_detections) if detectingTiara: x3ds_seq = {} for fi in dot_detections.keys(): frame = c3d_frames[(fi - 55) % len(c3d_frames)] which = np.array(np.where(frame[:, 3] == 0)[0], dtype=np.int32) x3ds_seq[fi] = np.concatenate((VICON_tiara_x3ds + np.array([150,-100,0],dtype=np.float32),frame[which,:3])), \ np.concatenate((np.arange(len(VICON_tiara_x3ds),dtype=np.int32),which+len(VICON_tiara_x3ds))) dot_labels = get_labels(dot_detections.keys(), x3ds_seq, dot_detections, mats, x2d_threshold=0.05) calibration_fi = 546 - 2 - 6 RT = tighten_calibration(x3ds_seq[calibration_fi], dot_labels[calibration_fi], mats) for v in c3d_frames: v[:, :3] = np.dot(v[:, :3], RT[:3, :3].T) + RT[:, 3] if True: dot_detections = IO.load(detections_filename)[1] x3ds_seq = {} for fi in dot_detections.keys(): frame = c3d_frames[(fi - 55) % len(c3d_frames)] which = np.array(np.where(frame[:, 3] == 0)[0], dtype=np.int32) x3ds_seq[fi] = np.concatenate((VICON_tiara_x3ds + np.array([0,1000,0],dtype=np.float32),frame[which,:3])), \ np.concatenate((np.arange(len(VICON_tiara_x3ds),dtype=np.int32),which+len(VICON_tiara_x3ds))) #dot_labels = get_labels(dot_detections.keys(), x3ds_seq, dot_detections, mats, x2d_threshold = 0.05) if detectingTiara: primitives.append(GLPoints3D(VICON_tiara_x3ds + [0, 1000, 0])) primitives[-1].pointSize = 5 global track3d, prev_frame, booting, trackGraph track3d = Label.Track3D(mats[:len(movies)], x2d_threshold=0.03, x3d_threshold=5.0, min_rays=3, boot_interval=2) #tilt_threshold = 0.01, gruffalo trackGraph = Label.TrackGraph() prev_frame = 0 booting = 1 from UI import QApp from PySide import QtGui from GCore import State # Modified the options parameter for fields to be the range of acceptable values for the box # Previously would crash if small_blur got too low QApp.fields = { 'image filter': [ ('small_blur', 'Small blur radius', 'This is part of the image filter which controls the size of smallest detected features.', 'int', small_blur, { "min": 0, "max": None }), ('large_blur', 'Large blur radius', 'This is part of the image filter which controls the size of largest detected features.', 'int', large_blur, { "min": 0, "max": None }), ('threshold_bright', 'threshold_bright', 'This is part of the image filter which controls the size of smallest detected features.', 'int', threshold_bright, { "min": 0, "max": 255 }), ('threshold_dark_inv', 'threshold_dark_inv', 'This is part of the image filter which controls the size of largest detected features.', 'int', threshold_dark_inv, { "min": 0, "max": 255 }), ('circularity_threshold', 'circularity_threshold', 'How circular?.', 'float', circularity_threshold, { "min": 0, "max": 100 }), ('min_dot_size', 'min_dot_size', 'min_dot_size smallest detected features.', 'float', min_dot_size, { "min": 0, "max": 100 }), ('max_dot_size', 'max_dot_size', 'max_dot_size largest detected features.', 'float', max_dot_size, { "min": 0, "max": 100 }), ] } State.addKey('dotParams', {'type': 'image filter', 'attrs': attrs}) State.setSel('dotParams') appIn = QtGui.QApplication(sys.argv) appIn.setStyle('plastique') win = QApp.QApp() win.setWindowTitle('Imaginarium Dots Viewer') QGLViewer.makeViewer(primitives=primitives, primitives2D=primitives2D, timeRange=(firstFrame, lastFrame), callback=setFrame, mats=mats, camera_ids=camera_ids, movies=movies, pickCallback=picked, appIn=appIn, win=win)
def main(x2d_filename, xcp_filename, c3d_filename=None): '''Generate a 3D view of an x2d file, using the calibration.''' global x2d_frames, mats, Ps, c3d_frames, primitives, primitives2D, track3d, prev_frame, track_orn, orn_graph, boot, orn_mapper, mar_mapper prev_frame = None c3d_frames = None if c3d_filename != None: c3d_dict = C3D.read(c3d_filename) c3d_frames, c3d_fps, c3d_labels = c3d_dict['frames'], c3d_dict[ 'fps'], c3d_dict['labels'] mats, xcp_data = ViconReader.loadXCP(xcp_filename) camera_ids = [int(x['DEVICEID']) for x in xcp_data] print 'loading 2d' x2d_dict = ViconReader.loadX2D(x2d_filename) x2d_frames = x2d_dict['frames'] cameras_info = ViconReader.extractCameraInfo(x2d_dict) print 'num frames', len(x2d_frames) Ps = [m[2] / (m[0][0, 0]) for m in mats] track3d = Label.Track3D(mats) primitives = QGLViewer.makePrimitives(vertices=[], altVertices=[]) primitives2D = QGLViewer.makePrimitives2D(([], [0])) global g_all_skels, md directory = os.path.join(os.environ['GRIP_DATA'], '151110') _, orn_skel_dict = IO.load(os.path.join(directory, 'orn.skel')) movie_fn = os.path.join(directory, '50_Grip_RoomCont_AA_02.v2.mov') md = MovieReader.open_file(movie_fn, audio=True, frame_offset=0, volume_ups=10) asf_filename = os.path.join(directory, 'Martha.asf') amc_filename = os.path.join(directory, 'Martha.amc') asf_dict = ASFReader.read_ASF(asf_filename) mar_skel_dict = ASFReader.asfDict_to_skelDict(asf_dict) mar_skel_dict['anim_dict'] = ASFReader.read_AMC(amc_filename, asf_dict) for k in ('geom_Vs', 'geom_vsplits', 'geom_Gs'): mar_skel_dict[k] = orn_skel_dict[k].copy() mar_skel_dict['shape_weights'] = orn_skel_dict['shape_weights'] mar_skel_dict['geom_dict'] = orn_skel_dict['geom_dict'] orn_vss = ViconReader.loadVSS(os.path.join(directory, 'Orn.vss')) orn_vss_chan_mapping = [ orn_vss['chanNames'].index(n) for n in orn_skel_dict['chanNames'] ] orn_anim_dict = orn_skel_dict['anim_dict'] orn_vss_anim = np.zeros( (orn_anim_dict['dofData'].shape[0], orn_vss['numChans']), dtype=np.float32) orn_vss_anim[:, orn_vss_chan_mapping] = orn_anim_dict['dofData'] orn_anim_dict['dofData'] = orn_vss_anim orn_vss['anim_dict'] = orn_anim_dict for x in [ 'geom_dict', 'geom_Vs', 'geom_vsplits', 'geom_Gs', 'shape_weights' ]: orn_vss[x] = orn_skel_dict[x] orn_skel_dict = orn_vss g_all_skels = {} orn_mesh_dict, orn_skel_mesh, orn_geom_mesh = orn_t = Character.make_geos( orn_skel_dict) g_all_skels['orn'] = (orn_skel_dict, orn_t) orn_skel_dict['chanValues'][:] = 0 Character.updatePoseAndMeshes(orn_skel_dict, orn_skel_mesh, orn_geom_mesh) mar_mesh_dict, mar_skel_mesh, mar_geom_mesh = mar_t = Character.make_geos( mar_skel_dict) g_all_skels['mar'] = (mar_skel_dict, mar_t) #ted_mesh_dict, ted_skel_mesh, ted_geom_mesh = ted_t = Character.make_geos(ted_skel_dict) #g_all_skels['ted'] = (ted_skel_dict, ted_t) #ted_skel_dict['chanValues'][0] += 1000 #Character.updatePoseAndMeshes(ted_skel_dict, ted_skel_mesh, ted_geom_mesh) mnu = orn_skel_dict['markerNamesUnq'] mns = orn_skel_dict['markerNames'] effectorLabels = np.array([mnu.index(n) for n in mns], dtype=np.int32) orn_graph = Label.graph_from_skel(orn_skel_dict, mnu) boot = -10 track_orn = Label.TrackModel(orn_skel_dict, effectorLabels, mats) #ted = GLSkel(ted_skel_dict['Bs'], ted_skel_dict['Gs']) #, mvs=ted_skel_dict['markerOffsets'], mvis=ted_skel_dict['markerParents']) #ted = GLSkeleton(ted_skel_dict['jointNames'],ted_skel_dict['jointParents'], ted_skel_dict['Gs'][:,:,3]) #ted.setName('ted') #ted.color = (1,1,0) #orn = GLSkeleton(orn_skel_dict['jointNames'],orn_skel_dict['jointParents'], orn_skel_dict['Gs'][:,:,3]) #orn.setName('orn') #orn.color = (0,1,1) #square = GLMeshes(names=['square'],verts=[[[0,0,0],[1000,0,0],[1000,1000,0],[0,1000,0]]],vts=[[[0,0],[1,0],[1,1],[0,1]]],faces=[[[0,1,2,3]]],fts=[[[0,1,2,3]]]) #square.setImageData(np.array([[[0,0,0],[255,255,255]],[[255,255,255],[0,0,0]]],dtype=np.uint8)) #orn_geom_mesh.setImageData(np.array([[[0,0,0],[255,255,255]],[[255,255,255],[0,0,0]]],dtype=np.uint8)) P = Calibrate.composeP_fromData((60.8, ), (-51.4, 14.7, 3.2), (6880, 2860, 5000), 0) # roughed in camera for 151110 ks = (0.06, 0.0) mat = Calibrate.makeMat(P, ks, (1080, 1920)) orn_mapper = Opengl.ProjectionMapper(mat) orn_mapper.setGLMeshes(orn_geom_mesh) orn_geom_mesh.setImage((md['vbuffer'], (md['vheight'], md['vwidth'], 3))) mar_mapper = Opengl.ProjectionMapper(mat) mar_mapper.setGLMeshes(mar_geom_mesh) mar_geom_mesh.setImage((md['vbuffer'], (md['vheight'], md['vwidth'], 3))) global g_screen g_screen = Opengl.make_quad_distortion_mesh() QGLViewer.makeViewer(mat=mat,md=md,layers = {\ #'ted':ted, 'orn':orn, #'ted_skel':ted_skel_mesh,'ted_geom':ted_geom_mesh,\ #'square':square, 'orn_skel':orn_skel_mesh,'orn_geom':orn_geom_mesh,\ 'mar_skel':mar_skel_mesh,'mar_geom':mar_geom_mesh,\ }, primitives=primitives, primitives2D=primitives2D, timeRange=(0, len(x2d_frames) - 1, 4, 25.0), callback=intersectRaysCB, mats=mats,camera_ids=camera_ids)
def retrack_refresh_rbfn(): grip_dir = os.environ['GRIP_DATA'] movie_fn,_ = QApp.app.loadFilename('Choose a movie to open', grip_dir, 'Movie Files (*.mp4 *.mov *.avi *.flv *.mpg)') md = MovieReader.open_file(movie_fn, audio=False) update_rbfn(md)
print 'generating ted skel and anim' ASFReader.convertASFAMC_to_SKELANIM(asf_filename, amc_filename, skelFilename, animFilename) ted_skel = IO.load(skelFilename)[1] ted_anim = IO.load(animFilename)[1] ted_xcp_mats, ted_xcp_data = ViconReader.loadXCP(xcp_filename) if True: # facial animation global ted_geom, ted_geom2, ted_shape, tony_geom, tony_shape, tony_geom2, tony_obj, ted_obj, diff_geom, c3d_frames global tony_shape_vector, tony_shape_mat, ted_lo_rest, ted_lo_mat, c3d_points global md, movies ted_dir = os.path.join(os.environ['GRIP_DATA'], 'ted') wavFilename = os.path.join(ted_dir, '32T01.WAV') md = MovieReader.open_file(wavFilename) c3d_filename = os.path.join( ted_dir, '201401211653-4Pico-32_Quad_Dialogue_01_Col_wip_02.c3d') c3d_dict = C3D.read(c3d_filename) c3d_frames, c3d_fps, c3d_labels = c3d_dict['frames'], c3d_dict[ 'fps'], c3d_dict['labels'] if False: # only for cleaned-up data c3d_subject = 'TedFace' which = np.where([s.startswith(c3d_subject) for s in c3d_labels])[0] c3d_frames = c3d_frames[:, which, :] c3d_labels = [c3d_labels[i] for i in which] print c3d_labels if False: # this is for the cleaned-up data (don't apply the other offset...) offset = Calibrate.composeRT(Calibrate.composeR((0.0, 0.0, 0)), (0, 0, -8), 0) # 0.902
def initialise(self, interface, attrs): directory = self.resolvePath(attrs['directory']) if not directory: return False prefix = attrs['prefix'] prefixFilename = self.resolvePath(attrs['prefixFilename']) if prefix and not prefixFilename: return calibration = attrs['calibration'] calibrationFilename = self.resolvePath(attrs['calibrationFilename']) calibrationLocation = self.resolvePath(attrs['calibrationLocation']) if calibration and (not calibrationFilename and not calibrationLocation): return False movieFilenames = [] try: for file in os.listdir(directory): if prefixFilename and not file.startswith(prefixFilename): continue if file.endswith('.avi') or file.endswith( '.mov') or file.endswith('mp4'): movieFilenames.append(os.path.join(directory, file)) except WindowsError as e: self.logger.error('Could not find videos: % s' % str(e)) if not movieFilenames: # TODO: Here we'll have to clear the cameras etc. return False # Windows will produce a wonky order, i.e. 1, 10, 11, .., 2, 3, .. # Use natural sorting to rectify movieFilenames.sort(key=self.alphaNumKey) self.camera_ids = [] self.camera_names = [] self.movies = [] self.mats = [] vheights = [] vwidths = [] timecodes = [] hasTimecode = False useTimecode = attrs['useTimecode'] if 'useTimecode' in attrs else True offset = attrs['offset'] if 'offsets' in attrs and attrs['offsets']: offsets = eval(attrs['offsets']) else: offsets = [offset] * len(movieFilenames) for ci, mf in enumerate(movieFilenames): self.logger.info('Loading MovieReader: %s' % mf) movieData = MovieReader.open_file(mf, audio=False, frame_offset=offsets[ci]) if movieData['vbuffer'] is not None: self.movies.append(movieData) self.timecodeOffsets.append(0) if 'timecode' in movieData and movieData['timecode']: hasTimecode = True timecodes.append(movieData['timecode']) # Make sure we have all the cameras before continuing if len(self.movies) != len(movieFilenames): self.logger.error('Could not load all movies in sequence') return # Make sure we have as many time codes as movies (if we have any) if hasTimecode and len(self.movies) != len(timecodes): self.logger.error('Not all movie files have a time code') return # See if we can get the offsets using the time codes if hasTimecode and useTimecode: print 'Video timecodes:', timecodes fps_all = [round(m['fps']) for m in self.movies] print 'FPS:', fps_all timecodeValues = [ Timecode.TCFtoInt(tc, fps) for tc, fps in zip(timecodes, fps_all) ] tcOrderDesc = [ timecodes.index(tc) for tc in sorted(timecodes, reverse=True) ] # Set the first offset to 0 firstTcIndex = tcOrderDesc[0] self.timecodeOffsets[firstTcIndex] = 0 largestTc = timecodes[firstTcIndex] offsetStartIndex = 1 # We can also get the timecode destination from an incoming location, e.g. 2D detections if 'timecodeLocation' in attrs and attrs['timecodeLocation']: tcSyncTime = interface.attr( 'timecode', atLocation=attrs['timecodeLocation']) if tcSyncTime is not None: tcSyncValue = Timecode.TCFtoInt(tcSyncTime, fps_all[0]) if tcSyncValue < timecodeValues[firstTcIndex]: self.logger.error( 'Sync timecode %s is smaller than video timecodes (%s).' % (tcSyncTime, largestTc)) return largestTc = tcSyncTime offsetStartIndex = 0 self.timecode = largestTc self.logger.info('Setting timecode to: %s' % (largestTc)) # Calculate the offset for each camera to get it up to speed with the target timecode # TODO: Replace hard coded timecode fps and multiplier timecodeFps, timecodeMultiplier = 25., 2. for tcInd in tcOrderDesc[offsetStartIndex:]: diff = Timecode.TCSub(largestTc, timecodes[tcInd], timecodeFps) self.timecodeOffsets[tcInd] = Timecode.TCFtoInt( diff, timecodeFps) * timecodeMultiplier if self.timecodeOffsets: print 'Video timecode offsets:', self.timecodeOffsets self.camera_ids = [ 'Camera %d' % ci for ci in xrange(len(movieFilenames)) ] self.movies = self.movies if not calibrationLocation: calibrationLocation = interface.root() if calibrationFilename or interface.hasAttr( 'mats', atLocation=calibrationLocation): if calibrationFilename: # TODO: Detect filetype, e.g. .cal and .xcp and handle accordingly try: self.mats, rawCalData = OptitrackReader.load_CAL( calibrationFilename) if not self.mats: return False except IOError as e: self.logger.error('Could not load calibration file: %s' % str(e)) return False else: self.mats = interface.attr('mats', atLocation=calibrationLocation) if not self.mats: self.logger.error('Could not find calibration mats: %s' % calibrationLocation) return False else: from GCore import Calibrate for ci, (cid, md) in enumerate(zip(self.camera_ids, self.movies)): if md is not None: self.mats.append( Calibrate.makeUninitialisedMat( ci, (md['vheight'], md['vwidth']))) for md in self.movies: vheights.append(md['vheight']) vwidths.append(md['vwidth']) Ps = interface.getPsFromMats(self.mats) self.attrs = { 'vheight': vheights, 'vwidth': vwidths, 'camera_ids': self.camera_ids, 'Ps': Ps, 'mats': self.mats, 'colour': eval(attrs['colour']) } if self.camera_names: self.attrs['camera_names'] = self.camera_names self.initialised = True return True