Esempio n. 1
0
def nextFrame():
	global view,actors,MyClient, skels
	frame = viconParseFrame(MyClient)
	for name in skels: actors.setActorData(name, False)
	if type(frame) == dict:
		for subject in frame['subjects']:
			name = subject['name']
			if not skels.has_key(name):
				actor = actors.addActor(name)
				print 'adding',name
				boneNames = subject['bone_names']
				boneParents = [[boneNames.index([parent,boneNames[0]][parent=='']),-1][parent==''] for parent in subject['bone_parents']]
				boneTs = subject['bone_Ts']
				skel = GLSkeleton(boneNames,boneParents,boneTs)
				skel.setName(name)
				view.primitives.append(skel)
				skels[name] = skel
				point = GLPoints3D(subject['marker_Ts'], subject['marker_names'])
				points[name] = point
			actors.setActorData(name, True)
			skels[name].vertices[:] = subject['bone_Ts']
			points[name].vertices[:] = subject['marker_Ts']
		if frame.has_key('unlabelled_markers'):
			global unlabelledMarkers
			unlabelledMarkers.vertices = list(frame['unlabelled_markers'])
		else:
			unlabelledMarkers.vertices = []
	view.updateGL()
Esempio n. 2
0
	def addPoints3D(self, points):
		'''
		add 3d points to the viewer.

		:type points: :class:`numpy.array` (Nx3)
		:param points: array of vertices
		'''
		glPoints = GLPoints3D(points)
		self.view().primitives.append(glPoints)
		return self.view().primitives[-1]
Esempio n. 3
0
def makePrimitives(vertices=None, altVertices=None, skelDict=None, altSkelDict=None, skels=None):
	from UI import GLPoints3D, GLSkel
	primitives = []
	if vertices is not None:
		points = GLPoints3D(vertices)
		primitives.append(points)
	if altVertices is not None:
		altpoints = GLPoints3D(altVertices)
		altpoints.pointSize = 5
		altpoints.colour = (1.0, 1.0, 0, 0.5)
		primitives.append(altpoints)
	if skelDict is not None:
		skel = GLSkel(skelDict['Bs'], skelDict['Gs'], mvs=skelDict['markerOffsets'], mvis=skelDict['markerParents'])
		skel.boneColour = (1, 1, 0, 1)
		primitives.append(skel)
		if skelDict.has_key('name'): skel.setName(skelDict['name'])
	if altSkelDict is not None:
		skel2 = GLSkel(altSkelDict['Bs'], altSkelDict['Gs'], mvs=altSkelDict['markerOffsets'], mvis=altSkelDict['markerParents'])
		skel2.boneColour = (1, 0, 1, 1)
		primitives.append(skel2)
		if altSkelDict.has_key('name'): skel2.setName(altSkelDict['name'])
	if skels is not None:
		primitives.extend(skels)
	return primitives
Esempio n. 4
0
def main():
	# Program options
	global actors
	TransmitMulticast = False
	HostName = "localhost:801"
	import sys
	if (len(sys.argv) > 1):
		HostName = sys.argv[1]
		print 'Setting hostname to ',HostName

	from UI import QApp, QActorWidget, GLGrid, GLPoints3D, GLSkeleton
	from PySide import QtCore, QtGui, QtOpenGL
	import numpy as np

	global app,win,view
	app = QtGui.QApplication(sys.argv)
	app.setStyle('plastique')
	win = QApp.QApp()
	win.setWindowTitle('Imaginarium ViconConnect')
	view = win.view()
	view.setMinimumWidth(640)
	view.setMinimumHeight(480)
	win.setCentralWidget(view)
	actorsDock = QtGui.QDockWidget('Actors')
	actors = QActorWidget.QActorWidget(setActorVisible)
	area = QtGui.QScrollArea()
	area.setMinimumWidth(200)
	area.setWidgetResizable(True)
	area.setWidget(actors)
	actorsDock.setWidget(area)
	actorsDock.setFeatures(QtGui.QDockWidget.DockWidgetMovable|QtGui.QDockWidget.DockWidgetFloatable)
	win.addDockWidget(QtCore.Qt.RightDockWidgetArea, actorsDock)
	win.show()
	global unlabelledMarkers
	unlabelledMarkers = GLPoints3D([])
	view.primitives.append(unlabelledMarkers)
	global MyClient, skels, points
	skels = {}
	points = {}
	view.primitives.append(GLGrid())
	timer = QtCore.QTimer(app)
	app.connect(timer, QtCore.SIGNAL('timeout()'), nextFrame)
	timer.start(20)

	MyClient = viconConnect(HostName, TransmitMulticast)
	
	app.connect(app, QtCore.SIGNAL('lastWindowClosed()') , app.quit)
	sys.exit(app.exec_())
