def __getitem__(self, item): image, landmarks = super(BBoxDataset, self).__getitem__(item) bbox_path = self.bboxes[item] try: bbox = utils.read_bbox(bbox_path) except: bbox = [0, 0, image.shape[1] - 1, image.shape[0] - 1] minx, miny, maxx, maxy = bbox if self.phase == 'train': left = min(minx, self.max_jitter) right = min(image.shape[1] - maxx - 1, self.max_jitter) up = min(miny, self.max_jitter) down = min(image.shape[0] - maxy - 1, self.max_jitter) dh = np.random.randint(-up, down + 1, 1) dv = np.random.randint(-left, right + 1, 1) bbox[0::2] += dv bbox[1::2] += dh minx, miny, maxx, maxy = bbox landmarks = utils.norm_landmarks(landmarks, bbox) image = image[miny:maxy + 1, minx:maxx + 1, :] image = cv2.resize(image, self.shape) if self.phase == 'train': image, landmarks = utils.random_flip(image, landmarks, 0.5) image = utils.random_gamma_trans(image, np.random.uniform(0.8, 1.2, 1)) image = utils.random_color(image) image = np.transpose(image, (2, 0, 1)).astype(np.float32) return image, np.reshape(landmarks, (-1))
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)
def __getitem__(self, i): img_path = self.images[i] bbox_path = self.bboxes[i] landmark_path = self.landmarks[i] bbox = utils.read_bbox(bbox_path) landmarks = utils.read_mat(landmark_path) landmarks = utils.norm_landmarks(landmarks, bbox) image = cv2.imread(img_path) # minx, miny, maxx, maxy = bbox # image = image[miny:maxy+1, minx:maxx+1, :] # image = cv2.resize(image, self.shape) # cv2.imshow("t", image) # cv2.waitKey(0) image = np.transpose(image, (2, 0, 1)).astype(np.float32) #print('origin', origin_landmarks) return image, np.reshape(landmarks, (-1)), np.reshape(np.array(bbox), (2, 2))
def main(): if not os.path.exists('cache'): os.system('mkdir cache') # 我偷懒了,所以最好不要写成'/home/yqi/data/icme/' root_dir = '/data/icme' lamdmark_dir = os.path.join(root_dir, 'data/landmark') image_dir = os.path.join(root_dir, 'data/picture') bbox_dir = os.path.join(root_dir, 'bbox') try: filenames = joblib.load('cache/filenames.pkl') norm_landmarks = joblib.load('cache/norm_landmarks.pkl') mean_landmarks = joblib.load('cache/mean_landmarks.pkl') bboxes = joblib.load('cache/bboxes.pkl') split = joblib.load('cache/split.pkl') except: filenames = os.listdir(image_dir) norm_landmarks = [] bboxes = [] split = {} for filename in filenames: id = get_id(filename) if np.random.uniform(0, 1) < 0.8: split[id] = 'train' landmark_path = os.path.join(lamdmark_dir, filename + '.txt') bbox_path = os.path.join(bbox_dir, filename + '.rect') bbox = utils.read_bbox(bbox_path) landmarks = utils.read_mat(landmark_path) landmarks = utils.norm_landmarks(landmarks, bbox) norm_landmarks.append(landmarks) else: split[id] = 'valid' bbox_path = os.path.join(bbox_dir, filename + '.rect') bbox = utils.read_bbox(bbox_path) bboxes.append(bbox) norm_landmarks = np.stack(norm_landmarks, axis=0) mean_landmarks = np.mean(norm_landmarks, axis=0) joblib.dump(norm_landmarks, 'cache/norm_landmarks.pkl', compress=3) joblib.dump(mean_landmarks, 'cache/mean_landmarks.pkl', compress=3) joblib.dump(filenames, 'cache/filenames.pkl', compress=3) joblib.dump(bboxes, 'cache/bboxes.pkl', compress=3) joblib.dump(split, 'cache/split.pkl', compress=3) # for i in range(106): # plt.scatter(mean_landmarks[i, 0], mean_landmarks[i, 1]) # plt.show() try: transform_matrix = joblib.load('cache/transform_matrix.pkl') aligned = joblib.load('cache/aligned.pkl') except: transform_matrix = [] aligned = [] i = -1 for filename in filenames: if split[get_id(filename)] == 'valid': continue i += 1 curr = norm_landmarks[i, :] one = np.ones(shape=(106, 1)) curr = np.concatenate((curr, one), axis=1) t = procrustes(curr, mean_landmarks) transform_matrix.append(t) aligned.append(np.reshape(curr@t, (-1))) joblib.dump(transform_matrix, 'cache/transform_matrix.pkl', compress=3) joblib.dump(aligned, 'cache/aligned.pkl', compress=3) temp = (aligned - np.mean(aligned, axis=0)) covariance = 1.0 / len(aligned) * temp.T.dot(temp) U, S, V = np.linalg.svd(covariance) joblib.dump(U, 'cache/u.pkl', compress=3) pc = temp.dot(U[:, 0]) plt.hist(pc,bins=11) plt.show() for i, filename in enumerate(filenames): img_path = os.path.join(image_dir, filename) if pc[i] > 0.793: n = '1' elif pc[i] > 0.615: n = '2' elif pc[i] > 0.44: n = '3' elif pc[i] > 0.26: n = '4' elif pc[i] > 0.087: n = '5' elif pc[i] > -0.0913: n = '6' elif pc[i] > -0.264: n = '7' elif pc[i] > -0.448: n = '8' elif pc[i] > -0.62: n = '9' elif pc[i] > -0.79: n = '10' else: n = '11' id = get_id(filename) cmd = 'ln -s %s %s/%s/%s/%s' % (img_path, root_dir, split[id], n, filename) os.system(cmd)
def main(): def get_id(name): t = name.split('_')[0:2] return t[0] + t[1] root_dir = 'D:\icmedata\correctdata\\' lamdmark_dir = os.path.join(root_dir, 'data/landmark') image_dir = os.path.join(root_dir, 'data/picture') bbox_dir = os.path.join(root_dir, 'bbox') filenames = os.listdir(image_dir) norm_landmarks = [] bboxes = [] split = {} for filename in filenames: id = get_id(filename) if np.random.uniform(0, 1) < 0.8: split[id] = 'train' else: split[id] = 'valid' landmark_path = os.path.join(lamdmark_dir, filename + '.txt') bbox_path = os.path.join(bbox_dir, filename + '.rect') bbox = utils.read_bbox(bbox_path) landmarks = utils.read_mat(landmark_path) landmarks = utils.norm_landmarks(landmarks, bbox) norm_landmarks.append(landmarks) bboxes.append(bbox) norm_landmarks = np.stack(norm_landmarks, axis=0) mean_landmarks = np.mean(norm_landmarks, axis=0) # for i in range(106): # plt.scatter(mean_landmarks[i, 0], mean_landmarks[i, 1]) # plt.show() target=[] for i, filename in enumerate(filenames): curr = norm_landmarks[i, :] y = curr[1::2] y_max = np.max(y) y_min = np.min(y) x = curr[::2] x_max = np.max(x) x_min = np.min(x) chang = x_max - x_min kuan = y_max - y_min Slandmark = chang * kuan #print((Slandmark)) #print(bboxes[i]) # bbox_tempt = np.array(bboxes) # Sbbox = (bbox_tempt[:, 2] - bbox_tempt[:, 0]) * (bbox_tempt[:, 3] - bbox_tempt[:, 1]) # print(Sbbox[i]) #landmark就是基于bbox做的归一化在untils。norm——landmark所以就不用求Sbbox target.append(Slandmark) draw_hist(target, 'acreage title', 'SL/SB', 'amount', 0, 1, 0, 3000) for i, filename in enumerate(filenames): img_path = os.path.join(image_dir, filename) if target[i] > 0.8: n = 's1' elif target[i] > 0.75: n = 's2' elif target[i] > 0.7: n = 's3' elif target[i] > 0.64: n = 's4' elif target[i] > 0.6: n = 's5' elif target[i] > 0.54: n = 's6' elif target[i] > 0.5: n = 's7' else: n = 's8' id = get_id(filename) cmd = 'ln -s %s %s/%s/%s/%s' % (img_path, root_dir, split[id], n, filename) os.system(cmd)
tform = PiecewiseAffineTransform() tform.estimate(dst, src) # out_rows ,out_cols = shape out_rows = image.shape[0] out_cols = image.shape[1] out = warp(image, tform, output_shape=(out_rows, out_cols)) return out if __name__ == '__main__': import matplotlib.pyplot as plt image = plt.imread('/data/icme/data/picture/AFW_134212_1_0.jpg') bbox = utils.read_bbox('/data/icme/bbox/AFW_134212_1_0.jpg.rect') src = utils.read_mat('/data/icme/data/landmark/AFW_134212_1_0.jpg.txt') src = utils.norm_landmarks(src, bbox) image = image[bbox[1]:bbox[3], bbox[0]:bbox[2]] bbox = utils.read_bbox('/data/icme/bbox/AFW_134212_1_3.jpg.rect') dst = utils.read_mat('/data/icme/data/landmark/AFW_134212_1_3.jpg.txt') dst = utils.norm_landmarks(dst, bbox) out = pwa(image, src, dst, (128, 128)) plt.subplot(1, 2, 1) plt.imshow(image) plt.subplot(1, 2, 2) plt.imshow(out) # plt.plot(tform.inverse(src)[:, 0], tform.inverse(src)[:, 1], '.b') plt.axis((0, 128, 128, 0)) plt.show()
import numpy as np import cv2 from data import utils imgdir = '/data/icme/data/picture/AFW_5452623_1_5.jpg' landmarks = utils.read_mat('/data/icme/data/landmark/AFW_5452623_1_5.jpg.txt') bbox = utils.read_bbox('/data/icme/bbox/AFW_5452623_1_5.jpg.rect') img = cv2.imread(imgdir) minx, miny, maxx, maxy = bbox img = img[miny:maxy+1, minx:maxx+1, :] landmarks = utils.norm_landmarks(landmarks, bbox) img, landmarks = utils.random_flip(img, landmarks, 1) img = np.transpose(img, (2, 0, 1)) img = utils.draw_landmarks(img, landmarks, (255, 255, 255)) img = np.transpose(img, (1, 2, 0)) cv2.imshow('', img) cv2.waitKey(0)