class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) # Store temporary configurations self._cpath = os.path.join(QtCore.QDir.homePath(), '.dataviewer.db') self._set_codec("UTF-8") self._cur_pid = None self._cur_pedes = None self._cur_index = None self._create_panels() self._create_docks() self._create_menus() self.setWindowTitle("Person Re-id Dataset Viewer") self.showMaximized() def open(self): fpath, __ = QtGui.QFileDialog.getOpenFileName(self, "Open File", self._prevdir, "Matlab File (*.mat)") if not fpath: return self._prevdir = QtCore.QFileInfo(fpath).absolutePath() # TODO: Handle errors self._data = DataLoader(fpath, verbose=True) # TODO: Check if we should manually delete it self._tree_dock = QtGui.QTreeView(self._dock) self._tree_dock.setModel(DataTreeModel(self._data, self._tree_dock)) self._tree_dock.setColumnWidth(0, 200) self._tree_dock.setColumnWidth(1, 100) self._tree_dock.doubleClicked[QtCore.QModelIndex].connect(self.display) self._dock.setWidget(self._tree_dock) self._gallery_panel.clear() def display(self, index): gid = index.parent().row() pid = index.row() if gid >= 0 and pid >= 0: # gid is -1 when double click a group pedes = self._data.get_pedes(gid) self._cur_pid = pid self._cur_pedes = pedes self._cur_index = index self._gallery_panel.show_pedes(pedes[pid, :]) else: self._cur_pid = None self._cur_pedes = None self._cur_index = None def next_pedes(self): if self._cur_pedes is None or self._cur_pid is None: return if self._cur_pid + 1 >= self._cur_pedes.shape[0]: msg = QtGui.QMessageBox() msg.setText("Reach the end of the group") msg.exec_() else: self._cur_pid += 1 self._gallery_panel.show_pedes(self._cur_pedes[self._cur_pid, :]) next_index = self._cur_index.sibling(self._cur_pid, 0) self._tree_dock.setCurrentIndex(next_index) self._cur_index = next_index def prev_pedes(self): if self._cur_pedes is None or self._cur_pid is None: return if self._cur_pid - 1 < 0: msg = QtGui.QMessageBox() msg.setText("Reach the beginning of the group") msg.exec_() else: self._cur_pid -= 1 self._gallery_panel.show_pedes(self._cur_pedes[self._cur_pid, :]) prev_index = self._cur_index.sibling(self._cur_pid, 0) self._tree_dock.setCurrentIndex(prev_index) self._cur_index = prev_index @property def _prevdir(self): d = shelve.open(self._cpath) ret = d['prevdir'] if 'prevdir' in d else QtCore.QDir.homePath() d.close() return ret @_prevdir.setter def _prevdir(self, prevdir): d = shelve.open(self._cpath) d['prevdir'] = prevdir d.close() def _set_codec(self, codec_name): codec = QtCore.QTextCodec.codecForName(codec_name) QtCore.QTextCodec.setCodecForLocale(codec) QtCore.QTextCodec.setCodecForCStrings(codec) QtCore.QTextCodec.setCodecForTr(codec) def _create_panels(self): self._gallery_panel = PedesGallery(self) self.setCentralWidget(self._gallery_panel) def _create_docks(self): self._dock = QtGui.QDockWidget(self) self._dock.setAllowedAreas(Qt.LeftDockWidgetArea) self._dock.setFeatures(QtGui.QDockWidget.NoDockWidgetFeatures) self._dock.setMinimumWidth(350) self.addDockWidget(Qt.LeftDockWidgetArea, self._dock) def _create_menus(self): # Actions open_act = QtGui.QAction("Open", self) open_act.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Open)) open_act.triggered.connect(self.open) next_pedes_act = QtGui.QAction("Next Pedestrian", self) next_pedes_act.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Forward)) next_pedes_act.triggered.connect(self.next_pedes) prev_pedes_act = QtGui.QAction("Prev Pedestrian", self) prev_pedes_act.setShortcut(QtGui.QKeySequence(QtGui.QKeySequence.Back)) prev_pedes_act.triggered.connect(self.prev_pedes) # Menu Bar menubar = self.menuBar() fileMenu = menubar.addMenu("&File") fileMenu.addAction(open_act) # Tool Bar toolbar = self.addToolBar("Toolbar") toolbar.addAction(next_pedes_act) toolbar.addAction(prev_pedes_act)
def _prepare_data(load_from_cache=False, save_to_cache=False): if load_from_cache: with open(_cached_datasets, 'rb') as f: views_data, datasets = cPickle.load(f) else: image_data = DataLoader('../data/cuhk_small_masked.mat', verbose=True) # Prepare the view-first order data representation print "Preparing the view-first order data ..." n_pedes, n_views = [], [] for gid in xrange(image_data.get_n_groups()): m, v = image_data.get_n_pedes_views(gid) n_pedes.append(m) n_views.append(v) assert min(n_views) == max(n_views), \ "The number of views in each group should be equal" v = n_views[0] views_data = [[] for __ in xrange(v)] for gid in xrange(image_data.get_n_groups()): bias = sum(n_pedes[0:gid]) group_data = data_manager.view_repr(image_data.get_pedes(gid)) for vid in xrange(v): view_data = group_data[vid] view_data = [(pid+bias, image) for pid, image in view_data] views_data[vid].extend(view_data) # Prepare the datasets print "Prepare the datasets ..." X, Y = [], [] for gid in xrange(image_data.get_n_groups()): m, v = image_data.get_n_pedes_views(gid) for pid in xrange(m): n_images = image_data.get_n_images(gid, pid) for vi in xrange(v): for vj in xrange(vi+1, v): for i in xrange(n_images[vi]): for j in xrange(n_images[vj]): X.append(_preproc( image_data.get_image(gid, pid, vi, i))) Y.append(_preproc( image_data.get_image(gid, pid, vj, j))) X = imageproc.images2mat(X).astype(theano.config.floatX) Y = imageproc.images2mat(Y).astype(theano.config.floatX) datasets = Datasets(X, Y) datasets.split(train_ratio=0.8, valid_ratio=0.1) if save_to_cache: with open(_cached_datasets, 'wb') as f: cPickle.dump((views_data, datasets), f, protocol=cPickle.HIGHEST_PROTOCOL) return (views_data, datasets)
def _prepare_data(load_from_cache=False, save_to_cache=False): if load_from_cache: with open(_cached_datasets, 'rb') as f: views_data, datasets = cPickle.load(f) else: image_data = DataLoader('../data/cuhk_small_masked.mat', verbose=True) # Prepare the view-first order data representation print "Preparing the view-first order data ..." n_pedes, n_views = [], [] for gid in xrange(image_data.get_n_groups()): m, v = image_data.get_n_pedes_views(gid) n_pedes.append(m) n_views.append(v) assert min(n_views) == max(n_views), \ "The number of views in each group should be equal" v = n_views[0] views_data = [[] for __ in xrange(v)] for gid in xrange(image_data.get_n_groups()): bias = sum(n_pedes[0:gid]) group_data = data_manager.view_repr(image_data.get_pedes(gid)) for vid in xrange(v): view_data = group_data[vid] view_data = [(pid + bias, image) for pid, image in view_data] views_data[vid].extend(view_data) # Prepare the datasets print "Prepare the datasets ..." X, Y = [], [] for gid in xrange(image_data.get_n_groups()): m, v = image_data.get_n_pedes_views(gid) for pid in xrange(m): n_images = image_data.get_n_images(gid, pid) for vi in xrange(v): for vj in xrange(vi + 1, v): for i in xrange(n_images[vi]): for j in xrange(n_images[vj]): X.append( _preproc( image_data.get_image(gid, pid, vi, i))) Y.append( _preproc( image_data.get_image(gid, pid, vj, j))) X = imageproc.images2mat(X).astype(theano.config.floatX) Y = imageproc.images2mat(Y).astype(theano.config.floatX) datasets = Datasets(X, Y) datasets.split(train_ratio=0.8, valid_ratio=0.1) if save_to_cache: with open(_cached_datasets, 'wb') as f: cPickle.dump((views_data, datasets), f, protocol=cPickle.HIGHEST_PROTOCOL) return (views_data, datasets)