Esempio n. 5
0
def nextFrame():
    """
	converts motion capture data into a number of items of global data
	which will be used to perform the OpenGL drawing
	"""
    global view, actors, skels, client
    frame = client.subscribe()

    for name in skels:
        actors.setActorData(name, False)
    if type(frame) == dict:
        frameHash = frame['hash']
        if frameHash == client.stateHash:
            for subject, subject_state in zip(frame['subjects'],
                                              client.state['subjects']):
                name = subject['name']
                if not skels.has_key(name):
                    actor = actors.addActor(name)
                    print 'adding', name
                    boneNames = subject_state['bone_names']
                    boneParents = [[
                        boneNames.index([parent, boneNames[0]][parent == '']),
                        -1
                    ][parent == '']
                                   for parent in subject_state['bone_parents']]
                    boneTs = subject['bone_Ts']
                    skel = GLSkeleton(boneNames, boneParents, boneTs)
                    skel.setName(name)
                    view.primitives.append(skel)
                    skels[name] = skel
                    point = GLPoints3D(subject['marker_Ts'],
                                       subject_state['marker_names'])
                    points[name] = point
                actors.setActorData(name, True)
                skels[name].vertices[:] = subject['bone_Ts']
                points[name].vertices[:] = subject['marker_Ts']
        if frame.has_key('unlabelled_markers'):
            global unlabelledMarkers
            unlabelledMarkers.vertices = list(frame['unlabelled_markers'])
        else:
            unlabelledMarkers.vertices = []
    view.updateGL()
Esempio n. 6
0
def main():
    # Program options
    hostName = "localhost:18667"
    if (len(sys.argv) > 1):
        hostName = sys.argv[1]
        print 'Setting hostName to ', hostName

    global app, win, view, state, stateHash, actors
    app = QtGui.QApplication(sys.argv)
    app.setStyle('plastique')
    win = QApp.QApp()
    win.setWindowTitle('Imaginarium Client')
    view = win.view()
    win.resize(640, 480)
    actorsDock = QtGui.QDockWidget('Actors')
    actors = QActorWidget.QActorWidget(setActorVisible)
    area = QtGui.QScrollArea()
    area.setMinimumWidth(200)
    area.setWidgetResizable(True)
    area.setWidget(actors)
    actorsDock.setWidget(area)
    actorsDock.setFeatures(QtGui.QDockWidget.DockWidgetMovable
                           | QtGui.QDockWidget.DockWidgetFloatable)
    win.addDockWidget(QtCore.Qt.RightDockWidgetArea, actorsDock)
    win.show()
    global unlabelledMarkers
    unlabelledMarkers = GLPoints3D([])
    view.primitives.append(unlabelledMarkers)
    global skels, points
    skels = {}
    points = {}
    view.primitives.append(GLGrid())
    timer = QtCore.QTimer(app)
    app.connect(timer, QtCore.SIGNAL('timeout()'), nextFrame)
    timer.start(20)

    global client
    client = ReframeClient('localhost', 18667)

    app.connect(app, QtCore.SIGNAL('lastWindowClosed()'), app.quit)
    sys.exit(app.exec_())
