class NoGuiRunner: def __init__(self, args): self._cw = CWrapper() self._args = args path = args.path self._grid = None self._image = None self._handles = None self.load_image(path) def run(self): self._grid = Grid(self._cw, self._image, self._args, gui=False) kpt_files = os.listdir(self._args.keypoints_dir) kpt_files.sort(key=lambda e: int(e.split('_')[0])) self._handles = self.add_bunch(os.path.join(self._args.keypoints_dir, kpt_files[0])) if not os.path.exists(self._args.output_dir): os.mkdir(self._args.output_dir) pose_num = 0 cv2.imwrite(os.path.join(self._args.output_dir, '{}.png'.format(pose_num)), self._image._data[:, :, ::-1]) for pose in tqdm(kpt_files[1:]): self.move_bunch(pose) pose_num += 1 for _ in range(self._args.num_iterations): self.run_once() cv2.imwrite(os.path.join(self._args.output_dir, '{}.png'.format(pose_num)), self._image._data[:, :, ::-1]) def run_once(self): # no time delay left self._grid.regularize() self._grid.project() def load_image(self, path): self._image = ImageHelper(self._cw, self._args, gui=False) def add_bunch(self, posepath): with open(posepath, 'r') as f: poss = json.load(f) kpts = poss['people'][0]['pose_keypoints_2d'] xs = kpts[::3] ys = kpts[1::3] new_handles = {} i = 0 for ptx, pty in zip(xs, ys): new_handles[i] = (ptx, pty) i += 1 self._grid.create_bunch_cp(new_handles=new_handles) return new_handles def move_bunch(self, filename): with open(os.path.join(self._args.keypoints_dir, filename)) as f: poss = json.load(f) newpos = poss['people'][0]['pose_keypoints_2d'] xs = newpos[::3] ys = newpos[1::3] for i, h_obj in self._handles.items(): self._grid.set_control_target(i, xs[i], ys[i])
class Application: def __init__(self, path): self._cw = CWrapper() self._window = tk.Tk() self._grid = None self._image = None self.load_image(path) self._canvas = tk.Canvas(self._window, width=self._image.width, height=self._image.height) self._canvas.pack() self._image.canvas = self._canvas self._active_handle = -1 self._loop = None self._t_last = 0 def load_image(self, path): self._image = ImageHelper(self._cw, path) def bind(self, event, fn): self._canvas.bind(event, fn) def run(self): self._grid = Grid(self._cw, self._image) self._image.draw() self._grid.draw() self._run_once() self._window.mainloop() def _run_once(self): self._grid.regularize() dt = datetime.now() delta = dt.timestamp() - self._t_last if 0 < delta > 0.03: # 0.03 - 30 FPS # dt = datetime.now() # t1 = dt.timestamp() self._grid.project() #dt = datetime.now() # print(dt.timestamp()-t1) self._image.draw() self._grid.draw() dt = datetime.now() self._t_last = dt.timestamp() self._loop = self._window.after(1, self._run_once) def select_handle(self, e): handle_id = self._image.select_handle(e.x, e.y) if handle_id == -1: handle_id = self._image.create_handle(e.x, e.y) if handle_id != -1: if not self._grid.create_control_point(handle_id, e.x, e.y): self._image.remove_handle(handle_id) return False else: return False self._active_handle = handle_id return True def deselect_handle(self, e): self._active_handle = -1 def remove_handle(self, e): handle_id = self._image.select_handle(e.x, e.y) if handle_id != -1: self._grid.remove_control_point(handle_id) self._image.remove_handle(handle_id) def move_handle(self, e): if self._active_handle != -1: self._image.move_handle(self._active_handle, e.x, e.y) self._grid.set_control_target(self._active_handle, e.x, e.y)
class Application: def __init__(self, path): self._cw = CWrapper() self._window = tk.Tk() self._grid = None self._image = None self.load_image(path) self._canvas = tk.Canvas(self._window, width=self._image.width, height=self._image.height) self._canvas.pack() self._image.canvas = self._canvas self._active_handle = -1 self._loop = None self._t_last = 0 def load_image(self, path): self._image = ImageHelper(self._cw, path) def bind(self, event, fn): self._canvas.bind(event, fn) def run(self): self._grid = Grid(self._cw, self._image) self._image.draw() self._grid.draw() self._run_once() self._window.mainloop() def _run_once(self): self._grid.regularize() dt = datetime.now() delta = dt.timestamp()-self._t_last if 0 < delta > 0.03: # 0.03 - 30 FPS # dt = datetime.now() # t1 = dt.timestamp() self._grid.project() #dt = datetime.now() # print(dt.timestamp()-t1) self._image.draw() self._grid.draw() dt = datetime.now() self._t_last = dt.timestamp() self._loop = self._window.after(1, self._run_once) def select_handle(self, e): handle_id = self._image.select_handle(e.x, e.y) if handle_id == -1: handle_id = self._image.create_handle(e.x, e.y) if handle_id != -1: if not self._grid.create_control_point(handle_id, e.x, e.y): self._image.remove_handle(handle_id) return False else: return False self._active_handle = handle_id return True def deselect_handle(self, e): self._active_handle = -1 def remove_handle(self, e): handle_id = self._image.select_handle(e.x, e.y) if handle_id != -1: self._grid.remove_control_point(handle_id) self._image.remove_handle(handle_id) def move_handle(self, e): if self._active_handle != -1: self._image.move_handle(self._active_handle, e.x, e.y) self._grid.set_control_target(self._active_handle, e.x, e.y)