该项目基于PaddleHub2.4.0,实现动态视频转漫画书功能。以人民美术出版社版《水浒传》连环画为风格学习数据,借助其预训练模型,通过安装依赖、处理数据、图片转漫画、视频关键帧转漫画等步骤,抽取视频关键帧并转为漫画风格,为视频创作提供新形式,展现了AI在文化创意领域的潜力。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

一键运行
在数字化时代,视频内容的创作与消费呈现出爆炸式增长,从个人Vlog到专业影视作品,无一不丰富着我们的视觉体验。然而,随着技术的进步和审美需求的多样化,用户对于视频内容的呈现形式提出了更高要求,渴望在保持原视频动态魅力的同时,探索更多创意与个性化的表达方式。漫画作为一种独特的艺术形式,以其夸张的表现手法、丰富的色彩运用和简洁的叙事风格,深受全球观众喜爱。因此,将动态视频转化为漫画风格,不仅能够为视频内容增添新的视觉维度,还能激发观众的无限想象,满足他们对创意内容的渴求。
基于这一背景,我们提出了“基于paddlehub2.4.0: 让动态视频生成漫画书”项目。该项目旨在利用PaddleHub这一强大的预训练模型管理工具,结合深度学习技术,实现将普通动态视频自动转化为具有漫画风格视频或漫画书的功能。PaddleHub作为百度飞桨(PaddlePaddle)的深度学习平台中的模型管理和迁移学习工具,提供了丰富的预训练模型,能够极大地降低开发门槛,加速模型部署和应用。特别是随着PaddleHub 2.4.0版本的发布,其在模型性能、易用性和扩展性方面均有了显著提升,为本项目提供了坚实的技术支撑。
水浒传连环画,人民美术出版社版。png格式,压缩为index.zip,可以用于学习漫画风格。
# 安装 paddlehub!pip install paddlehub !hub install stylepro_artistic
%%capture !unzip /home/aistudio/data/data93870/index.zip -d /home/aistudio/work/style
#定义函数计算单通道的直方图的相似值,越小差异越大def calculate_delta(image1=False, image2=False):
if type(image1)!=type(image2) :
degree = [1e-10] else: # 灰度直方图算法
# 计算单通道的直方图的相似值
hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0])
hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) # 计算直方图的重合度
degree = 0
for i in range(len(hist1)): if hist1[i] != hist2[i]:
degree = degree + \
(1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i])) else:
degree = degree + 1
degree = degree / len(hist1) while type(degree)==type([]):
degree=degree[0]
degree=[degree] return degree
plt.subplot(1, 4, 1)
img1 = load_comic_photo('/home/aistudio/work/style/06/0012.png')
plt.imshow( cv2.cvtColor( img1 ,cv2.COLOR_BGR2RGB) )
plt.axis('off')
plt.subplot(1, 4, 2)
img2 = load_comic_photo('/home/aistudio/work/style/05/0013.png')
plt.imshow( cv2.cvtColor( img2 ,cv2.COLOR_BGR2RGB) )
plt.axis('off')
plt.subplot(1, 4, 3)
img3 = load_comic_photo('/home/aistudio/work/style/05/0014.png')
plt.imshow( cv2.cvtColor( img3 ,cv2.COLOR_BGR2RGB) )
plt.axis('off')
plt.subplot(1, 4, 4)
img4 = load_comic_photo('/home/aistudio/work/style/05/0015.png')
plt.imshow( cv2.cvtColor( img4 ,cv2.COLOR_BGR2RGB) )
plt.axis('off')
plt.tight_layout() # 自动调整子图参数 plt.show()<Figure size 640x480 with 4 Axes>
import paddlehub as hub#图片转为漫画stylepro_artistic = hub.Module(name="stylepro_artistic") #加载预训练模型def get_cartoon_img(src_img_data):
result = stylepro_artistic.style_transfer(
images=[{ 'content': src_img_data , 'styles': [load_comic_photo('/home/aistudio/work/style/05/0013.png')
,load_comic_photo('/home/aistudio/work/style/07/0015.png')
]
}]
)
t = result[0]['data']
t = cv2.cvtColor( t,cv2.COLOR_RGB2GRAY) # 先要转换为灰度图片
t = cv2.medianBlur(t,3) return t[2024-09-06 21:17:28,641] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object
## 将视频转换为图片,抽取关键帧import osimport cv2#显示图片def showimg(img, isgray=False):
plt.axis("off") if isgray == True:
plt.imshow(img, cmap='gray') else:
plt.imshow(img)
plt.show()def get_video_key_frame(video_src,frame_path='',show_key_frame=False,show_comic_key_frame=False):
if frame_path and not os.path.exists(frame_path):
os.mkdir(frame_path) if frame_path and not os.path.exists(frame_path+'results/'):
frame_path_ = frame_path+'results/'
os.mkdir(frame_path_)
vc = cv2.VideoCapture(video_src)
i_imp =-1000 #上一次保存的帧号
(frame_last,delta_last) =(False,0)#上一帧图、上一帧的差异指数
if vc.isOpened():
rval, frame = vc.read() else:
rval = False
i = 0 #当前帧号
while rval:
rval, frame = vc.read() if rval:
delta = calculate_delta(frame_last, frame )[0] # 计算差异程度
pct = abs(delta - delta_last)/(delta+0.000001) #差异程度的变化量
if pct>0.2 and i-i_imp>10: #差异程度变化很大,且间隔半秒以上
if show_key_frame: None
if frame_path:
cv2.imwrite(frame_path+'img_{}.jpg'.format(i), frame) if show_comic_key_frame:
frame_cartoon = get_cartoon_img(frame) if frame_path:
cv2.imwrite(frame_path+'results/'+'img_{}.jpg'.format(i), frame_cartoon)
i_imp = i
(frame_last,delta_last) = ( frame , delta )
i += 1video = 'work/shipin.mp4'frame_path = 'work/mov/picture/'# 抽取关键帧get_video_key_frame(video,frame_path,show_key_frame=True,show_comic_key_frame=False)# 关键帧转为漫画get_video_key_frame(video,frame_path,show_key_frame=False,show_comic_key_frame=True)
!mv /home/aistudio/work/mov/picture/results /home/aistudio/work/movdef sort_images(img_folder):
"""按文件名中的数字排序图片"""
# 使用glob匹配所有jpg图片
img_paths = [os.path.join(img_folder, img) for img in os.listdir(img_folder) if img.endswith('.jpg')]
# 按文件名中的数字排序
img_paths.sort(key=lambda x: int(os.path.splitext(os.path.basename(x))[0].split('_')[1]))
return img_paths
img_list_1 = sort_images("/home/aistudio/work/mov/picture")
img_list_2 = sort_images("/home/aistudio/work/mov/results")print("img_list_1:",img_list_1)print("img_list_2:",img_list_2)img_list_1: ['/home/aistudio/work/mov/picture/img_1.jpg', '/home/aistudio/work/mov/picture/img_121.jpg', '/home/aistudio/work/mov/picture/img_253.jpg', '/home/aistudio/work/mov/picture/img_393.jpg', '/home/aistudio/work/mov/picture/img_476.jpg', '/home/aistudio/work/mov/picture/img_540.jpg', '/home/aistudio/work/mov/picture/img_764.jpg', '/home/aistudio/work/mov/picture/img_850.jpg', '/home/aistudio/work/mov/picture/img_885.jpg', '/home/aistudio/work/mov/picture/img_904.jpg', '/home/aistudio/work/mov/picture/img_929.jpg', '/home/aistudio/work/mov/picture/img_990.jpg', '/home/aistudio/work/mov/picture/img_1002.jpg', '/home/aistudio/work/mov/picture/img_1037.jpg', '/home/aistudio/work/mov/picture/img_1106.jpg', '/home/aistudio/work/mov/picture/img_1121.jpg', '/home/aistudio/work/mov/picture/img_1145.jpg', '/home/aistudio/work/mov/picture/img_1162.jpg'] img_list_2: ['/home/aistudio/work/mov/results/img_1.jpg', '/home/aistudio/work/mov/results/img_121.jpg', '/home/aistudio/work/mov/results/img_253.jpg', '/home/aistudio/work/mov/results/img_393.jpg', '/home/aistudio/work/mov/results/img_476.jpg', '/home/aistudio/work/mov/results/img_540.jpg', '/home/aistudio/work/mov/results/img_764.jpg', '/home/aistudio/work/mov/results/img_850.jpg', '/home/aistudio/work/mov/results/img_885.jpg', '/home/aistudio/work/mov/results/img_904.jpg', '/home/aistudio/work/mov/results/img_929.jpg', '/home/aistudio/work/mov/results/img_990.jpg', '/home/aistudio/work/mov/results/img_1002.jpg', '/home/aistudio/work/mov/results/img_1037.jpg', '/home/aistudio/work/mov/results/img_1106.jpg', '/home/aistudio/work/mov/results/img_1121.jpg', '/home/aistudio/work/mov/results/img_1145.jpg', '/home/aistudio/work/mov/results/img_1162.jpg']
import cv2
import matplotlib.pyplot as plt
for i in range(len(img_list_1)): # 读取图片
image1 = cv2.imread(img_list_1[i]) # 替换为你的图片路径
image2 = cv2.imread(img_list_2[i]) # 替换为你的图片路径
image1_rgb = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
image2_rgb = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
# 使用matplotlib显示图片
fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # 创建一个1行2列的图形
axes[0].imshow(image1_rgb) # 显示第一张图片
axes[0].axis('off') # 关闭坐标轴
axes[1].imshow(image2_rgb) # 显示第二张图片
axes[1].axis('off') # 关闭坐标轴
plt.show()<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
<Figure size 1000x500 with 2 Axes>
本项目成功利用PaddleHub 2.4.0的深度学习技术,实现了动态视频到漫画风格视频的自动化转换,为视频内容创作带来了全新的表现形式,展现了AI技术在文化创意领域的巨大潜力。
通过漫画风格的视频生成,极大地丰富了用户的视觉体验,使视频内容更加生动有趣,满足了用户对创意内容的多样化需求,提升了用户满意度。
本项目为AI技术在文化创意产业的应用提供了有力支持,展示了AI技术在提升内容创作效率、丰富内容表现形式方面的巨大价值,为行业带来了新的增长点和发展机遇。
以上就是基于paddlehub2.4.0: 让动态视频生成漫画书!【一键运行】的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号