总之是需要改不少代码才能跑起来。其中有个地方是 Path 这个对象,如果直接 str() 会把最后的 ‘/’ 去掉。
最后生成视频部分
这里重新写一下 make_gif.py 这里
首先分享一个保存视频的类
import cv2
import cv2 as cv
import os
def decode_fourcc(cc):
return "".join([chr((int(cc) >> 8 * i) & 0xFF) for i in range(4)])
def get_cap_info(cap):
# w, h,
arr=[ 3, 4, 5, 7 ]
arr= [ int( cap.get(x) ) for x in arr ]
#for i in [0, 1, 2, 4] : arr[i]=int(arr[i])
return arr + [ decode_fourcc( cap.get(6) ) ]
class Cls_video:
def init_by_cap(self, fvideo, cap_or_f, fourcc=None, verbos=False):
if isinstance(cap_or_f, str):
cap_or_f = cv.VideoCapture(cap_or_f)
infos = get_cap_info( cap_or_f )
if verbos: print("video info:", infos)
w, h, fps = infos[:3]
fcc = infos[-1]
if fourcc is not None: fcc = fourcc
#print(fcc)
return self.init(fvideo, fps=fps, wh=(w, h), fourcc=fcc)
def init(self, fvideo, fps=20, wh=(512, 512), fourcc='MJPG'):
fourcc = cv2.VideoWriter_fourcc(*fourcc) #(*'XVID')
cap_out = cv2.VideoWriter(fvideo, fourcc, fps, wh)
self.cap_out = cap_out
self.fvideo = fvideo
return self
def write(self, img): self.cap_out.write(img)
def close(self): self.cap_out.release()
def convert_by_ffmpeg(self, fout, vcodec='h264', fin=None, fwav=None, aug="-shortest"):
if fin is None: fin=self.fvideo
#os.system(f"ffmpeg -y -loglevel error -i {fin} -vcodec {vcodec} -c:a copy {fout}")
cmdbg="ffmpeg -y -loglevel error"
if fwav:
os.system(f"{cmdbg} -i {fin} -i {fwav} -vcodec {vcodec} -strict -2 {aug} {fout}")
else:
os.system(f"{cmdbg} -i {fin} -vcodec {vcodec} {aug} {fout}")
然后make_git.py 可以改成如下
import numpy as np
import matplotlib.pyplot as plt
import cv2
from pathlib import Path
import skimage
from skimage import io
# !!!【需要上面的 Cls_video】
source_dir = Path('./data/source/test_img')
target_dir = Path('./results/target/test_latest/images')
label_dir = Path('./data/source/test_label_ori')
source_img_paths = sorted(source_dir.iterdir())
target_synth_paths = sorted(target_dir.glob('*synthesized*'))
target_label_paths = sorted(label_dir.iterdir())
def main():
isfirst=True
for i in tqdm(range( len(target_label_paths) )):
img = animate(i)
if isfirst:
isfirst=False
wh=img.shape[:2]
wh = ( wh[1], wh[0] )
cls_v = Cls_video().init("data/res.avi", fps=24, wh=wh, fourcc="XVID")
cls_v.write(img)
cls_v.close()
cls_v.convert_by_ffmpeg( "data/res.mp4" )
def animate(nframe):
source_img = imread(source_img_paths[nframe])
target_label = imread(target_label_paths[nframe]) * 255
target_synth = imread(target_synth_paths[nframe])
arr = [ source_img, target_label, target_synth ]
img = np.hstack( ( source_img, target_label, target_synth ) )
#exit()
return img
def imread(fimg):
img = io.imread(fimg)
img = skimage.img_as_ubyte(img)
if len(img.shape)==2:
hw = img.shape
img = np.repeat(img, 3, axis=1)
img = img.reshape( hw[0], hw[1] , 3 )
return img[...,::-1]
if __name__=="__main__": main()