美文网首页大数据 爬虫Python AI SqlPython小哥哥
Python 让蔡徐坤在我的命令行里打篮球!

Python 让蔡徐坤在我的命令行里打篮球!

作者: 14e61d025165 | 来源:发表于2019-07-04 15:26 被阅读1次
    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1562225140065" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; 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-align: left; 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>

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

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

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

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

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1562225152395 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>

    Python学习交流群:1004391443

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

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

    <tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1562225152400 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>

    实验

    实验开始前我们需要安装 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;">pip install opencv-python
    </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;">def genCharVideo(self, filepath):
    self.charVideo =[]

    用opencv读取视频

    cap = cv2.VideoCapture(filepath)
    self.timeInterval = round(1/ cap.get(5),3)
    nf = int(cap.get(7))
    print('Generate char video, please wait...')
    for i in pyprind.prog_bar(range(nf)):

    转换颜色空间,第二个参数是转换类型,cv2.COLOR_BGR2GRAY表示从BGR↔Gray

    rawFrame = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
    frame = self.convert(rawFrame, os.get_terminal_size(), fill=True)
    self.charVideo.append(frame)
    cap.release()
    </pre>

    这里的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;">ascii_frame
    ascii_char ="$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,"^`'. "

    像素映射到字符

    def pixelToChar(self, luminance):
    return self.ascii_char[int(luminance /256* len(self.ascii_char))]

    将普通帧转为 ASCII 字符帧

    def convert(self, img, limitSize=-1, fill=False, wrap=False):
    if limitSize !=-1and(img.shape[0]> limitSize[1]or img.shape[1]>
    limitSize[0]):
    img = cv2.resize(img, limitSize,
    interpolation=cv2.INTER_AREA)
    ascii_frame =''
    blank =''
    if fill:
    blank +=' '*(limitSize[0]- img.shape[1])
    if wrap:
    blank +='\n'
    for i in range(img.shape[0]):
    for j in range(img.shape[1]):
    ascii_frame += self.pixelToChar(img[i, j])
    ascii_frame += blank
    return 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;"># 创建线程
    getchar = threading.Thread(target=getChar)

    设置为守护线程

    getchar.daemon =True

    启动守护线程

    getchar.start()

    输出的字符画行数

    rows = len(self.charVideo[0])// os.get_terminal_size()[0]
    for frame in self.charVideo:
    # 接收到输入则退出循环
    if breakflag:
    break
    self.streamOut(frame)
    self.streamFlush()
    time.sleep(self.timeInterval)
    # 共 rows 行,光标上移 rows-1 行回到开始处
    self.streamOut('\033[{}A\r'.format(rows -1))
    # 光标下移 rows-1 行到最后一行,清空最后一行
    self.streamOut('\033[{}B\033[K'.format(rows -1))
    # 清空最后一帧的所有行(从倒数第二行起)
    for i in range(rows -1):
    # 光标上移一行
    self.streamOut('\033[1A')
    # 清空光标所在行
    self.streamOut('\r\033[K')
    if breakflag:
    self.streamOut('User interrupt!\n')
    else:
    self.streamOut('Finished!\n')
    </pre>

    执行

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

    相关文章

      网友评论

        本文标题:Python 让蔡徐坤在我的命令行里打篮球!

        本文链接:https://www.haomeiwen.com/subject/zukrhctx.html