鱼C论坛

 找回密码
 立即注册
查看: 2802|回复: 1

[技术交流] 让cxk在你的控制台打篮球

[复制链接]
发表于 2020-3-6 15:42:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
刚刚看完小甲鱼零基础python,想学学别的东西。
大概流程是先按帧数截取图片,再把这些图片转化成字符串,最后按帧输出。在cmd运行就行了
边学边练好像不能添加附件,那就网盘吧
链接: https://pan.baidu.com/s/1FJ0RnKLOLSogBd-W1-icrA 提取码: f9bw

  1. import os
  2. import time
  3. import cv2
  4. import pyprind


  5. class CharFrame:
  6.     ascii_char = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,"^`'. "

  7.     # 像素映射到字符
  8.     def pixelToChar(self, luminance):
  9.         return self.ascii_char[int(luminance / 256 * len(self.ascii_char))]

  10.     # 将普通帧转为 ASCII 字符帧
  11.     def convert(self, img, limitSize):

  12.         img = cv2.resize(img, limitSize, interpolation=cv2.INTER_AREA)

  13.         ascii_frame = ''
  14.         blank = ''
  15.         for i in range(img.shape[0]):

  16.             for j in range(img.shape[1]):
  17.               ascii_frame += self.pixelToChar(img[i, j])
  18.             ascii_frame += blank
  19.         return ascii_frame


  20. class V2Char(CharFrame):
  21.     charVideo = []
  22.     timeInterval = 0.033

  23.     def __init__(self, path):
  24.         self.genCharVideo(path)

  25.     def genCharVideo(self, filepath):
  26.         self.charVideo = []

  27.         cap = cv2.VideoCapture(filepath)
  28.         self.timeInterval = round(1 / cap.get(5), 3)#帧速率

  29.         nf = int(cap.get(7))#得到文件的总帧数
  30.         print('Generate char video, please wait...')
  31.         for i in pyprind.prog_bar(range(nf)):
  32.             # 转换颜色空间,第二个参数是转换类型,cv2.COLOR_BGR2GRAY表示从BGR&#8596;Gray
  33.             rawFrame = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)

  34.             frame = self.convert(rawFrame, os.get_terminal_size())
  35.             self.charVideo.append(frame)
  36.         cap.release()

  37.     def play(self):
  38.         for frame in self.charVideo:
  39.            print(frame)
  40.            time.sleep(self.timeInterval)

  41. if __name__ == '__main__':
  42.     v2char = V2Char('vedio.mp4')
  43.     v2char.play()
复制代码






小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-20 23:57:48 | 显示全部楼层
运行不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 18:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表