def images_shapes_not_all_labels_unmarked(): images = State.getKey('/images') shapes = State.getKey('/shapes') labels = State.getKey('/labels') which = np.where(np.sum(np.int32(labels) == 0, axis=1) == 0)[0] return [JPEG.decompress(images[x]) for x in which ], [shapes[x] for x in which], [labels[x] for x in which]
def buildOps(self, filename, win): # Check if the state contains a recipe as we can't continue without it since the order (graph) is unknown if not State.hasKey('/recipe'): return # Get the recipe and available ops recipe = State.getKey('/recipe') registeredOps = Registry.getRegisteredOps() # Go through the recipe in order for op in recipe: opName, opType = op # Look up the corresponding State data and check if the type corresponds to a registered Op opTypeKey = opName + '/type' if not State.hasKey(opName) or not State.hasKey(opTypeKey): return opType_state = State.getKey(opTypeKey)[1:] if opType_state not in registeredOps: continue # Get the Op from the registration factory and create the op (without duplicating the State entry) self._addOp(registeredOps[opType_state], win, initialiseState=False, name=opName) self.interface.reset() self.cookOps(win, self.getFrame(), forceRecook=True) if win: win.updateGL() win.updateLayers()
def updateFieldKey(self, key): #print 'updateFieldKey',key if not State.hasKey(key): return # TODO maybe need to remove something from UI sel = State.getSel() if sel is None: return self.qfields.setKeyValue(key, State.getKey(key)) s = '/%s/attrs/'%sel.strip('/') #print 'updateFieldKey',s,key if key.startswith(s): self.qfields.setKeyValue(key[len(s):], State.getKey(key))
def delete_image(): fi = frame_number images = State.getKey('/images', []) shapes = State.getKey('/shapes', []) labels = State.getKey('/labels', []) images.pop(fi) shapes.pop(fi) labels.pop(fi) State.setKey('/images', images) State.setKey('/shapes', shapes) State.setKey('/labels', labels) State.push('delete image')
def getAttrs(self, location=None, onlyDirty=False): if location is None: location = self.name op = State.getKey(location) if onlyDirty: op = State.getKey(location) attrs = { name: value for name, value in op['attrs'].iteritems() if self.isDirty(name) } return attrs return op['attrs']
def delete_selected_vertex(): vi = get_selected_vertex() shapes = State.getKey('/shapes') labels = State.getKey('/labels') vnames = State.getKey('/vnames') num_images, num_vertices = labels.shape which = range(0, vi) + range(vi + 1, num_vertices()) shapes = list(np.float32(shapes)[:, which]) labels = list(np.int32(labels)[:, which]) vnames = [vnames[x] for x in which] State.setKey('/shapes', shapes) State.setKey('/labels', labels) State.setKey('/vnames', vnames) State.push('delete vertex')
def train(): images, shapes, labels = images_shapes_not_all_labels_unmarked() flip = State.getKey('/flip', None) Face.double_data(images, shapes, flip_order=flip) predictor = Face.train_shape_predictor(images, shapes) State.setKey('/predictor', predictor) State.push('train predictor')
def updateMenus(self): '''Keeps the GUI menus and the attribute editor in sync with the actual state.''' undoCmd = State.getUndoCmd() self.undoItem.setText('&Undo' if undoCmd is None else '&Undo [%s]' % undoCmd) redoCmd = State.getRedoCmd() self.redoItem.setText('Re&do' if redoCmd is None else 'Re&do [%s]' % redoCmd) #print undoCmd, redoCmd if self.attributeEditor is not None: sel = State.getSel() if sel is None: self.qfields.setFields('',[],{}) else: st = State.getKey(sel+'/type',None) sa = State.getKey(sel+'/attrs',{}) global fields self.qfields.setFields(sel, fields.get(st,[]), sa) self.attributeEditor.setWidget(self.qfields) self.updateGL()
def add_vertex(vname=None): # TODO later, we want to initialise every vertex that has a zero label shapes = State.getKey('/shapes') labels = State.getKey('/labels') vnames = State.getKey('/vnames') if vname is None: vname = 'pt_%d' % len(vnames) shapes = np.float32(shapes) labels = np.int32(labels) num_images, num_vertices = labels.shape shapes = list( np.hstack((shapes, np.zeros((num_images, 1, 2), dtype=np.float32)))) labels = list( np.hstack((labels, np.zeros((num_images, 1), dtype=np.int32)))) vnames.append(vname) State.setKey('/shapes', shapes) State.setKey('/labels', labels) State.setKey('/vnames', vnames) State.push('add vertex')
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 train_aam(): images, shapes, labels = images_shapes_not_all_labels_unmarked() flip = State.getKey('/flip', None) aam_model = Face.train_aam_model(images, shapes, flip_order=flip, texture_rank=20) State.setKey('/aam', aam_model) State.push('train aam')
def update_flip_order_last2(): '''A hacky method that makes the last two added points be each others flip.''' flip = State.getKey('/flip', [16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,\ 26,25,24,23,22,21,20,19,18,17,27,28,29,30,35,34,33,32,31,45,44,43,42,47,46,\ 39,38,37,36,41,40,54,53,52,51,50,49,48,59,58,57,56,55,64,63,62,61,60,67,66,65]) flip = list(flip) flip.extend([len(flip) + 1, len(flip)]) print flip flip = np.int32(flip) State.setKey('/flip', flip) State.push('update flip last 2')
def reorder_images(): images = State.getKey('/images') shapes = State.getKey('/shapes') labels = State.getKey('/labels') old_order = State.getKey('/order', np.arange(len(images), dtype=np.int32)) norm_shapes, ref_shape, ref_pinv = Face.normalized_shapes(shapes) norm_shapes -= ref_shape shapes_u, shapes_s, shapes_vt = np.linalg.svd(norm_shapes.reshape( norm_shapes.shape[0], -1), full_matrices=0) wts = np.sum(shapes_u**2, axis=1) order = np.argsort(wts)[::-1] images = [images[o] for o in order] shapes = [shapes[o] for o in order] labels = [labels[o] for o in order] State.setKey('/images', images) State.setKey('/shapes', shapes) State.setKey('/labels', labels) State.setKey('/order', old_order[order]) State.push('reorder images')
def keypress_cb(view, key): if key == ord('R'): State.setKey('/root/ui/attrs/booting',True) State.push('boot face') if key == ord('N'): State.setKey('/root/ui/attrs/setting_neutral',True) State.push('set neutral') if key == ord('S'): toggle_unreal() if key == ord('Z'): State.setKey('/root/ui/attrs/debugging',not State.getKey('/root/ui/attrs/debugging')) State.push('debugging')
def toggle_unreal(): global g_TIS_server streaming_TIS = State.getKey('/root/ui/attrs/streaming_TIS') if streaming_TIS: g_TIS_server.Stop() State._setKey('/root/ui/attrs/streaming_TIS', False) print "Stopping Server" else: print 'Trying to start Server' if g_TIS_server.Start('',6500): State._setKey('/root/ui/attrs/streaming_TIS', True) else: print 'ARGH!!! Failed to start server'
def add_image(img, shp=None): if shp is None: shp = get_predictor()['ref_shape'] if not State.hasKey('/images') or not State.hasKey( '/shapes') or not State.hasKey('/labels'): State.setKey('/images', []) State.setKey('/shapes', []) State.setKey('/labels', []) fi = len(State.getKey('/images')) lbl = np.zeros(len(shp), dtype=np.int32) # unlabelled set_frame_image(fi, img) set_frame_markup(fi, shp) set_frame_labels(fi, lbl) State.push('add image')
def dirtyCB(dirty): # triggered by a state change, this allows the app to synch with the state # TODO this is the correct place to deal with changes due to eg menu items or hot keys # should be able to remove eg updateGL from everywhere else, really #print 'dirty',dirty if dirty: outliner = QApp.app.qoutliner outliner.set_root(outliner.root) # TODO this causes a total rebuild global g_predictor, g_aam_model if '/predictor' in dirty: g_predictor = None if '/aam' in dirty: g_aam_model = None if '/vnames' in dirty: #print 'setting names',State.getKey('/vnames') QApp.view().getLayer('markup_mesh').names = State.getKey('/vnames', []) if '/markup_mesh_sel' in dirty: QApp.view().getLayer('markup_mesh').selectedIndex = State.getKey( '/markup_mesh_sel', -1) if '/edges' in dirty: QApp.view().getLayer('markup_mesh').edges = State.getKey( '/edges', None) QApp.view().getLayer('ref_mesh').edges = State.getKey('/edges', None)
def get_frame_image(fi): img = State.getKey('/images/%d' % fi, None) if isinstance(img, np.ndarray): print 'compressing image', fi set_frame_image(fi, img) img = JPEG.decompress(img) if img is not None and (len(img.shape) != 3 or img.shape[2] != 3 or img.dtype != np.uint8): print 'repairing img', fi, img.shape, img.dtype ret = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) ret[:, :, :3] = img.reshape(img.shape[0], img.shape[1], -1)[:, :, :3] img = ret print 'repaired img', fi, img.shape, img.dtype set_frame_image(fi, img) return img
def getAttr(self, attrName, defaultValue=None, location=None, onlyDirty=False): if location is None: location = self.name if not State.hasKey(location): return defaultValue op = State.getKey(location) if not op or 'attrs' not in op: return defaultValue attrs = op['attrs'] if not attrName in attrs: return defaultValue if onlyDirty and not self.isDirty(attrName, location): return defaultValue return attrs[attrName]
def save_object(desc, filetype, key): print 'exporting', desc aam_fn, _ = QApp.app.saveFilename('Choose a file to write ' + desc, cwd(), filetype) IO.save(aam_fn, State.getKey(key))
def cwd(): cwd = State.getKey('/cwd', None) if cwd is None: cwd = os.environ['GRIP_DATA'] + '/' return cwd
def get_frame_markup(fi=None): if fi is None: fi = frame_number() shp = State.getKey('/shapes/%d' % fi, None) labels = State.getKey('/labels/%d' % fi, None) return shp, labels
def getFrame(self): return State.getKey("frame")
def get_predictor(): global g_predictor if g_predictor is None: g_predictor = State.getKey('/predictor', None) return g_predictor
def num_vertices(): # this is the number of vertices in the DB; it could be different from the number of vertices in the predictor or aam... return len(State.getKey('/labels/0', []))
def num_aam_vertices(): return len(State.getKey('/aam/ref_shape', []))
def get_aam(): global g_aam_model if g_aam_model is None: g_aam_model = State.getKey('/aam', None) return g_aam_model
def frame_number(): return State.getKey('/frame_number', 0)
def num_predictor_vertices(): return len(State.getKey('/predictor/ref_shape', []))
def get_selected_vertex(): return State.getKey('/markup_mesh_sel', -1)