def main():
    plt.close("all")
    tf.enable_eager_execution()

    codes = [
        "S1_I1", "S1_I2", "S1_I3", "S2_I1", "S2_I2", "S2_I3", "S3_I1", "S3_I2",
        "S3_I3", "S4_I1", "S4_I2", "S4_I3"
    ]

    # code = "S1_I1"

    for code in codes:

        data_folder = "Data_synthesized/"
        envDir = data_folder + "EnvMap/"
        envName = envDir + code + "_Illum"
        if (predict_sphere_envMap[1]):
            envFile = envName + "_probe.hdr"
        else:
            envFile = envName + ".hdr"

        albedo_folder = data_folder + "Albedo/"
        normal_folder = data_folder + "AppearanceNormal/"
        albedo_file = albedo_folder + code + ".png"
        normal_file = normal_folder + code + "_Normal_UV.png"
        albedoMap = load_rgba(
            albedo_file)  # "Data02_isomaps/AppearanceMap_test.png")
        normalMap = load_rgb(
            normal_file)  # "Data02_isomaps/NormalMap_test.png")

        data_input_folder_name = "Data_to_synthesize"

        input_shape = (256, 256, 3)
        num_replicate = 1
        (dataset_train_input,
         num_train_input) = load_input_data_with_normals_and_replicate(
             data_input_folder_name, input_shape, True, num_replicate)
        envMap = load_rgb(envFile, -1)
        print("Before resize : max: {:.3f}, mean: ({:.3f})".format(
            np.amax(envMap), np.mean(envMap)))

        (mEnv, nEnv, dEnv) = envMap.shape
        print((mEnv, nEnv))
        plt.imshow(envMap)
        plt.show()
        albedoMap = cv2.resize(albedoMap, (256, 256))
        normalMap = cv2.resize(normalMap, (256, 256))
        envMap = cv2.resize(envMap, (64, 32), interpolation=cv2.INTER_LINEAR)

        print("After resize : max: {:.3f}, mean: ({:.3f})".format(
            np.amax(envMap), np.mean(envMap)))

        (mIm, nIm, dIm) = albedoMap.shape
        (mEnv, nEnv, dEnv) = envMap.shape
        input_shape = albedoMap.shape
        d = 3

        plt.imshow(envMap)
        plt.show()
        plt.imshow(albedoMap)
        plt.show()
        plt.imshow(normalMap)
        plt.show()

        # cv2.namedWindow('albedo', cv2.WINDOW_NORMAL)
        # cv2.imshow('albedo', albedoMap)
        # cv2.namedWindow('normal', cv2.WINDOW_NORMAL)
        # cv2.imshow('normal', normalMap)
        # cv2.namedWindow('envMap', cv2.WINDOW_NORMAL)
        # cv2.imshow('envMap', envMap)
        # cv2.waitKey(0)

        gamma = tf.constant(2.2)
        invGamma = tf.constant(1. / 2.2)
        normalizingValue = tf.constant(255.)
        albedoTensor = tf.constant(albedoMap[:, :, :3], dtype=tf.float32)
        normalTensor = tf.constant(normalMap[:, :, :3], dtype=tf.float32)
        envMapTensor = tf.constant(envMap, dtype=tf.float32)
        albedoTensor = tf.scalar_mul(1. / normalizingValue,
                                     albedoTensor[:, :, :3])
        normalTensor = tf.scalar_mul(1. / normalizingValue,
                                     normalTensor[:, :, :3])
        # albedoTensor = tf.pow(albedoTensor,gamma)

        if (predict_sphere_envMap[1]):
            envNormalization = tf.constant(math.pi * mEnv * nEnv / 4.)
        else:
            envNormalization = tf.constant((float)(mEnv * nEnv))

        ## Calculate envMap orientations
        envVectors = envMapOrientation.envMapOrientation(
            mEnv, nEnv, predict_sphere_envMap[1])
        envOrientationTensor = tf.constant(envVectors, dtype=tf.float32)
        envOrientationTensor = tf.reshape(envOrientationTensor,
                                          [3, mEnv * nEnv])
        envMapTensor = tf.reshape(envMapTensor, [mEnv * nEnv, 3])

        autoencoder_model = FirstGenerator(input_shape, envOrientationTensor,
                                           envMapTensor, envNormalization,
                                           predict_sphere_envMap,
                                           high_res_mode)

        normalTensor = tf.reshape(normalTensor, [1, mIm, nIm, d])
        albedoTensor = tf.reshape(albedoTensor, [1, mIm, nIm, d])

        # resTensor = autoencoder_model.render(normalTensor,albedoTensor)
        resTensor = autoencoder_model.render_with_predicted_envMap(
            normalTensor, albedoTensor,
            tf.reshape(envMapTensor, [num_replicate, mEnv * nEnv, 3]))
        resTensorGamma = tf.pow(resTensor, invGamma)

        res_save = 255. * np.array(resTensorGamma[0])
        res_save = convert_rgb_to_cv2(res_save)
        res_save_temp = np.zeros([res_save.shape[0], res_save.shape[1], 4])
        res_save_temp[:, :, :3] = res_save[:, :, :]
        res_save_temp[:, :, 3] = albedoMap[:, :, 3]
        res_save = res_save_temp.astype(int)

        plt.imshow(resTensor[0])
        plt.show()
        plt.imshow(resTensorGamma[0])
        plt.show()

        cv2.imwrite(normal_folder + code + "_Appearance_UV.png", res_save)

    # x_train_input = dataset_train_input.batch(num_replicate)
    # for (batch, (inputs, labels_appearance, labels_normals, masks)) \
    #         in enumerate(x_train_input):
    #     resTensor = autoencoder_model.render_with_predicted_envMap(labels_normals, inputs,
    #                                                                tf.reshape(envMapsTensors,
    #                                                                           [num_replicate, mEnv * nEnv, 3]))
    #     resTensorGamma = tf.pow(resTensor, invGamma)
    #     res_save = 255. * np.array(resTensorGamma[0])
    #     res_save = convert_rgb_to_cv2(res_save)
    #     res_save_temp = np.zeros([res_save.shape[0], res_save.shape[1], 4])
    #     res_save_temp[:, :, :3] = res_save[:, :, :]
    #     res_save_temp[:, :, 3] = albedoMap[:, :, 3]
    #     res_save = res_save_temp.astype(int)
    #
    #     plt.imshow(envMapsTensors[0])
    #     plt.show()
    #     plt.imshow(resTensorGamma[0])
    #     plt.show()
    #     plt.imshow(envMapsTensors[1])
    #     plt.show()
    #     plt.imshow(resTensorGamma[1])
    #     plt.show()
    #
    # return

    return