Esempio n. 7
0
def drawEdges(win, locationName, attrs, interface, picked):
    from UI import GLPoints3D
    edgesLayer = None
    if not win.hasLayer(locationName):
        edgesLayer = win.setLayer(locationName,
                                  GLPoints3D(np.array([], dtype=np.float32)))
    else:
        edgesLayer = win.getLayer(locationName)

    points_x0 = attrs['x0']
    points_x1 = attrs['x1']

    points = np.concatenate((points_x0, points_x1))
    conns = [[i, i + len(points_x0)] for i in range(len(points_x1))]

    colours = np.array([], dtype=np.float32)
    if 'x3ds_colours' in attrs: colours = attrs['x3ds_colours']

    if 'x3ds_labels' in attrs:
        x3ds_labels = attrs['x3ds_labels']
        edgesLayer.setData(np.array(points, dtype=np.float32),
                           names=Interface.getLabelNames(x3ds_labels),
                           colours=colours)
    else:
        edgesLayer.setData(np.array(points, dtype=np.float32), colours=colours)

    edgesLayer.edges = conns
    edgesLayer.visible = isVisible(attrs)

    if 'colour' in attrs:
        edgesLayer.colour = attrs['colour']
        edgesLayer.edgeColour = attrs['colour']
    else:
        edgesLayer.colour = (0., 0., 0., 1.)
        edgesLayer.edgeColour = (0., 0., 0., 1.)

    if 'edgeColour' in attrs: edgesLayer.edgeColour = attrs['edgeColour']

    if 'pointSize' in attrs: edgesLayer.pointSize = attrs['pointSize']
    else: edgesLayer.pointSize = 6
Esempio n. 8
0
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_())
Esempio n. 9
0
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)
Esempio n. 10
0
 def clearLayer(self, layerName):
     # A little temporary hack until we've got some nice UI way of disabling/enabling layers
     from UI import GLPoints3D
     self.win.view().setLayer(layerName, GLPoints3D([]))
