大数据 爬虫Python AI SqlPython小哥哥

抖音为啥蔡徐坤打篮球火了?Python让蔡徐坤在我的命令行里打篮

2019-04-12  本文已影响0人  14e61d025165

【导语】作者自称是一个经常逛 B 站的肥宅。最近 B 站上流行的视频素材除了“换脸”,其次就要属“蔡xx打球”视频了。有模仿的、对比的、手绘的... ...更过分的是,竟然有人在命令行输出了他的打球视频。不过,视频中的动画好像是用某个软件生成的 txt 文件,作者就在想既然都可以用 txt 输出了,能不能用 python 在命令行中显示呢?

说到这作者便开始搜索资料,做后制作了下面一段视频“

文字视频

更换封面

00:32 视频尚未发布,暂时无法播放

代码是自己在网上查询资料后自己修改的,本着学习和分享的精神,今天就来分享下上面这段视频的制作过程。

原理

既然要开始做东西,首要的问题就是想好要怎么做,大家都知道视频是由一系列图片一帧一帧组成的,因此视频转字符动画最基本的便是图片转字符画。

在这里简单的说一下图片转字符画的原理:首先将图片转为灰度图,每个像素都只有亮度信息(用 0~255 表示)。然后我们构建一个有限字符集合,其中的每一个字符都与一段亮度范围对应,我们便可以根据此对应关系以及像素的亮度信息把每一个像素用对应的字符表示,这样字符画就形成了。

<bi style="box-sizing: border-box; display: block;">Tips:如果对"灰度图像"这个概念不太理解的可以查阅百度百科</bi>

计算一张图片的灰度图像的方法如下(来自百度百科):

<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1555050835384 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"> image

<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>

所以我们要做的就只是让字符画在命令行里面动起来就可以了。

<bi style="box-sizing: border-box; display: block;">Tips:图片转字符画可以参考:https://www.shiyanlou.com/courses/370</bi>

准备

环境和工具:vscode、Mac OS、python 3.7

这次实验使用到的核心的库是opencv-python

<bi style="box-sizing: border-box; display: block;">Tips:这里分享一个我觉得还不错的opencv-python的中文文档:</bi><bi style="box-sizing: border-box; display: block;">https://www.kancloud.cn/aollo/aolloopencv/269602</bi>

实验

实验开始前我们需要安装 opencv-python 的包:

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1pip install opencv
</pre>

读取视频:

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1def genCharVideo(self, filepath):
2
3self.charVideo =[]
4
5# 用opencv读取视频
6
7cap = cv2.VideoCapture(filepath)
8
9self.timeInterval = round(1/ cap.get(5),3)
10
11nf = int(cap.get(7))
12
13print('Generate char video, please wait...')
14
15for i in pyprind.prog_bar(range(nf)):
16
17# 转换颜色空间,第二个参数是转换类型,cv2.COLOR_BGR2GRAY表示从BGR↔Gray
18
19rawFrame = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
20
21frame = self.convert(rawFrame, os.get_terminal_size(), fill=True)
22
23self.charVideo.append(frame)
24
25cap.release()
</pre>

Python学习群:683380553,有大牛答疑,有资源共享!有想学习python编程的,或是转行,或是大学生,还有工作中想提升自己能力的,正在学习的小伙伴欢迎加入学习。

这里的VideoCapture是用来读取视频的, cv2.cvtColor(input_imageflag)用于转换颜色空间,其中flag就是转换类型。对于 BGR↔Gray 的转换,我们使用的 flag 就是 cv2.COLORBGR2GRAY。对于 BGR↔HSV 的转换我们用的 flag 就是 cv2.COLORBGR2HSV。

将帧转换成字符画

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1ascii_frame
2
3ascii_char ="$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,"^`'. "
4
5
6
7# 像素映射到字符
8
9def pixelToChar(self, luminance):
10
11return self.ascii_char[int(luminance /256
len(self.ascii_char))]
12
13
14
15# 将普通帧转为 ASCII 字符帧
16
17def convert(self, img, limitSize=-1, fill=False, wrap=False):
18
19if limitSize !=-1and(img.shape[0]> limitSize[1]or img.shape[1]> limitSize[0]):
20
21img = cv2.resize(img, limitSize, interpolation=cv2.INTER_AREA)
22
23ascii_frame =''
24
25blank =''
26
27if fill:
28
29blank +=' '*(limitSize[0]- img.shape[1])
30
31if wrap:
32
33blank +='\n'
34
35for i in range(img.shape[0]):
36
37for j in range(img.shape[1]):
38
39ascii_frame += self.pixelToChar(img[i, j])
40
41ascii_frame += blank
42
43return ascii_frame
</pre>

这段代码其实就是将已经转变的灰度图的像素值映射到 ascii_char上,然后输出到控制台。

控制输出

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1# 创建线程
2
3getchar = threading.Thread(target=getChar)
4
5# 设置为守护线程
6
7getchar.daemon =True
8
9# 启动守护线程
10
11getchar.start()
12
13# 输出的字符画行数
14
15rows = len(self.charVideo[0])// os.get_terminal_size()[0]
16
17for frame in self.charVideo:
18
19# 接收到输入则退出循环
20
21if breakflag:
22
23break
24
25self.streamOut(frame)
26
27self.streamFlush()
28
29time.sleep(self.timeInterval)
30
31# 共 rows 行,光标上移 rows-1 行回到开始处
32
33self.streamOut('\033[{}A\r'.format(rows -1))
34
35# 光标下移 rows-1 行到最后一行,清空最后一行
36
37self.streamOut('\033[{}B\033[K'.format(rows -1))
38
39# 清空最后一帧的所有行(从倒数第二行起)
40
41for i in range(rows -1):
42
43# 光标上移一行
44
45self.streamOut('\033[1A')
46
47# 清空光标所在行
48
49self.streamOut('\r\033[K')
50
51if breakflag:
52
53self.streamOut('User interrupt!\n')
54
55else:
56
57self.streamOut('Finished!\n')
</pre>

执行

最后在main函数中设置下要读取的文件名,再play一下就可以了

<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1if
2__name__ =='main':
3
4v2char = V2Char('vedio.mp4')
5
6v2char.play()
</pre>

上一篇下一篇

猜你喜欢

热点阅读