Beispiel #2
0
from load_rgb_cv import load_rgb, convert_rgb_to_cv2
import cv2
import matplotlib.pyplot as plt

image_file = "Chicago_albedo.png"
output_file = "Chicago_albedo_test.png"

image1 = load_rgb(image_file)

plt.imshow(image1)
plt.show()
print("image1 : {}".format(image1[150, 150, 1]))

cv2.imwrite(output_file, convert_rgb_to_cv2(image1))

image2 = load_rgb(output_file)

plt.imshow(image2)
plt.show()
print("image2 : {}".format(image2[150, 150, 1]))
autoencoder_model = FirstGenerator(input_shape, envOrientationTensor,
                                   envMapTensor, envNormalization,
                                   predict_sphere_envMap, high_res_mode)

normalTensor = tf.reshape(normalTensor, [1, mIm, nIm, d])
albedoTensor = tf.reshape(albedoTensor, [1, mIm, nIm, d])

# resTensor = autoencoder_model.render(normalTensor,albedoTensor)
resTensor = autoencoder_model.render_with_predicted_envMap(
    normalTensor, albedoTensor,
    tf.reshape(envMapTensor, [num_replicate, mEnv * nEnv, 3]))

resTensorGamma = tf.pow(resTensor, invGamma)

res_save = 255. * np.array(resTensorGamma[0])
res_save = convert_rgb_to_cv2(res_save)
res_save_temp = np.zeros([res_save.shape[0], res_save.shape[1], 4])
res_save_temp[:, :, :3] = res_save[:, :, :]
res_save_temp[:, :, 3] = albedoMap[:, :, 3]
res_save = res_save_temp.astype(int)

plt.imshow(resTensor[0])
plt.show()
plt.imshow(resTensorGamma[0])
plt.show()
#cv2.imwrite(output_data_folder+ code_write + "_Appearance_UV.png", res_save)

resTensorBoosted = autoencoder_model.render_with_predicted_envMap(
    normalTensor, albedo_boosted_Tensor,
    tf.reshape(envMapTensor, [num_replicate, mEnv * nEnv, 3]))
