Ejemplo n.º 1
0
def conversion(video_path_A):
    if len(sys.argv)<=2:
        output_video_path_A = "./Aout.mp4"
    elif len(sys.argv)==3:
        output_video_path_A = sys.argv[3]

    K.set_learning_phase(0)

    # Input/Output resolution
    RESOLUTION = 64 # 64x64, 128x128, 256x256
    assert (RESOLUTION % 64) == 0, "RESOLUTION should be 64, 128, 256"

    # Architecture configuration
    arch_config = {}
    arch_config['IMAGE_SHAPE'] = (RESOLUTION, RESOLUTION, 3)
    arch_config['use_self_attn'] = True
    arch_config['norm'] = "instancenorm" # instancenorm, batchnorm, layernorm, groupnorm, none
    arch_config['model_capacity'] = "standard" # standard, lite

    model = FaceswapGANModel(**arch_config)

    model.load_weights(path="./models")

    mtcnn_weights_dir = "./mtcnn_weights/"

    fd = MTCNNFaceDetector(sess=K.get_session(), model_path=mtcnn_weights_dir)
    vc = VideoConverter()

    vc.set_face_detector(fd)
    vc.set_gan_model(model)

    options = {
        # ===== Fixed =====
        "use_smoothed_bbox": True,
        "use_kalman_filter": True,
        "use_auto_downscaling": False,
        "bbox_moving_avg_coef": 0.65,
        "min_face_area": 35 * 35,
        "IMAGE_SHAPE": model.IMAGE_SHAPE,
        # ===== Tunable =====
        "kf_noise_coef": 3e-3,
        "use_color_correction": "hist_match",
        "detec_threshold": 0.7,
        "roi_coverage": 0.9,
        "enhance": 0.5,
        "output_type": 3,
        "direction": "AtoB",
    }

    input_fn = video_path_A
    output_fn = output_video_path_A
    duration = None
    vc.convert(input_fn=input_fn, output_fn=output_fn, options=options, duration=duration)
Ejemplo n.º 2
0
        "min_face_area": 128 * 128,
        "IMAGE_SHAPE": model.IMAGE_SHAPE,
        # ===== Tunable =====
        "kf_noise_coef": 1e-3,
        "use_color_correction": "hist_match",
        "detec_threshold": 0.8,
        "roi_coverage": 0.92,
        "enhance": 0.,
        "output_type": 1,
        "direction":
        "BtoA",  # ==================== This line determines the transform direction ====================
    }

    model.load_weights(path=models_dir)
    fd = MTCNNFaceDetector(sess=K.get_session(), model_path="./mtcnn_weights/")
    vc = VideoConverter()
    vc.set_face_detector(fd)
    vc.set_gan_model(model)
    vc._init_kalman_filters(options["kf_noise_coef"])

    # fn_source_video = "obama_new.mp4"
    # fn_target_video = 'zyl.mp4'
    #
    # if options["direction"] == "AtoB":
    #     input_fn = fn_source_video
    #     output_fn = "OUTPUT_VIDEO_AtoB.mp4"
    # elif options["direction"] == "BtoA":
    #     input_fn = fn_target_video
    #     output_fn = "OUTPUT_VIDEO_BtoA.mp4"
    #
    # duration = None  # None or a non-negative float tuple: (start_sec, end_sec). Duration of input video to
model = FaceswapGANModel(**arch_config)

model.load_weights(path="./endeweight")

from converter.video_converter import VideoConverter

from detector.face_detector import MTCNNFaceDetector

mtcnn_weights_dir = "./weights/"

fd = MTCNNFaceDetector(sess=K.get_session(), model_path=mtcnn_weights_dir)

frames = 0
x0 = x1 = y0 = y1 = 0
vc = VideoConverter(x0, x1, y0, y1, frames)

vc.set_face_detector(fd)
vc.set_gan_model(model)

