Audio-driven-TalkingFace-HeadPose icon indicating copy to clipboard operation
Audio-driven-TalkingFace-HeadPose copied to clipboard

how to transfer alpha_blend_newsold.m to python?

Open ghost opened this issue 4 years ago • 5 comments

how to transfer alpha_blend_newsold.m to python?

ghost avatar Dec 07 '20 03:12 ghost

如何将alpha_blend_newsold.m传输到python? I also encountered the same problem, have you solved it?

Kenny-Li2023 avatar Dec 22 '20 11:12 Kenny-Li2023

I borrow it from other issue. The python code for alpha_blend_newsold.m

import sys
import glob
import os
import cv2
import numpy as np
video = sys.argv[1]
starti = int(sys.argv[2])
framenum = int(sys.argv[3])
srcdir = os.path.join('render',video)
tardir = os.path.join('render',video,'bm')
if not os.path.exists(tardir):
    os.makedirs(tardir)
for i in range(starti,starti+framenum):
    file = 'frame%s.png'% i
    filepath = os.path.join(srcdir,file)
    im1 = cv2.imread(filepath)
    im2 = cv2.imread(os.path.join(srcdir,file[:-4]+'_render.png'),cv2.IMREAD_UNCHANGED)
    alpha = im2[:,:,3]
    contours,hierarchy  = cv2.findContours(alpha,mode=cv2.RETR_TREE,method=cv2.CHAIN_APPROX_SIMPLE)
    mask = np.zeros_like(im1)
    cv2.drawContours(mask,contours,0,(1,1,1),-1)
    im = im1*(1-mask)+im2[:,:,:3]*mask
    print(os.path.join(tardir,file[:-4]+'_renderold_bm.png'))
    cv2.imwrite(os.path.join(tardir,file[:-4]+'_renderold_bm.png'),im)

willing2020 avatar Jan 12 '21 12:01 willing2020

The python code for alpha_blend_vbg.m

import sys
import glob
import os
import cv2
import numpy as np

srcdirbg = sys.argv[1]
srcdir = sys.argv[2]

files = glob.glob(os.path.join(srcdir, '*.png'))
for file in files:
    file = os.path.basename(file)
    if len(file) >= 10 and file[-10:] == '_blend.png':
        continue
    if len(file) >= 11 and file[-11:] == '_blend2.png':
        continue

    im1 = cv2.imread(os.path.join(srcdirbg, file))
    im2 = cv2.imread(os.path.join(srcdir, file), cv2.IMREAD_UNCHANGED)
    alpha = im2[:, :, 3]
    contours, hierarchy = cv2.findContours(alpha, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
    mask = np.zeros_like(im1)
    cv2.drawContours(mask,contours,0,(1,1,1),-1)
    im = im1*(1-mask)+im2[:,:,:3]*mask
    tarname = file[0:-4] + '_blend2.png'
    cv2.imwrite(os.path.join(srcdir, tarname), im)

willing2020 avatar Jan 12 '21 12:01 willing2020

Hello, how do you need to modify the code in train_19news_1.py after converting the matlab code (alpha_blend.m/alpha_blend_vlog.m ) into python code?

15458wew avatar Mar 30 '21 03:03 15458wew

Excuse me, could you tell me the version of matlab you use?

xiaobairui avatar Aug 12 '21 07:08 xiaobairui