コード例 #1
0
ファイル: test.py プロジェクト: 7yChris/AI-Final-Project
def test2():
    images = ['imgs/1.jpg', 'imgs/2.jpg']
    style_path = 'styles/var'
    styles = os.listdir(style_path)
    target, sess, content, y1, y2, y3, y4, alpha1, alpha2, alpha3 = Init(
        args.C_NUMS, args.PATH_MODEL)
    x_len = args.C_NUMS + 1
    y_len = len(images) + 1
    img_all = Image.new("RGB", (x_len * 512, y_len * 512))
    for i, style_name in enumerate(styles):
        path = os.path.join(style_path, style_name)
        with Image.open(path) as img:
            img_resized = center_crop_img(img).resize((512, 512))
            img_all.paste(img_resized, get_point(0, i + 1, 512))
    for content_n in range(y_len - 1):
        with Image.open(images[content_n]) as img:
            img_resized = center_crop_img(img).resize((512, 512))
            img_all.paste(img_resized, get_point(content_n + 1, 0, 512))
            for style_n in range(x_len - 1):
                img_arr = np.array(img)
                y = np.zeros([1, 10])
                y[0, style_n] = 1
                img_stylized_arr = sess.run(target,
                                            feed_dict={
                                                content:
                                                img_arr[np.newaxis, :, :, :],
                                                y1:
                                                y,
                                                y2:
                                                None,
                                                y3:
                                                None,
                                                y4:
                                                None,
                                                alpha1:
                                                1,
                                                alpha2:
                                                None,
                                                alpha3:
                                                None
                                            })
                img_stylized = Image.fromarray(
                    np.uint8(img_stylized_arr[0, :, :, :]))
                img_stylized.save('{}_{}'.format(
                    style_n,
                    os.path.split(images[content_n])[1]))
                img_stylized = center_crop_img(img_stylized).resize((512, 512))
                img_all.paste(img_stylized,
                              get_point(content_n + 1, style_n + 1, 512))
コード例 #2
0
ファイル: test.py プロジェクト: 7yChris/AI-Final-Project
    def save_25result(self):
        size = 5
        i = 0
        # 按行生成
        while i < size:
            # 1、2风格权重之和
            x_sum = 100 - i * 25.0
            # 3、4风格权重之和
            y_sum = i * 25
            # 1、2风格之和进行五等分,计算权重step值
            x_step = x_sum / 4.0
            # 3、4风格之和进行五等分,计算权重step值
            y_step = y_sum / 4.0

            # 按列生成
            j = 0
            while j < size:
                # 计算1、2风格的权重
                ap1 = x_sum - j * x_step
                ap2 = j * x_step
                # 计算3风格权重
                ap3 = y_sum - j * y_step
                # 归一化后存到args中
                alphas = (float('%.2f' % (ap1 / 100.0)),
                          float('%.2f' % (ap2 / 100.0)),
                          float('%.2f' % (ap3 / 100.0)))
                # 返回融合后图像
                # img_return = stylize(args.PATH_IMG, args.PATH_RESULTS, args.LABEL_1, args.LABEL_2, args.LABEL_3,
                #                      args.LABEL_4, args.ALPHA1, args.ALPHA2, args.ALPHA3, target, sess, content, y1, y2, y3,
                #                      y4, alpha1, alpha2, alpha3)
                img_return = self.stylize(alphas)
                # array转IMG
                img_return = Image.fromarray(np.uint8(img_return[0, :, :, :]))
                # 将5个图像按行拼接
                if j == 0:
                    width, height = img_return.size
                    img_5 = Image.new(img_return.mode, (width * 5, height))
                img_5.paste(img_return,
                            (width * j, 0, width * (j + 1), height))
                j = j + 1

            # 将多个行拼接图像,拼接成5*5矩阵
            if i == 0:
                img_25 = Image.new(img_return.mode, (width * 5, height * 5))
            img_25.paste(img_5, (0, height * i, width * 5, height * (i + 1)))

            i = i + 1

        # 将5*5矩阵图像的4个角加上4个风格图像,以作对比
        img_25_4 = Image.new(img_return.mode, (width * 7, height * 5))
        img_25_4 = ImageOps.invert(img_25_4)
        img_25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[0] + 1) + '.png')).resize(
                               (width, height)), (0, 0, width, height))
        img_25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[1] + 1) + '.png')).resize(
                               (width, height)),
            (width * 6, 0, width * 7, height))
        img_25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[2] + 1) + '.png')).resize(
                               (width, height)),
            (0, height * 4, width, height * 5))
        img_25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[3] + 1) + '.png')).resize(
                               (width, height)),
            (width * 6, height * 4, width * 7, height * 5))
        img_25_4.paste(img_25, [width, 0, width * 6, height * 5])

        # 存储5*5图像矩阵
        img_25.save(self.stylizer_arg.PATH_RESULTS +
                    self.img_path.split('/')[-1].split('.')[0] +
                    str(self.label_list) + '_result_25' + '.jpg')
        # 存储5*5+4风格图像矩阵

        print(self.stylizer_arg.PATH_RESULTS +
              self.img_path.split('/')[-1].split('.')[0] +
              str(self.label_list) + '_result_25_4' + '.jpg' + " saved!")
