示例#1
0
    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))
示例#2
0
    def __getitem__(self, item):

        img_path = self.images[item]
        image = cv2.imread(img_path)
        landmark_path = self.landmarks[item]
        landmarks = utils.read_mat(landmark_path)
        bbox_path = self.bboxes[item]
        bbox = utils.read_bbox(bbox_path)
        return image, landmarks, np.array(bbox), self.file_list[item]
示例#3
0
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)
示例#4
0
    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
        image = image[miny:maxy + 1, minx:maxx + 1, :]
        image = cv2.resize(image, self.shape)

        image = np.transpose(image, (2, 0, 1)).astype(np.float32)
        return image, np.reshape(landmarks,
                                 (-1)), np.array(bbox), self.file_list[item]
示例#5
0
 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))
示例#6
0
    bins = os.listdir(bin_dir)

    file_list = []
    b = bins[pose]
    curr = os.path.join(bin_dir, b)
    files = os.listdir(curr)
    for i in files:
        file_list.append(i)
    for i in range(100):
        img_dir = os.path.join(root_dir, 'data/picture')
        landmark_dir = os.path.join(root_dir, 'data/landmark')
        bbox_dir = os.path.join(root_dir, 'bbox')
        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_path = images[i]
        bbox_path = bboxes[i]
        landmark_path = landmarks[i]
        bbox = ul.read_bbox(bbox_path)
        landmarks = ul.read_mat(landmark_path)
        image = cv2.imread(img_path)

        image, landmark, t = a(image, landmarks)

        plt.imshow(image)
        plt.scatter(landmark[:, 0], landmark[:, 1])
        # plt.scatter(self.reference[:, 0], self.reference[:, 1])
        # plt.plot(bbox[:, 0], bbox[:, 1])
        plt.xlim(0, a.scale[0])
        plt.ylim(a.scale[1], 0)
        plt.show()
示例#7
0
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)
示例#8
0
文件: crop.py 项目: hyb1234hi/emci
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)
示例#9
0
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)
示例#10
0
文件: pwa.py 项目: hyb1234hi/emci
    add = np.concatenate([top, bottom, left, right], axis=0)
    src = np.concatenate([src, add], axis=0)
    dst = np.concatenate([dst, add], axis=0)
    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)
示例#11
0
文件: distort.py 项目: hyb1234hi/emci
filenames = np.array(joblib.load('cache/filenames.pkl'))
img_dir = '/data/icme/data/picture'
bbox_dir = '/data/icme/bbox'
landmark_dir = '/data/icme/data/landmark'

aligner = alignment.Align('cache/mean_landmarks.pkl')
idx = np.argsort(pc)

for i in range(5000, len(idx) - 5):
    id = list(idx[i:i + 5])
    names = filenames[id]
    for j in range(1, 5):
        src = names[0]
        dst = names[j]
        image = plt.imread(os.path.join(img_dir, src))
        bbox = utils.read_bbox(os.path.join(bbox_dir, src + '.rect'))
        src = utils.read_mat(os.path.join(landmark_dir, src + '.txt'))
        image, src, _ = aligner(image, src, bbox)
        src[:, 0] /= aligner.scale[0]
        src[:, 1] /= aligner.scale[1]
        image2 = plt.imread(os.path.join(img_dir, dst))
        bbox = utils.read_bbox(os.path.join(bbox_dir, dst + '.rect'))
        dst = utils.read_mat(os.path.join(landmark_dir, dst + '.txt'))
        image2, dst, _ = aligner(image2, dst, bbox)
        dst[:, 0] /= aligner.scale[0]
        dst[:, 1] /= aligner.scale[1]
        dst[0:34, :] = src[0:34, :]
        out = pwa.pwa(image, src, dst, (128, 128))

        plt.subplot(1, 3, 1)
        plt.imshow(image)
示例#12
0
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)




示例#13
0
from utils import pwa
from data import utils
import numpy as np
import matplotlib.pyplot as plt

filename = os.listdir("/home/zhzhong/Desktop/correctdata/train/1/")
img_dir = '/home/zhzhong/Desktop/correctdata/train/1/'
base_dir = "/home/zhzhong/Desktop/correctdata/bbox/"
new_dir = "/home/zhzhong/Desktop/newdata/1/"
bbox1 = np.array([0, 0, 128, 128])
bbox2 = np.array([0, 0, 128, 128])
dst = utils.read_mat(
    '/home/zhzhong/Desktop/correctdata/data/landmark/AFW_5452623_1_5.jpg.txt')
dst = utils.norm_landmarks(dst, bbox2)
filenameforlmk = os.listdir("/home/zhzhong/Desktop/correctdata/data/landmark/")
base_diroflmk = "/home/zhzhong/Desktop/correctdata/data/landmark/"
for img in filename:
    image = plt.imread(img_dir + img)
    # plt.imshow(image)
    # plt.show()
    box_dir = "/home/zhzhong/Desktop/correctdata/bbox/" + img + '.rect'
    box = utils.read_bbox(box_dir)
    image = image[box[1]:box[3], box[0]:box[2]]
    for lmk in filenameforlmk:
        src = utils.read_mat(base_diroflmk + lmk)
        src = utils.norm_landmarks(src, bbox1)
        image_pwa, _ = pwa.pwa(image, src, dst, (128, 128))
        #image_pwa.save(new_dir + img)
        plt.imshow(image_pwa)
        plt.show()