def save_pic_and_lmk(kk): shuru = kk if shuru == 0: e = 1 #return 0 输入为0,说明只有一张图 else: lmk_dir = '/home/zhang/correctdata/data/landmark/' #注意lmk文件的后缀为.txt pic_dir = '/home/zhang/correctdata/data/picture/' bbox_dir = '/home/zhang/correctdata/bbox/' length = len(shuru) for i in range(length): if i % 2 == 0: filename1 = pic_dir + shuru[i] '''filename2 = pic_dir+shuru[i+1]''' lmk1 = utils.read_mat(lmk_dir + shuru[i + 1] + '.txt') lmk2 = utils.read_mat(lmk_dir + shuru[i] + '.txt') lmk = 0.5 * lmk1 + 0.5 * lmk2 image = plt.imread(filename1) #图一的landmark归一化 bbox1 = utils.read_bbox(bbox_dir + shuru[i + 1] + '.rect') lmk1 = utils.norm_landmarks(lmk1, bbox1) image = image[bbox1[1]:bbox1[3], bbox1[0]:bbox1[2]] #图一图二的插值图的landmark归一化 bbox2 = utils.read_bbox(bbox_dir + shuru[i] + '.rect') lmk = utils.norm_landmarks(lmk, bbox2) out = pwa.pwa(image.copy(), lmk1.copy(), lmk.copy(), (256, 256)) # 图片与lmk保存 utils.save_landmarks( lmk * 255, '/home/zhang/ming/save_for_lmk2/' + shuru[i][:-5] + "%d" % (200 + i) + '.jpg' + '.txt') io.imsave( '/home/zhang/ming/save_for_pic2/' + shuru[i][:-5] + "%d" % (200 + i) + '.jpg', out)
bbox_dir = os.path.join(root, 'bbox') out_dir = os.path.join(root, 'crop') if not os.path.exists(out_dir): os.makedirs(os.path.join(out_dir, 'data/picture')) os.makedirs(os.path.join(out_dir, 'data/landmark')) for name in os.listdir(img_dir): img_name = name ldmk_name = name + '.txt' bbox_name = name + '.rect' img_path = os.path.join(img_dir, img_name) ldmk_path = os.path.join(ldmk_dir, ldmk_name) bbox_path = os.path.join(bbox_dir, bbox_name) img_out_path = img_path.replace(root, root + 'crop/').replace('.jpg', '.png') ldmk_out_path = ldmk_path.replace(root, root + 'crop/') img = plt.imread(img_path) bbox = utils.read_bbox(bbox_path) ldmk = utils.read_mat(ldmk_path) minx, miny, maxx, maxy = bbox ldmk -= [minx, miny] img = img[miny:maxy + 1, minx:maxx + 1, :] plt.imsave(img_out_path, img) utils.save_landmarks(ldmk, ldmk_out_path)
images = [os.path.join(img_dir, f) for f in file_list] landmarks = [os.path.join(landmark_dir, f + '.txt') for f in file_list] bboxes = [os.path.join(bbox_dir, f + '.rect') for f in file_list] img_out_dir = '/data/icme/align_by_ldmk/picture' landmarks_out_dir = '/data/icme/align_by_ldmk/landmark' t_out_dir = '/data/icme/align_by_ldmk/T' if not os.path.exists(img_out_dir): os.makedirs(img_out_dir) if not os.path.exists(landmarks_out_dir): os.makedirs(landmarks_out_dir) for img_path, bbox_path, landmark_path, f in zip(images, bboxes, landmarks, file_list): bbox = utils.read_bbox(bbox_path) landmark = utils.read_mat(landmark_path) image = cv2.imread(img_path) image, landmark, T = aligner(image, landmark, bbox) # T[2, 0] /= image.shape[1] # T[2, 1] /= image.shape[0] # T[0:2, :] = np.linalg.inv(T[0:2, :]) cv2.imwrite(os.path.join(img_out_dir, f), image) utils.save_landmarks(landmark, os.path.join(landmarks_out_dir, f + '.txt')) utils.save_T(T, os.path.join(t_out_dir, f + '.txt'))
num_workers=4)) for i in range(len(a)): image, gt, t, name = next(batch_iterator) image = image.cuda() with torch.no_grad(): pr = net.forward(image) pr = pr.cpu().data.numpy() gt = gt.data.numpy() t = t.data.numpy() pr = np.reshape(pr, (-1, 2)) pr[:, 0] *= a.shape[1] pr[:, 1] *= a.shape[0] pr = a.aligner.inverse(pr, t[0]) gt = np.reshape(gt, (-1, 2)) all_pr.append(pr) all_gt.append(gt) # save prediction utils.save_landmarks(pr, os.path.join(save_dir, name[0] + '.txt')) all_gt = np.stack(all_gt, axis=0) all_pr = np.stack(all_pr, axis=0) nme = metrics.nme.update(np.reshape(all_gt, (-1, 106, 2)), np.reshape(all_pr, (-1, 106, 2))) metrics.auc.update(nme) print("NME: %f\nAUC: %f" % (metrics.nme.value, metrics.auc.value))
# plt.subplot(1, 3, 3) # plt.imshow(out) # plt.axis((0, 128, 128, 0)) # plt.show() for k in range(106): cv2.circle(temp_out, (out_landmark[k, 0], out_landmark[k, 1]), 0, (0, 255, 0)) temp_out = cv2.resize(temp_out, (1024, 1024)) # cv2.namedWindow('output', flags=cv2.WINDOW_NORMAL) # cv2.imshow('output', temp_out) # cv2.waitKey(0) # time.sleep(3) #创建新的名字 temp_src_name = src_name[0:len(src_name) - 4] temp_dst_name = dst_name[0:len(dst_name) - 4] distort_image_name = temp_src_name + '_' + temp_dst_name + '.jpg' #保存扭曲图片 imagefile = Image.fromarray(out).convert('RGB') imagefile.save(out_dir + 'images/' + distort_image_name) #保存picture_draws imagefile2 = Image.fromarray(temp_out).convert('RGB') imagefile2.save(out_dir + 'picture_draws/' + str(j) + '/' + distort_image_name) #保存landmarks utils.save_landmarks( out_landmark, out_dir + 'landmarks/' + distort_image_name + '.txt')