コード例 #3
0
ファイル: test.py プロジェクト: leonda/TensorFlow1_Notes
    def generate_result(self):
        size = 5
        i = 0
        # 按行生成
        while i < size:
            # 1、2风格权重之和
            x_sum = 100 - i * 25.0
            # 3、4风格权重之和
            y_sum = i * 25
            # 1、2风格之和进行五等分,计算权重step值
            x_step = x_sum / 4.0
            # 3、4风格之和进行五等分,计算权重step值
            y_step = y_sum / 4.0

            # 按列生成
            j = 0
            while j < size:
                # 计算1、2风格的权重
                ap1 = x_sum - j * x_step
                ap2 = j * x_step
                # 计算3风格权重
                ap3 = y_sum - j * y_step
                ap4 = j * y_step
                # 归一化后存到alphas中
                alphas = (float('%.2f' % (ap1 / 100.0)),
                          float('%.2f' % (ap2 / 100.0)),
                          float('%.2f' % (ap3 / 100.0)),
                          float('%.2f' % (ap4 / 100.0)))
                # 返回融合后图像
                img_return = self.stylize(alphas)
                # array转IMG
                img_return = Image.fromarray(np.uint8(img_return[0, :, :, :]))
                # 将5个图像按行拼接
                if j == 0:
                    width, height = img_return.size
                    img_5 = Image.new(img_return.mode, (width * 5, height))
                img_5.paste(img_return,
                            (width * j, 0, width * (j + 1), height))
                j = j + 1

            # 将多个行拼接图像,拼接成5*5矩阵
            if i == 0:
                img_25 = Image.new(img_return.mode, (width * 5, height * 5))
            img_25.paste(img_5, (0, height * i, width * 5, height * (i + 1)))

            i = i + 1

        # 将5*5矩阵图像的4个角加上4个风格图像,以作对比
        img25_4 = Image.new(img_25.mode, (width * 7, height * 5))
        img25_4 = ImageOps.invert(img25_4)
        img25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[0] + 1) + '.png')).resize(
                               (width, height)), (0, 0, width, height))
        img25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[1] + 1) + '.png')).resize(
                               (width, height)),
            (width * 6, 0, width * 7, height))
        img25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[2] + 1) + '.png')).resize(
                               (width, height)),
            (0, height * 4, width, height * 5))
        img25_4.paste(
            center_crop_img(
                Image.open(self.stylizer_arg.PATH_STYLE +
                           str(self.label_list[3] + 1) + '.png')).resize(
                               (width, height)),
            (width * 6, height * 4, width * 7, height * 5))
        img25_4.paste(img_25, [width, 0, width * 6, height * 5])

        self.img25 = img_25
        self.img25_4 = img25_4