Esempio n. 11
0
    img_fs = np.int32([[0, 1, 2, 3]])
    img_ts = np.float32([[1, 0, 0, 0], [0, 1, 0, 1000], [0, 0, 1, 0]])
    img_vts = np.float32([[0, 1], [1, 1], [1, 0], [0, 0]])

    if not State.hasKey('/edges') or not State.hasKey('/tris'):
        retriangulate()

    template_vs = get_predictor()['ref_shape'] * 100
    size = len(template_vs)
    markup_x2ds = np.zeros((size, 3), dtype=np.float32)
    ref_vs = np.zeros((size, 3), dtype=np.float32)
    ref_vs[:, :2] = template_vs

    markup_mesh = GLPoints3D(vertices=markup_x2ds,
                             edges=None,
                             names=[],
                             colour=[0, 1, 0, 1],
                             edgeColour=[1, 1, 1, 1])
    ref_mesh = GLPoints3D(vertices=ref_vs,
                          edges=State.getKey('/edges', None),
                          edgeColour=[0.0, 1.0, 0.0, 0.5],
                          colour=[0.0, 1.0, 1.0, 0.5])
    image_mesh = GLMeshes(names=['image_mesh'],
                          verts=[img_vs],
                          faces=[img_fs],
                          transforms=[img_ts],
                          bones=[[]],
                          vts=[img_vts])
    layers = {
        'image_mesh': image_mesh,
        'markup_mesh': markup_mesh,
Esempio n. 12
0
def main():
	from UI import QGLViewer
	from UI import GLMeshes, GLPoints3D

	global g_setting_frame
	g_setting_frame = False
	# static data
	global g_webcam, g_md, g_rbfn, g_predictor, g_head_pan_shape, g_head_tilt_shape
	# runtime options and state
	global g_prev_smooth_shape, g_prev_vs, g_hmc_boot, g_neutral_corrective_shape, g_settle, g_head_pan_tilt_roll, g_smooth_pose
	global g_directory, g_TIS_server, g_mode, g_frame

	g_TIS_server = SocketServer.SocketServer()
	g_mode, g_frame = 0,{}

	grip_dir = os.environ['GRIP_DATA']
	g_directory = grip_dir

	g_webcam,g_md = None,None

	g_prev_vs, g_prev_smooth_shape = None,None
	g_hmc_boot = None
	#clear_neutral()
	g_neutral_corrective_shape = IO.load(os.path.join(g_directory,'neutral.out'))[1]
	g_settle = -1
	g_head_pan_tilt_roll = None
	g_smooth_pose = {}

	aam = IO.load(os.path.join(g_directory,'aam.out'))[1]
	if 0:
		svt = np.float32(aam['shapes']).reshape(-1,140)
		svt = np.dot(aam['shapes_u'],aam['shapes_s'].reshape(-1,1)*aam['shapes_vt'])
		svt = aam['shapes_s'].reshape(-1,1)*aam['shapes_vt']
		tmp = svt.reshape(svt.shape[0],-1,2)
		Sx,Sy = tmp[:,:,0],tmp[:,:,1]
		tmp = np.dot(np.dot(Sy.T,np.dot(Sx,Sx.T)),Sy)
		u,s,vt = np.linalg.svd(tmp, full_matrices=False)
		print s
		g_head_pan_shape = np.zeros((svt.shape[1]/2,2),dtype=np.float32)
		g_head_tilt_shape = np.zeros((svt.shape[1]/2,2),dtype=np.float32)
		g_head_pan_shape[:,0] = g_head_tilt_shape[:,1] = vt[0]
		print np.sum(g_head_pan_shape * aam['shapes_vt'][0].reshape(-1,2))
		print np.sum(g_head_tilt_shape * aam['shapes_vt'][1].reshape(-1,2))
	g_head_pan_shape = aam['shapes_vt'][0].reshape(-1,2)
	g_head_tilt_shape = aam['shapes_vt'][1].reshape(-1,2)
	g_head_tilt_shape = g_head_pan_shape[:,::-1]*np.float32([1,-1])
	print np.sum(g_head_pan_shape*g_head_tilt_shape)
	g_head_pan_shape *= np.linalg.norm(g_head_pan_shape)**-0.5
	g_head_tilt_shape *= np.linalg.norm(g_head_tilt_shape)**-0.5
	if np.sum(g_head_pan_shape[:,0] < 1): g_head_pan_shape = -g_head_pan_shape
	if np.sum(g_head_tilt_shape[:,1] > 1): g_head_tilt_shape = -g_head_tilt_shape
	#print np.sum(g_head_pan_shape * g_head_tilt_shape)
	#print np.dot(g_head_pan_shape[:,0],g_head_tilt_shape[:,1])

	g_predictor = Face.load_predictor(os.path.join(g_directory,'train.out'))
	rbfn_filename = os.path.join(g_directory,'rbfn.out')
	g_rbfn = IO.load(rbfn_filename)[1]
	#g_rbfn = convert_rbfn(rbfn_in_filename)
	#IO.save(rbfn_filename, g_rbfn)

	
	ref_shape = g_predictor['ref_shape']
	cx,cy = np.mean(ref_shape,axis=0)
	vx,vy = (np.var(ref_shape,axis=0)**0.5) * 2.5
	geo_bs = []
	ref_fs = Face.triangulate_2D(ref_shape)
	for p0,p1,p2 in ref_fs:
		geo_bs.append((p0,p1))
		geo_bs.append((p1,p2))
		geo_bs.append((p2,p0))
	geo_vs = np.zeros((len(ref_shape),3), dtype=np.float32)
	geo_fs = []
	geo_ts = np.float32([[1,0,0,0],[0,1,0,1000],[0,0,1,0]])
	geo_vts = np.zeros_like(ref_shape)
	
	img_vs = np.float32([[-1000,-1000,0],[1000,-1000,0],[1000,1000,0],[-1000,1000,0]])
	img_fs = np.int32([[0,1,2,3]])
	img_ts = np.float32([[1,0,0,0],[0,1,0,1000],[0,0,1,0]])
	img_vts = np.float32([[0,1],[1,1],[1,0],[0,0]])
	markup_mesh = GLPoints3D(vertices=geo_vs, edges=np.int32(geo_bs), names=[], colour=[0,1,0,1],edgeColour=[1,1,1,1])
	geo_mesh = GLMeshes(names=['geo_mesh'],verts=[geo_vs],faces=[geo_fs],transforms=[geo_ts],bones=[geo_bs], vts=[geo_vts], colour=[1,0,0,1])
	image_mesh = GLMeshes(names=['image_mesh'],verts=[img_vs],faces=[img_fs],transforms=[img_ts],vts=[img_vts])

	global g_bs_vs, g_bs_shape_mat, g_bs_fs, g_bs_vts, g_bs_shape_mat_T
	bs_dict = IO.load(os.path.join(g_directory,'harpy_ma.out'))[1]['blendShapes']['Harpy_cFace_GEOShape']
	obj_scale = 10.0
	g_bs_vs = np.float32(bs_dict['vs']*obj_scale)
	bs_dict['pts'] = [b*obj_scale for b in bs_dict['pts']]
	g_bs_fs = bs_dict['fs'] # warning: mix of quads and triangles :-(
	assert bs_dict['vts'].keys() == range(len(bs_dict['vts'].keys()))
	g_bs_vts = bs_dict['vts'].values()
	g_bs_ts = np.float32([[1,0,0,800],[0,1,0,-600],[0,0,1,300]])
	bs_mesh = GLMeshes(names=['bs_mesh'],verts=[g_bs_vs],faces=[g_bs_fs],transforms=[g_bs_ts],vts=[g_bs_vts],visible=False)

	rbfn_groups, rbfn_slider_splits, rbfn_slider_names, rbfn_marker_names = extract_groups(g_rbfn)
	slider_names = [(x[8:-2]+'.translateY' if x.startswith('get_ty') else x) for x in bs_dict['wt_names']]
	try:
		slider_order = [slider_names.index(x) for x in rbfn_slider_names]
	except Exception as e:
		print 'error',e
		slider_order = []
	g_bs_shape_mat = bs_dict['matrix'] = np.zeros((len(bs_dict['pts']), len(bs_dict['vs']), 3),dtype=np.float32)
	for m,ct,pt in zip(g_bs_shape_mat,bs_dict['cts'],bs_dict['pts']): m[ct] = pt
	g_bs_shape_mat = g_bs_shape_mat[slider_order]
	g_bs_shape_mat_T = g_bs_shape_mat.transpose(1,2,0).copy()

	layers = {'image_mesh':image_mesh,'geo_mesh':geo_mesh,'bs_mesh':bs_mesh,'markup_mesh':markup_mesh}
	app,win = QGLViewer.makeApp()
	outliner = win.qoutliner
	#for gi,geo in enumerate(layers.keys()): outliner.addItem(geo, data='_OBJ_'+geo, index=gi)

	State.setKey('ui',{'type':'ui','attrs':{\
		'harpy_xoffset':300.0,'show_harpy':True,'rotate':0,'mirroring':False,'unreal':True,'streaming_TIS':False,\
		'using_webcam':False,'HMC_mode':True,'booting':True,'filtering':True,'setting_neutral':True,'debugging':False, \
		'webcam_index':0,'cam_offset':700,'cam_fps':50,'cam_width':1280,'cam_height':720, 'movie_filename':''}})
	if True: # running on deployed face machine at 720p50
		State.setKey('/root/ui',{'type':'ui','attrs':{\
			'harpy_xoffset':300.0,'show_harpy':False,'rotate':1,'mirroring':False,'unreal':True,'streaming_TIS':False,\
			'using_webcam':True,'HMC_mode':True,'booting':True,'filtering':True,'setting_neutral':True,'debugging':False, \
			'webcam_index':0,'cam_offset':700,'cam_fps':50,'cam_width':1280,'cam_height':720, 'movie_filename':''}})
	win.setFields('ui',     [
		('show_harpy',      'show_harpy','Whether to display the Harpy','bool', False),
		('harpy_xoffset',   'xoffset', 'Pixels to offset Harpy to right', 'float', 300.0),
		('rotate',          'rotation','Rotate image 0=up,1=left,2=down,3=right,-1=any angle','int', 0),
		('mirroring',       'mirror',  'Show reversed',                 'bool', False),
		('unreal',          'unreal',  'Whether to connect to unreal',  'bool', True),
		#('streaming_TIS',   'streaming_TIS',  'Whether currently streaming',   'bool', False),
		('using_webcam',    'webcam',  'Whether using the webcam',      'bool', False),
		('HMC_mode',        'HMC_mode','Boot every frame',              'bool', True),
		('booting',         'boot',    'Boot at next chance',           'bool', True),
		('filtering',       'filter',  'Whether to filter noise',       'bool', True),
		('setting_neutral', 'neutral', 'Set neutral at next chance',    'bool', False),
		('debugging',       'debug',   'Show rbfn input for debugging', 'bool', False),
		('webcam_index',    'camindex', 'The index of the webcam',      'int',  0),
		('cam_offset',      'camoffset','The offset of the webcam',     'int',  700),
		('cam_fps',         'fps',      'The frame rate of the webcam', 'int',  50),
		('cam_width',       'width',    'The width of the webcam image', 'int',  1280),
		('cam_height',      'height',   'The height of the webcam image', 'int',  720),
		('movie_filename',  'movie',   'The filename of the movie', 'string',  ''),
		])
	slider_names = sorted(g_rbfn['slider_names'])
	win.setFields('sliders', [(sn,sn,'Slider %d'%si,'float',0.0) for si,sn in enumerate(slider_names)])
	State.setKey('/root/sliders', {'type':'sliders','attrs':{sn:0.0 for sn in slider_names}})
	outliner.set_root('/root')
	#outliner.addItem('sliders', data='sliders', index=1)
	win.outliner.raise_()
	#win.select('ui')
	QApp.app.dirtyCB = dirty_cb
	QApp.app.addMenuItem({'menu':'&File','item':'Import &movie','tip':'Import a movie file','cmd':import_movie})
	QApp.app.addMenuItem({'menu':'&Edit','item':'Retrain rbfn','tip':'Train the rbfn','cmd':retrain_RBFN})
	QApp.app.addMenuItem({'menu':'&Edit','item':'Retrain rbfn (no linear)','tip':'Train the rbfn with no linear part','cmd':retrain_RBFN_no_linear})
	QApp.app.addMenuItem({'menu':'&Edit','item':'Retrack refresh rbfn','tip':'Refresh the rbfn','cmd':retrack_refresh_rbfn})
	QApp.app.addMenuItem({'menu':'&Edit','item':'Retrack remap rbfn','tip':'Rebuild the rbfn','cmd':retrack_remap_rbfn})
	QApp.app.addMenuItem({'menu':'&File','item':'Export rbfn','tip':'Export the rbfn','cmd':export_rbfn})
	State.clearUndoStack()
	QGLViewer.makeViewer(appName='StreamVideoTrack',timeRange=(0,100), callback=setFrame_cb, keyCallback=keypress_cb, layers=layers, mats=[Calibrate.makeMat(Calibrate.composeRT(np.eye(3)*[10,10,1],[0,1000,6000],1000),[0,0],[1920,1080])], camera_ids=['RBFN'])

	# Ensure the server has stopped when program terminates
	g_TIS_server.Stop()
Esempio n. 13
0
             [363.43197632, -315.17553711], [170.24447632, -407.59741211],
             [221.22885132, -405.47241211], [270.54135132, -409.98803711],
             [325.93197632, -398.12866211], [362.99447632, -379.26928711],
             [395.51010132, -350.64428711], [426.2131958, -314.92553711],
             [417.29135132, -288.36303711], [447.74447632, -274.65991211]],
            dtype=np.float32)
        head_pts = np.zeros((head_pts_bogons.shape[0], 3), dtype=np.float32)
        head_pts[:, 0] = mm_per_bogon * head_pts_bogons[:, 0]
        head_pts[:,
                 1] = -mm_per_bogon * head_pts_bogons[:, 1]  # our y-axis is up
        print head_pts
        head_pts += [150, -100, 0] - np.mean(head_pts, axis=0)
        #head_pts += [85,-193,0]
        head_pts = np.dot(head_pts - offset[:3, 3], offset[:3, :3])

        c3d_points = GLPoints3D([])
        surface_points = GLPoints3D([])
        head_points = GLPoints3D(head_pts)
        head_points.colour = (0, 1, 1, 1.0)

        # generate the animation
        if False:
            tsv_filename = 'tony_shape_vectors6'
            try:
                tony_shape_vectors = IO.load(tsv_filename)[1]
            except:
                tony_shape_vectors = np.zeros(
                    (len(c3d_frames), ted_lo_mat.shape[0]), dtype=np.float32)
                bnds = np.array([[0, 1]] * ted_lo_mat.shape[0],
                                dtype=np.float32)
                x_0 = np.zeros(ted_lo_mat.shape[0], dtype=np.float32)