Beispiel #4
0
def main():
    plt.close("all")
    tf.enable_eager_execution()

    folder_write_name = "Data_synthesized/"
    extension_nor_app = "AppearanceNormal/"
    extension_env = "EnvMap/"
    code_sample = "S4_I3_"

    envDir = "EnvMaps/"
    envName = envDir + "STUDIOATM_13SN"
    if (predict_sphere_envMap[1]):
        envFile = envName + "_probe.hdr"
    else:
        #envFile = folder_write_name + extension_env + code_sample + "Illum.hdr"
        envFile = envName + ".hdr"
    albedoMap = load_rgb(
        "Chicago_albedo.png")  #"Data02_isomaps/AppearanceMap_test.png")
    normalMap = load_rgb(
        "Chicago_normal.png")  #"Data02_isomaps/NormalMap_test.png")
    envMap = load_rgb(envFile, -1)
    print("Before resize : max: {:.3f}, mean: ({:.3f})".format(
        np.amax(envMap), np.mean(envMap)))

    shutil.copy(
        "Chicago_normal.png",
        folder_write_name + extension_nor_app + code_sample + "Normal_UV.png")
    shutil.copy(envFile,
                folder_write_name + extension_env + code_sample + "Illum.hdr")

    (mEnv, nEnv, dEnv) = envMap.shape
    print((mEnv, nEnv))
    plt.imshow(envMap)
    plt.show()
    albedoMap = cv2.resize(albedoMap, (256, 256))
    normalMap = cv2.resize(normalMap, (256, 256))
    envMap = cv2.resize(envMap, (3 * 32, 3 * 16),
                        interpolation=cv2.INTER_LINEAR)

    print("After resize : max: {:.3f}, mean: ({:.3f})".format(
        np.amax(envMap), np.mean(envMap)))

    (mIm, nIm, dIm) = albedoMap.shape
    (mEnv, nEnv, dEnv) = envMap.shape
    input_shape = albedoMap.shape
    d = 3

    plt.imshow(envMap)
    plt.show()
    plt.imshow(albedoMap)
    plt.show()
    plt.imshow(normalMap)
    plt.show()

    # cv2.namedWindow('albedo', cv2.WINDOW_NORMAL)
    # cv2.imshow('albedo', albedoMap)
    # cv2.namedWindow('normal', cv2.WINDOW_NORMAL)
    # cv2.imshow('normal', normalMap)
    # cv2.namedWindow('envMap', cv2.WINDOW_NORMAL)
    # cv2.imshow('envMap', envMap)
    # cv2.waitKey(0)

    gamma = tf.constant(2.2)
    invGamma = tf.constant(1. / 2.2)
    normalizingValue = tf.constant(255.)
    albedoTensor = tf.constant(albedoMap[:, :, :3], dtype=tf.float32)
    normalTensor = tf.constant(normalMap[:, :, :3], dtype=tf.float32)
    envMapTensor = tf.constant(envMap, dtype=tf.float32)
    albedoTensor = tf.scalar_mul(1. / normalizingValue, albedoTensor[:, :, :3])
    normalTensor = tf.scalar_mul(1. / normalizingValue, normalTensor[:, :, :3])
    #albedoTensor = tf.pow(albedoTensor,gamma)

    if (predict_sphere_envMap[1]):
        envNormalization = tf.constant(math.pi * mEnv * nEnv / 4.)
    else:
        envNormalization = tf.constant((float)(mEnv * nEnv))

    ## Calculate envMap orientations
    envVectors = envMapOrientation.envMapOrientation(mEnv, nEnv,
                                                     predict_sphere_envMap[1])
    envOrientationTensor = tf.constant(envVectors, dtype=tf.float32)
    envOrientationTensor = tf.reshape(envOrientationTensor, [3, mEnv * nEnv])
    envMapTensor = tf.reshape(envMapTensor, [mEnv * nEnv, 3])

    autoencoder_model = FirstGenerator(input_shape, envOrientationTensor,
                                       envMapTensor, envNormalization,
                                       predict_sphere_envMap, high_res_mode)

    normalTensor = tf.reshape(normalTensor, [1, mIm, nIm, d])
    albedoTensor = tf.reshape(albedoTensor, [1, mIm, nIm, d])

    resTensor = autoencoder_model.render(normalTensor, albedoTensor)
    resTensorGamma = tf.pow(resTensor, invGamma)

    res_save = 255. * np.array(resTensorGamma[0])
    res_save = convert_rgb_to_cv2(res_save)
    res_save = res_save.astype(int)

    plt.imshow(resTensor[0])
    plt.show()
    plt.imshow(resTensorGamma[0])
    plt.show()

    #cv2.imwrite(folder_name+ "envMap_resized.hdr" , envMap)
    cv2.imwrite(
        folder_write_name + extension_nor_app + code_sample +
        "Appearance_UV.png", res_save)
    #cv2.imwrite(folder_name+ "normalMap_resized.png" , normalMap)

    return