options = {
    # ===== Fixed =====
    "use_smoothed_bbox": True,
    "use_kalman_filter": True,
    "use_auto_downscaling": True,
    "bbox_moving_avg_coef": 0.60,
    "min_face_area": 40 * 40,
    "IMAGE_SHAPE": model.IMAGE_SHAPE,
    # ===== Tunable =====
    "kf_noise_coef": 5e-2,
    "use_color_correction": "adain_xyz",  #"hist_match",#adain_xyz
Ejemplo n.º 4
0
def face_transform():
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # 不全部占满显存, 按需分配
    session = tf.Session(config=config)
    # 设置session
    K.set_session(session)
    models_dir = "../models"
    RESOLUTION = 256  # 64x64, 128x128, 256x256
    # Architecture configuration
    arch_config = {}
    arch_config['IMAGE_SHAPE'] = (RESOLUTION, RESOLUTION, 3)
    arch_config['use_self_attn'] = True

    arch_config[
        'norm'] = "instancenorm"  # instancenorm, batchnorm, layernorm, groupnorm, none
    arch_config['model_capacity'] = "standard"  # standard, lite

    model = FaceswapGANModel(**arch_config)

    options = {
        # ===== Fixed =====
        "use_smoothed_bbox": True,
        "use_kalman_filter": True,
        "use_auto_downscaling": False,
        "bbox_moving_avg_coef": 0.70,  # 0.65
        "min_face_area": 128 * 128,
        "IMAGE_SHAPE": model.IMAGE_SHAPE,
        # ===== Tunable =====
        "kf_noise_coef": 1e-3,
        "use_color_correction": "hist_match",
        "detec_threshold": 0.8,
        "roi_coverage": 0.92,
        "enhance": 0.,
        "output_type": 1,
        "direction":
        "BtoA",  # ==================== This line determines the transform direction ====================
    }

    model.load_weights(path=models_dir)
    fd = MTCNNFaceDetector(sess=K.get_session(),
                           model_path="../mtcnn_weights/")
    vc = VideoConverter()
    vc.set_face_detector(fd)
    vc.set_gan_model(model)
    vc._init_kalman_filters(options["kf_noise_coef"])

    def transform(imageB64):
        """
        :param imageB64: 图片base64编码
        :return: 转换后的图片base64编码
        """
        rgb_img = base64_to_image(imageB64)

        result = vc.process_video(rgb_img, options)
        result = normalization(result)

        r, g, b = cv2.split(result)
        img_bgr = cv2.merge([b, g, r])
        return np.array(result).tolist()
        # bgr base64
        # res_base64 = image_to_base64(img_bgr)
        # return res_base64

    return transform
Ejemplo n.º 5
0
def transform_img(inStack, outStack):
    print('Process to transform: %s    ' % os.getpid(), time.time())

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True  # 不全部占满显存, 按需分配
    session = tf.Session(config=config)
    # 设置session
    K.set_session(session)

    models_dir = "../models_transform"
    RESOLUTION = 256  # 64x64, 128x128, 256x256
    # Architecture configuration
    arch_config = {}
    arch_config['IMAGE_SHAPE'] = (RESOLUTION, RESOLUTION, 3)
    arch_config['use_self_attn'] = True
    # TODO 归一化设置
    arch_config[
        'norm'] = "instancenorm"  # instancenorm, batchnorm, layernorm, groupnorm, none
    arch_config['model_capacity'] = "standard"  # standard, lite

    model = FaceswapGANModel(**arch_config)

    options = {
        # ===== Fixed =====
        "use_smoothed_bbox": True,
        "use_kalman_filter": True,
        "use_auto_downscaling": False,
        "bbox_moving_avg_coef": 0.65,  # 0.65
        "min_face_area": 35 * 35,
        "IMAGE_SHAPE": model.IMAGE_SHAPE,
        # ===== Tunable =====
        "kf_noise_coef": 1e-3,
        "use_color_correction": "hist_match",
        "detec_threshold": 0.8,
        "roi_coverage": 0.90,
        "enhance": 0.,
        "output_type": 1,
        "direction":
        "BtoA",  # ==================== This line determines the transform direction ====================
    }

    model.load_weights(path=models_dir)
    fd = MTCNNFaceDetector(sess=K.get_session(),
                           model_path="../mtcnn_weights/")
    vc = VideoConverter()
    vc.set_face_detector(fd)
    vc.set_gan_model(model)
    vc._init_kalman_filters(options["kf_noise_coef"])

    while True:
        if len(inStack) != 0:
            start_time = time.time()
            rgb_img = inStack.pop()
            # print("transform_img inputQ size ;",inputQ.qsize())
            # 获取转换后的人脸
            result = vc.process_video(rgb_img, options)
            result = normalization(result) * 255
            result = np.uint8(result)

            print(time.time() - start_time)
            outStack.append(result)