Python实现跳舞词云视频 Python编程实现小姐姐跳舞并生成词云视频示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python实现跳舞词云视频 Python编程实现小姐姐跳舞并生成词云视频示例

小张Python   2021-10-08 我要评论
想了解Python编程实现小姐姐跳舞并生成词云视频示例的相关内容吗,小张Python在本文为您仔细讲解Python实现跳舞词云视频的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Python编程,Python实现跳舞词云视频,下面大家一起来学习吧。

Python做了一个词云视频,以另一种角度来看小姐姐跳舞

制作过程分为以下几个部分

1、视频下载

首先需要下载一个小姐姐跳舞的视频,这里我用的是 you-get 工具,可借助 Python 的 pip 命令进行安装

pip install you-get

you-get 支持下载平台包括:Youtube、Blili、TED、腾讯、优酷、爱奇艺(涵盖所有视频平台下载链接),

以 youtube 视频为例,you-get 下载命令

you-get -o ~/Videos(存放视频路径) -O zoo.webm(视频命名) 'https://www.youtube.com/watch?v=jNQXAC9IVRw'

图片

这里通过 os 模块来实现 you-get 下载命令,使用时传入三个参数即可:

1,视频链接,

2,要存放视频的文件路径;

3,视频命名;

def download(video_url,save_path,video_name):
   '''
   youget 下载视频
   :param video_url:视频链接
   :param save_path: 保存路径
   :param video_name: 视频命名
   :return:
   '''
   cmd = 'you-get -o {} -O {} {}'.format(save_path,video_name,video_url)
   res = os.popen(cmd,)
   res.encoding = 'utf-8'
   print(res.read())# 打印输出

关于 you-get 更多用法, 可参考官网,里面关于用法介绍的非常详细:

https://you-get.org/#getting-started

图片

2、B 站弹幕下载

做词云图需要有文本数据支持,这里选取 B 站弹幕为素材;关于 B 站视频弹幕下载方式,这里一个快捷方法,用 requests 访问指定视频的 API 接口,就能得到该视频下的全部弹幕

http://comment.bilibili.com/{cid}.xml # cid 为B站视频的cid 编号

但 API 接口的构造,需要知道视频的 cid 编号

B站视频 cid 编号获取方式:

F12打开开发者模式->NetWork->XHR->v2?cid=… 链接 ,该网页链接中有一个”cid=一串数字“ 的字符串,其中等号后面的连续数字就是该视频的 cid 编号

图片

以上面视频为例,291424805 就是这个视频的 cid 编号,

有了 cid 之后,通过 requests 请求 API 接口,就能获取到里面的弹幕数据

http://comment.bilibili.com/291424805.xml

图片

图片

def download_danmu():
    '''弹幕下载并存储'''
    cid = '141367679'# video_id
    url = 'http://comment.bilibili.com/{}.xml'.format(cid)
    f = open('danmu.txt','w+',encoding='utf-8') #打开 txt 文件
    res = requests.get(url)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'lxml')
    items = soup.find_all('d')# 找到 d 标签
    for item in items:
        text = item.text
        print('---------------------------------'*10)
        print(text)
        seg_list = jieba.cut(text,cut_all =True)# 对字符串进行分词处理,方便后面制作词云图
        for j in seg_list:
            print(j)
            f.write(j)
            f.write('\n')
    f.close()

3、视频切帧,人像分割

下载到视频之后,先把视频拆分成一帧一帧图像;

vc = cv2.VideoCapture(video_path)
    c =0
    if vc.isOpened():
        rval,frame = vc.read()# 读取视频帧
    else:
        rval=False
    while rval:
        rval,frame = vc.read()# 读取每一视频帧,并保存至图片中

        cv2.imwrite(os.path.join(Pic_path,'{}.jpg'.format(c)),frame)
        c += 1
        print('第 {} 张图片存放成功!'.format(c))

图片

对每一帧中的小姐姐进行识别提取,也就是人像分割,这里借助了百度 API 接口,

