def view_combined_model(shp_fn, txt_fn, cmb_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') # cv2.namedWindow('combined model') tm = TextureMapper(img.shape[1], img.shape[0]) smodel = ShapeModel.load(shp_fn) tmodel = TextureModel.load(txt_fn) cmodel = CombinedModel.load(cmb_fn) vals = genvals() params = smodel.get_params(scale, tranx, trany) ref = smodel.calc_shape(params) ref = ref.reshape((ref.size // 2, 2)) verts = get_vertices(ref) while True: for k in range(cmodel.num_modes()): for v in vals: p = np.zeros(cmodel.num_modes()) p[k] = v * 3 * np.sqrt(cmodel.variance[k]) sparams, tparams = cmodel.calc_shp_tex_params(p, smodel.num_modes()) params[4:] = sparams shp = smodel.calc_shape(params) shp = shp.reshape(ref.shape) t = tmodel.calc_texture(tparams) img[:] = 0 draw_texture(img, t, ref) warped = tm.warp_triangles(img, ref[verts], shp[verts]) s = 'mode: %d, val: %f sd' % (k, v * 3) draw_string(warped, s) print('#####') # cv2.imshow('combined model', warped) cv2.imwrite('output/%d.jpg'%(int(time.time()*1000000)), warped)
def view_combined_model(shp_fn, txt_fn, cmb_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') cv2.namedWindow('combined model') tm = TextureMapper(img.shape[1], img.shape[0]) smodel = ShapeModel.load(shp_fn) tmodel = TextureModel.load(txt_fn) cmodel = CombinedModel.load(cmb_fn) vals = genvals() params = smodel.get_params(scale, tranx, trany) ref = smodel.calc_shape(params) ref = ref.reshape((ref.size//2, 2)) verts = get_vertices(ref) while True: for k in xrange(cmodel.num_modes()): for v in vals: p = np.zeros(cmodel.num_modes()) p[k] = v * 3 * np.sqrt(cmodel.variance[k]) sparams, tparams = cmodel.calc_shp_tex_params(p, smodel.num_modes()) params[4:] = sparams shp = smodel.calc_shape(params) shp = shp.reshape(ref.shape) t = tmodel.calc_texture(tparams) img[:] = 0 draw_texture(img, t, ref) warped = tm.warp_triangles(img, ref[verts], shp[verts]) s = 'mode: %d, val: %f sd' % (k, v*3) draw_string(warped, s) cv2.imshow('combined model', warped) if cv2.waitKey(10) == 27: sys.exit()
def view_patches_model(ptc_fn, shp_fn, width): pmodel = PatchesModel.load(ptc_fn) smodel = ShapeModel.load(shp_fn) ref = pmodel.ref_shape ref = np.column_stack((ref[::2], ref[1::2])) # compute scale factor scale = width / ref[:, 0].ptp() height = int(scale * ref[:, 1].ptp() + 0.5) # compute image width max_height = int(scale * pmodel.patches.shape[1]) max_width = int(scale * pmodel.patches.shape[2]) # create reference image image_size = (height + 4 * max_height, width + 4 * max_width, 3) image = np.empty(image_size, dtype='uint8') image[:] = 192 patches = [] points = [] for i in xrange(len(pmodel.patches)): im = cv2.normalize(pmodel.patches[i], alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) im = cv2.resize(im, (int(scale * im.shape[0]), int(scale * im.shape[1]))) im = im.astype('uint8') patches.append(cv2.cvtColor(im, cv.CV_GRAY2BGR)) h, w = patches[i].shape[:2] points.append((int(scale * ref[i, 1] + image_size[0] / 2 - h / 2), int(scale * ref[i, 0] + image_size[1] / 2 - w / 2))) y, x = points[i] image[y:y + h, x:x + w, :] = patches[i] cv2.namedWindow('patches model') i = 0 while True: img = image.copy() y, x = points[i] h, w = patches[i].shape[:2] img[y:y + h, x:x + w, :] = patches[i] # draw current patch on top cv2.rectangle(img, (x, y), (x + w, y + h), Color.red, 2, cv2.CV_AA) text = 'patch %d' % (i + 1) draw_string(img, text) cv2.imshow('patches model', img) c = cv2.waitKey(0) if c == 27: break elif c == ord('j'): i += 1 elif c == ord('k'): i -= 1 if i < 0: i = 0 elif i >= len(pmodel.patches): i = len(pmodel.patches) - 1
def draw(self, flip=False, points=False, line=False, pairs=False): name = self.name_flip if flip else self.name img = self.img_flip if flip else self.img lmks = self.lmks_flip if flip else self.lmks # draw on image copy img = img.copy() # prepare points pts = np.column_stack((lmks[::2], lmks[1::2])) # draw draw_string(img, name) if line: draw_line(img, pts, Color.blue) if pairs: draw_pairs(img, pts, MuctDataset.PAIRS, Color.red) if points: draw_points(img, pts, Color.green) return img
def draw(self, flip=False, points=False, line=False, pairs=False): name = self.name_flip if flip else self.name img = self.img_flip if flip else self.img lmks = self.lmks_flip if flip else self.lmks # draw on image copy img = img.copy() # prepare points pts = np.column_stack((lmks[::2], lmks[1::2])) # draw draw_string(img, name) if line: draw_line(img, pts, Color.blue) if pairs: draw_pairs(img, pts, MuctDataset.PAIRS, Color.red) if points: draw_points(img, pts, Color.green) return img
def view_patches_model(ptc_fn, shp_fn, width): pmodel = PatchesModel.load(ptc_fn) smodel = ShapeModel.load(shp_fn) ref = pmodel.ref_shape ref = np.column_stack((ref[::2], ref[1::2])) # compute scale factor scale = width / ref[:,0].ptp() height = int(scale * ref[:,1].ptp() + 0.5) # compute image width max_height = int(scale * pmodel.patches.shape[1]) max_width = int(scale * pmodel.patches.shape[2]) # create reference image image_size = (height+4*max_height, width+4*max_width, 3) image = np.empty(image_size, dtype='uint8') image[:] = 192 patches = [] points = [] for i in xrange(len(pmodel.patches)): im = cv2.normalize(pmodel.patches[i], alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) im = cv2.resize(im, (int(scale*im.shape[0]), int(scale*im.shape[1]))) im = im.astype('uint8') patches.append(cv2.cvtColor(im, cv.CV_GRAY2BGR)) h,w = patches[i].shape[:2] points.append((int(scale*ref[i,1] + image_size[0]/2 - h/2), int(scale*ref[i,0] + image_size[1]/2 - w/2))) y,x = points[i] image[y:y+h,x:x+w,:] = patches[i] cv2.namedWindow('patches model') i = 0 while True: img = image.copy() y,x = points[i] h,w = patches[i].shape[:2] img[y:y+h,x:x+w,:] = patches[i] # draw current patch on top cv2.rectangle(img, (x,y), (x+w, y+h), Color.red, 2, cv2.CV_AA) text = 'patch %d' % (i+1) draw_string(img, text) cv2.imshow('patches model', img) c = cv2.waitKey(0) if c == 27: break elif c == ord('j'): i += 1 elif c == ord('k'): i -= 1 if i < 0: i = 0 elif i >= len(pmodel.patches): i = len(pmodel.patches) - 1
def view_shape_model(shp_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') smodel = ShapeModel.load(shp_fn) vals = genvals() while True: for k in xrange(4, smodel.model.shape[1]): for v in vals: p = smodel.get_params(scale, tranx, trany) p[k] = p[0] * v * 3 * np.sqrt(smodel.variance[k]) img[:] = 0 # set to black s = 'mode: %d, val: %f sd' % (k - 3, v * 3) draw_string(img, s) q = smodel.calc_shape(p) draw_muct_shape(img, q) cv2.imshow('shape model', img) if cv2.waitKey(10) == 27: sys.exit()
def view_shape_model(shp_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') smodel = ShapeModel.load(shp_fn) vals = genvals() while True: for k in xrange(4, smodel.model.shape[1]): for v in vals: p = smodel.get_params(scale, tranx, trany) p[k] = p[0] * v * 3 * np.sqrt(smodel.variance[k]) img[:] = 0 # set to black s = 'mode: %d, val: %f sd' % (k-3, v*3) draw_string(img, s) q = smodel.calc_shape(p) draw_muct_shape(img, q) cv2.imshow('shape model', img) if cv2.waitKey(10) == 27: sys.exit()
def view_texture_model(shp_fn, txt_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') smodel = ShapeModel.load(shp_fn) tmodel = TextureModel.load(txt_fn) vals = genvals() # get reference shape ref = smodel.get_shape(scale, tranx, trany) ref = ref.reshape((ref.size // 2, 2)) while True: for k in range(tmodel.num_modes()): for v in vals: p = np.zeros(tmodel.num_modes()) p[k] = v * 3 * np.sqrt(tmodel.variance[k]) img[:] = 0 s = 'mode: %d, val: %f sd' % (k, v * 3) draw_string(img, s) t = tmodel.calc_texture(p) draw_texture(img, t, ref) cv2.imshow('texture model', img) if cv2.waitKey(10) == 27: sys.exit()
def view_texture_model(shp_fn, txt_fn, scale, tranx, trany, width, height): img = np.empty((height, width, 3), dtype='uint8') smodel = ShapeModel.load(shp_fn) tmodel = TextureModel.load(txt_fn) vals = genvals() # get reference shape ref = smodel.get_shape(scale, tranx, trany) ref = ref.reshape((ref.size//2, 2)) while True: for k in xrange(tmodel.num_modes()): for v in vals: p = np.zeros(tmodel.num_modes()) p[k] = v * 3 * np.sqrt(tmodel.variance[k]) img[:] = 0 s = 'mode: %d, val: %f sd' % (k, v*3) draw_string(img, s) t = tmodel.calc_texture(p) draw_texture(img, t, ref) cv2.imshow('texture model', img) if cv2.waitKey(10) == 27: sys.exit()