def set_target(self, img, rect, landmarks=None): # rect: [x0, y0, x1, y1] self.template = self.crop(img, rect) self.rect = list(rect) m, n, nchannel = self.template.shape if landmarks is not None: self.landmarks = landmarks.copy() else: self.landmarks = distortion_util.create_landmarks(m, n, self.nSide) self.landmarks_global = self.landmarks.copy() self.landmarks_global[0, :, :] += rect[0] self.landmarks_global[1, :, :] += rect[1] def_spec = distortion_util.get_def_spec(self.template, self.landmarks, "landmark") alg_spec = distortion_util.get_hddd_parameters_fixed_layer( self.template, self.sigma, self.landmarks, 7, 0.7) # alg_spec = distortion_util.get_hddd_parameters(self.template, self.sigma, self.landmarks); deformation_pylib.InitializeDeformation(def_spec.SerializeToString()) print "GenerateDDDSamples" deformation_pylib.GenerateDDDSamples(self.template, alg_spec.SerializeToString())
def set_target(self, img, rect, landmarks=None): # rect: [x0, y0, x1, y1] self.template = self.crop(img, rect); self.rect = list(rect); m, n, nchannel = self.template.shape; if landmarks is not None: self.landmarks = landmarks.copy(); else: self.landmarks = distortion_util.create_landmarks(m, n, self.nSide) self.landmarks_global = self.landmarks.copy(); self.landmarks_global[0,:,:] += rect[0] self.landmarks_global[1,:,:] += rect[1] def_spec = distortion_util.get_def_spec(self.template, self.landmarks, "landmark"); alg_spec = distortion_util.get_hddd_parameters_fixed_layer(self.template, self.sigma, self.landmarks, 7, 0.7) # alg_spec = distortion_util.get_hddd_parameters(self.template, self.sigma, self.landmarks); deformation_pylib.InitializeDeformation(def_spec.SerializeToString()); print "GenerateDDDSamples" deformation_pylib.GenerateDDDSamples(self.template, alg_spec.SerializeToString());
def test_data_driven_descent(img, landmarks, distortion_sigma): # Landmark is set def_spec = distortion_util.get_def_spec(img, landmarks, "landmark") #alg_spec = get_ddd_parameters(def_spec.dof, distortion_sigma, landmarks) alg_spec = distortion_util.get_hddd_parameters_fixed_layer( img, distortion_sigma, landmarks, 7, 0.7) alg_spec.dump_intermediate = True deformation_pylib.InitializeDeformation(def_spec.SerializeToString()) print "GenerateDDDSamples" deformation_pylib.GenerateDDDSamples(img, alg_spec.SerializeToString()) while True: deform = numpy.zeros(img.shape, dtype='f4') p = numpy.random.normal(0, distortion_sigma, (1, def_spec.dof)).astype('f4') #p = numpy.array([5.0] * def_spec.dof).astype('f4'); deformation_pylib.SetParameters(p) deformation_pylib.DeformImage(img, deform) import pdb pdb.set_trace() print "Run Data-Driven Descent" #cProfile.runctx("result = run_ddd(deform)", {"deform" : deform, "run_ddd" : run_ddd}, {}); result = run_ddd(deform) # print the result print "Gt:", p print "Estimated:", result.estimates # show the debug images rows = 4 cols = 4 fig, axes = plt.subplots(rows, cols, sharex='col', sharey='row') for frame in result.frames: count = frame.t # if frame.t % 2 == 1: continue; # if c >= 25: break; rectified_img = numpy.array(frame.representation) rectified_img.shape = (img.shape[0], img.shape[1], 3) row = count / cols col = count % cols #axes[row][col].imshow(rectified_img, interpolation='nearest') axes[row][col].imshow(deform, interpolation='nearest') axes[row][col].set_title("iteration = %d" % frame.t) p = numpy.array(frame.estimates).reshape(landmarks.shape) plot_landmarks(axes[row][col], landmarks, delta=p) axes[-1][-1].imshow(img, interpolation='nearest') axes[-1][-1].set_title("Template") plot_landmarks(axes[-1][-1], landmarks) plt.show() key = raw_input("Press Enter to continue...") print int(key) if key != '\n': break
def test_data_driven_descent(img, landmarks, distortion_sigma): # Landmark is set def_spec = distortion_util.get_def_spec(img, landmarks, "landmark") #alg_spec = get_ddd_parameters(def_spec.dof, distortion_sigma, landmarks) alg_spec = distortion_util.get_hddd_parameters_fixed_layer(img, distortion_sigma, landmarks, 7, 0.7); alg_spec.dump_intermediate = True; deformation_pylib.InitializeDeformation(def_spec.SerializeToString()); print "GenerateDDDSamples" deformation_pylib.GenerateDDDSamples(img, alg_spec.SerializeToString()); while True: deform = numpy.zeros(img.shape, dtype='f4'); p = numpy.random.normal(0, distortion_sigma, (1, def_spec.dof)).astype('f4'); #p = numpy.array([5.0] * def_spec.dof).astype('f4'); deformation_pylib.SetParameters(p); deformation_pylib.DeformImage(img, deform); import pdb; pdb.set_trace(); print "Run Data-Driven Descent" #cProfile.runctx("result = run_ddd(deform)", {"deform" : deform, "run_ddd" : run_ddd}, {}); result = run_ddd(deform); # print the result print "Gt:", p print "Estimated:", result.estimates # show the debug images rows = 4; cols = 4; fig, axes = plt.subplots(rows, cols, sharex='col', sharey='row') for frame in result.frames: count = frame.t; # if frame.t % 2 == 1: continue; # if c >= 25: break; rectified_img = numpy.array(frame.representation); rectified_img.shape = (img.shape[0], img.shape[1], 3); row = count / cols; col = count % cols; #axes[row][col].imshow(rectified_img, interpolation='nearest') axes[row][col].imshow(deform, interpolation='nearest') axes[row][col].set_title("iteration = %d" % frame.t); p = numpy.array(frame.estimates).reshape(landmarks.shape); plot_landmarks(axes[row][col], landmarks, delta=p) axes[-1][-1].imshow(img, interpolation='nearest'); axes[-1][-1].set_title("Template"); plot_landmarks(axes[-1][-1], landmarks) plt.show(); key = raw_input("Press Enter to continue...") print int(key) if key != '\n': break;