def render_face(envName, input_code, output_code, log_and_show, write_results,
                albedo_mode):
    if (predict_sphere_envMap[1]):
        envFile = envDir + envName + "_probe.hdr"
    else:
        envFile = envDir + envName + ".hdr"

    albedo_file = input_data_folder + input_code + "_Appearance_UV.png"
    normal_file = input_data_folder + input_code + "_Normal_UV.png"
    # LOAD
    albedoMap = load_rgba(
        albedo_file)  # "Data02_isomaps/AppearanceMap_test.png")
    normalMap = load_rgb(normal_file)  # "Data02_isomaps/NormalMap_test.png")
    envMap = load_rgb(envFile, -1)
    if (write_results):
        shutil.copy(envFile, output_data_folder + output_code + "_Illum.hdr")

    (mEnv_input, nEnv_input, dEnv) = envMap.shape

    if (log_and_show):
        print("Before resize : max: {:.3f}, mean: ({:.3f})".format(
            np.amax(envMap), np.mean(envMap)))
        print("Input envmap size : ({:},{:})".format(mEnv_input, nEnv_input))
        plt.imshow(envMap)
        plt.show()

    #RESIZE
    albedoMap = cv2.resize(albedoMap, (nIm, mIm))
    normalMap = cv2.resize(normalMap, (nIm, mIm))
    envMap = cv2.resize(envMap, (nEnv, mEnv), interpolation=cv2.INTER_LINEAR)

    if (log_and_show):
        print("After resize : max: {:.3f}, mean: ({:.3f})".format(
            np.amax(envMap), np.mean(envMap)))

    d = 3

    albedoTensor = tf.constant(albedoMap[:, :, :3], dtype=tf.float32)
    normalTensor = tf.constant(normalMap[:, :, :3], dtype=tf.float32)
    envMapTensor = tf.constant(envMap, dtype=tf.float32)
    albedoTensor = tf.scalar_mul(1. / normalizingValue, albedoTensor[:, :, :3])
    normalTensor = tf.scalar_mul(1. / normalizingValue, normalTensor[:, :, :3])

    # Boost albedo
    albedo_boosted_Tensor = tf.scalar_mul(tf.constant(albedo_boosting_factor),
                                          tf.pow(albedoTensor, gamma))

    if (log_and_show):
        plt.imshow(envMapTensor)
        plt.show()
        plt.imshow(albedoTensor)
        plt.show()
        plt.imshow(tf.pow(albedo_boosted_Tensor, invGamma))
        plt.show()
        # plt.imshow(normalTensor)
        # plt.show()

    envMapTensor = tf.reshape(envMapTensor, [mEnv * nEnv, 3])
    normalTensor = tf.reshape(normalTensor, [1, mIm, nIm, d])
    albedoTensor = tf.reshape(albedoTensor, [1, mIm, nIm, d])

    autoencoder_model = FirstGenerator(input_shape, envOrientationTensor,
                                       envMapTensor, envNormalization,
                                       predict_sphere_envMap, high_res_mode)

    # resTensor = autoencoder_model.render(normalTensor,albedoTensor)
    if albedo_mode == 0:
        albedo_render = albedoTensor
        albedo_save = albedoMap
    else:
        albedo_render = albedo_boosted_Tensor
        albedo_save = 255. * np.array(tf.pow(albedo_boosted_Tensor, invGamma))

    resTensor = autoencoder_model.render_with_predicted_envMap(
        normalTensor, albedo_render,
        tf.reshape(envMapTensor, [1, mEnv * nEnv, 3]))

    resTensorGamma = tf.pow(resTensor, invGamma)
    res_save = 255. * np.array(resTensorGamma[0])
    res_save = convert_rgb_to_cv2(res_save)
    res_save_temp = np.zeros([res_save.shape[0], res_save.shape[1], 4])
    res_save_temp[:, :, :3] = res_save[:, :, :]
    res_save_temp[:, :, 3] = albedoMap[:, :, 3]
    res_save = res_save_temp.astype(int)

    albedo_save = convert_rgb_to_cv2(albedo_save)
    albedo_save = albedo_save.astype(int)

    if (log_and_show):
        # plt.imshow(resTensor[0])
        # plt.show()
        plt.imshow(resTensorGamma[0])
        plt.show()

    if (write_results):
        cv2.imwrite(output_data_folder + output_code + "_Appearance_UV.png",
                    res_save)
        cv2.imwrite(output_data_folder + output_code + "_Albedo_UV.png",
                    albedo_save)
        cv2.imwrite(output_data_folder + output_code + "_Normal_UV.png",
                    normalMap)

    return