Esempio n. 14
0
def drawX3ds(win, locationName, attrs, interface, picked):
    from UI import GLPoints3D

    if 'x3ds' not in attrs: return

    layerName = locationName
    if attrs['type'] == 'points': layerName += '_x3ds'
    if not win.hasLayer(layerName):
        layer = GLPoints3D([])
        win.setLayer(layerName, layer)

    layer = win.getLayer(layerName)

    if 'x3ds_pointSize' in attrs: layer.pointSize = attrs['x3ds_pointSize']
    else: layer.pointSize = 8

    if 'x3ds_colour' in attrs: layer.colour = attrs['x3ds_colour']
    else: layer.colour = (1, 0.5, 0, 0.7)

    colours = np.array([], dtype=np.float32)
    if 'x3ds_colours' in attrs and attrs['x3ds_colours'].any():
        colours = attrs['x3ds_colours']

    if 'drawStyle' in attrs and attrs['drawStyle']:
        layer.drawStyle = attrs['drawStyle']

    x3ds = np.array(attrs['x3ds'], dtype=np.float32)
    x3ds_labels = None
    if 'x3ds_labels' in attrs:
        x3ds_labels = attrs['x3ds_labels']
        layer.setData(x3ds,
                      names=Interface.getLabelNames(x3ds_labels),
                      colours=colours)
    else:
        layer.setData(x3ds, colours=colours)

    if layer: layer.visible = isVisible(attrs)

    if 'normals' in attrs:
        layer.normals = attrs['normals']

    if 'edges' in attrs and attrs['edges'] is not None:
        layer.edges = attrs['edges']
        # layer.graph = attrs['trackGraph']

    # Check if we want to draw the camera ray contributions
    # Make sure:
    #  - We have a 3D object that has been picked
    #  - The picked object is a 3D point
    #  - The picked layer matches the one we are processing
    if picked is not None and picked['type'] == '3d' and picked[
            'primitiveType'] == 'GLPoints3D' and win.view().layers.keys()[
                picked['primitiveIndex']] == layerName:
        if 'index' not in picked: return
        if picked['isLabel']:
            li = picked['index']
            if x3ds_labels is not None:
                liIdx = np.where(x3ds_labels == li)[0]
                xi = liIdx[0] if liIdx else li
            else:
                xi = li
        else:
            xi = picked['index']
            li = x3ds_labels[xi]

        if xi >= len(x3ds): return
        selected_x3d = x3ds[xi]

        # Create a yellow highlight around (behind) the picked 3D point
        highlightLayer = GLPoints3D(np.array([selected_x3d], dtype=np.float32),
                                    colour=(1, 1, 0, 0.9))
        highlightLayer.pointSize = layer.pointSize + 6.
        win.setLayer('x3d_selected', highlightLayer, selection=True)

        if selected_x3d.any() and 'showCameraContributions' in attrs and attrs[
                'showCameraContributions']:
            if 'camerasLocation' not in attrs: return
            if 'x3ds_labels' not in attrs: return
            if 'labels' not in attrs or 'x2ds_splits' not in attrs: return

            camsLoc = attrs['camerasLocation']
            cams = interface.location(camsLoc)
            if cams is None:
                print 'Render Callback: No cameras found when showing 3D point ray contributions.'
                return

            x2ds_labels = attrs['labels']
            x2ds_splits = attrs['x2ds_splits']
            camIds = [
                interface.findCameraIdFromRayId(rayId, x2ds_splits)
                for rayId in np.where(x2ds_labels == li)[0]
            ]

            camNames = [
                cam.name for ci, cam in enumerate(win.view().cameras[1:])
                if ci in camIds
            ]
            camPositions = np.array([m[4] for m in cams['mats']],
                                    dtype=np.float32)[camIds]
            print '3D Point', li, '|', 'Cameras:', camIds, camNames
            pts = [selected_x3d]
            pts.extend(camPositions)
            edges = [[0, idx + 1] for idx in range(len(camPositions))]
            selLayer = GLPoints3D(pts, edges=edges, colour=(0, 1, 1, 0.5))
            win.setLayer('x3d_cameraContributions', selLayer, selection=True)

            # Camera panels test
            if False:
                try:
                    if win.cameraPanels is None:
                        win.cameraPanels = QApp.CameraPanels(
                            mainView=win.view())

                    win.cameraPanels.resetCurrPos()
                    for c in camIds:
                        win.cameraPanels.setCameraLayer(c + 1)

                    win.cameraPanels.show()
                    win.cameraPanels.update()
                except Exception as e:
                    print e