APP_ID = "23633750"
    API_KEY = 'uqnHjMZfChbDHvPqWgjeZHCR'
    SECRET_KEY = '************************************'
    client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
    # 文件夹
    jpg_file = os.listdir(jpg_path)
    # 要保存的文件夹
    for i in jpg_file:
        open_file = os.path.join(jpg_path,i)
        save_file = os.path.join(save_path,i)
        if not os.path.exists(save_file):#文件不存在时,进行下步操作
            img = cv2.imread(open_file)  # 获取图像尺寸
            height, width, _ = img.shape
            if crop_path:# 若Crop_path 不为 None,则不进行裁剪
                crop_file = os.path.join(crop_path,i)
                img = img[100:-1,300:-400] #图片太大,对图像进行裁剪里面参数根据自己情况设定
                cv2.imwrite(crop_file,img)
                image= get_file_content(crop_file)
            else:

                image = get_file_content(open_file)
            res = client.bodySeg(image)#调用百度API 对人像进行分割
            labelmap = base64.b64decode(res['labelmap'])
            labelimg = np.frombuffer(labelmap,np.uint8)# 转化为np数组 0-255
            labelimg = cv2.imdecode(labelimg,1)
            labelimg = cv2.resize(labelimg,(width,height),interpolation=cv2.INTER_NEAREST)
            img_new = np.where(labelimg==1,255,labelimg)# 将 1 转化为 255
            cv2.imwrite(save_file,img_new)
            print(save_file,'save successfully')

将含有人像的图像转化为二值化图像,前景为人物,其余部分为背景

图片

API 使用之前需要用自己账号在百度智能云平台创建一个 人体分析 应用,里面需要三个参数:ID、AK、SK

图片

关于百度 API 使用方法,可参考官方文档资料

图片

4、对分割后的图像制作词云图

根据步骤 3 中得到了小姐姐 人像 Mask,

图片

借助 wordcloud 词云库和采集到的弹幕信息,对每一张二值化图像绘制词云图(在制作之前,请确保每一张都是二值化图像,全部为黑色像素图像需要剔除)

图片

word_list = []
    with open('danmu.txt',encoding='utf-8') as f:
        con = f.read().split('\n')# 读取txt文本词云文本
        for i in con:
            if re.findall('[\u4e00-\u9fa5]+', str(i), re.S): #去除无中文的词频
                word_list.append(i)
    for i in os.listdir(mask_path):
        open_file = os.path.join(mask_path,i)
        save_file = os.path.join(cloud_path,i)
        if not os.path.exists(save_file):
            # 随机索引前 start 频率词
            start = random.randint(0, 15)
            word_counts = collections.Counter(word_list)
            word_counts = dict(word_counts.most_common()[start:])
            background = 255- np.array(Image.open(open_file))
            wc =WordCloud(
                background_color='black',
                max_words=500,
                mask=background,
                mode = 'RGB',
                font_path ="D:/Data/fonts/HGXK_CNKI.ttf",# 设置字体路径,用于设置中文,

            ).generate_from_frequencies(word_counts)
            wc.to_file(save_file)
            print(save_file,'Save Sucessfully!')

5、图片拼接,合成视频

词云图全部生成完毕之后,如果一张一张图像看肯定没意思,如果把处理后的词云图合成视频会更酷一点!

为了视频前后对比效果这里我多加了一个步骤,在合并之前先对原图和词云图进行拼接,合成效果如下:

图片

 num_list = [int(str(i).split('.')[0]) for i in os.listdir(origin_path)]
    fps = 24# 视频帧率,越大越流畅
    height,width,_=cv2.imread(os.path.join(origin_path,'{}.jpg'.format(num_list[0]))).shape # 视频高度和宽度
    width = width*2
    # 创建一个写入操作;
    video_writer = cv2.VideoWriter(video_path,cv2.VideoWriter_fourcc(*'mp4v'),fps,(width,height))
    for i in sorted(num_list):
        i = '{}.jpg'.format(i)
        ori_jpg = os.path.join(origin_path,str(i))
        word_jpg = os.path.join(wordart_path,str(i))
        # com_jpg = os.path.join(Composite_path,str(i))
        ori_arr = cv2.imread(ori_jpg)
        word_arr = cv2.imread(word_jpg)
        # 利用 Numpy 进行拼接
        com_arr = np.hstack((ori_arr,word_arr))
        # cv2.imwrite(com_jpg,com_arr)# 合成图保存
        video_writer.write(com_arr) # 将每一帧画面写入视频流中
        print("{} Save Sucessfully---------".format(ori_jpg))

再加上背景音乐,视频又能提升一个档次~

最后

关于视频中的素材,特此声明

弹幕取自B站 Up 主 半佛仙人《【半佛】你知道奶茶加盟到底有多坑人吗?》

小姐姐跳舞视频取自Youtube Channel Lilifilm Official 《LILI's FILM #3 - LISA Dance Performance Video》

最后,感谢大家的阅读,我们下期见~

更多关于Python实现跳舞词云视频的资料请关注其它相关文章!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们