鱼C论坛

 找回密码
 立即注册
查看: 1662|回复: 9

[已解决]关于路径问题

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

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

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

x
报错:    import pyprind
ModuleNotFoundError: No module named 'pyprind'。是什么意思?怎么处理?求大神支招!!!下附代码:

  1. import sys
  2. import os
  3. import time
  4. import threading
  5. import cv2
  6. import pyprind


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

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

  12.     # 将普通帧转为 ASCII 字符帧
  13.     def convert(self, img, limitSize=-1, fill=False, wrap=False):
  14.         if limitSize != -1 and (img.shape[0] > limitSize[1] or img.shape[1] > limitSize[0]):
  15.             img = cv2.resize(img, limitSize, interpolation=cv2.INTER_AREA)
  16.         ascii_frame = ''
  17.         blank = ''
  18.         if fill:
  19.             blank += ' ' * (limitSize[0] - img.shape[1])
  20.         if wrap:
  21.             blank += '\n'
  22.         for i in range(img.shape[0]):
  23.             for j in range(img.shape[1]):
  24.                 ascii_frame += self.pixelToChar(img[i, j])
  25.             ascii_frame += blank
  26.         return ascii_frame


  27. class V2Char(CharFrame):
  28.     charVideo = []
  29.     timeInterval = 0.033

  30.     def __init__(self, path):
  31.         if path.endswith('txt'):
  32.             self.load(path)
  33.         else:
  34.             self.genCharVideo(path)

  35.     def genCharVideo(self, filepath):
  36.         self.charVideo = []
  37.         # 用opencv读取视频
  38.         cap = cv2.VideoCapture(filepath)
  39.         self.timeInterval = round(1 / cap.get(5), 3)
  40.         nf = int(cap.get(7))
  41.         print('Generate char video, please wait...')
  42.         for i in pyprind.prog_bar(range(nf)):
  43.             # 转换颜色空间,第二个参数是转换类型,cv2.COLOR_BGR2GRAY表示从BGR&#8596;Gray
  44.             rawFrame = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2GRAY)
  45.             frame = self.convert(rawFrame, os.get_terminal_size(), fill=True)
  46.             self.charVideo.append(frame)
  47.         cap.release()

  48.     def export(self, filepath):
  49.         if not self.charVideo:
  50.             return
  51.         with open(filepath, 'w') as f:
  52.             for frame in self.charVideo:
  53.                 # 加一个换行符用以分隔每一帧
  54.                 f.write(frame + '\n')

  55.     def load(self, filepath):
  56.         self.charVideo = []
  57.         # 一行即为一帧
  58.         for i in open(filepath):
  59.             self.charVideo.append(i[:-1])

  60.     def play(self, stream=1):
  61.         # Bug:
  62.         # 光标定位转义编码不兼容 Windows
  63.         if not self.charVideo:
  64.             return
  65.         if stream == 1 and os.isatty(sys.stdout.fileno()):
  66.             self.streamOut = sys.stdout.write
  67.             self.streamFlush = sys.stdout.flush
  68.         elif stream == 2 and os.isatty(sys.stderr.fileno()):
  69.             self.streamOut = sys.stderr.write
  70.             self.streamFlush = sys.stderr.flush
  71.         elif hasattr(stream, 'write'):
  72.             self.streamOut = stream.write
  73.             self.streamFlush = stream.flush
  74.         breakflag = False

  75.         def getChar():
  76.             nonlocal breakflag
  77.             try:
  78.                 # 若系统为 windows 则直接调用 msvcrt.getch()
  79.                 import msvcrt
  80.             except ImportError:
  81.                 import termios
  82.                 import tty
  83.                 # 获得标准输入的文件描述符
  84.                 fd = sys.stdin.fileno()
  85.                 # 保存标准输入的属性
  86.                 old_settings = termios.tcgetattr(fd)
  87.                 try:
  88.                     # 设置标准输入为原始模式
  89.                     tty.setraw(sys.stdin.fileno())
  90.                     # 读取一个字符
  91.                     ch = sys.stdin.read(1)
  92.                 finally:
  93.                     # 恢复标准输入为原来的属性
  94.                     termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  95.                 if ch:
  96.                     breakflag = True
  97.             else:
  98.                 if msvcrt.getch():
  99.                     breakflag = True

  100.         # 创建线程
  101.         getchar = threading.Thread(target=getChar)
  102.         # 设置为守护线程
  103.         getchar.daemon = True
  104.         # 启动守护线程
  105.         getchar.start()
  106.         # 输出的字符画行数
  107.         rows = len(self.charVideo[0]) // os.get_terminal_size()[0]
  108.         for frame in self.charVideo:
  109.             # 接收到输入则退出循环
  110.             if breakflag:
  111.                 break
  112.             self.streamOut(frame)
  113.             self.streamFlush()
  114.             time.sleep(self.timeInterval)
  115.             # 共 rows 行,光标上移 rows-1 行回到开始处
  116.             self.streamOut('\033[{}A\r'.format(rows - 1))
  117.         # 光标下移 rows-1 行到最后一行,清空最后一行
  118.         self.streamOut('\033[{}B\033[K'.format(rows - 1))
  119.         # 清空最后一帧的所有行(从倒数第二行起)
  120.         for i in range(rows - 1):
  121.             # 光标上移一行
  122.             self.streamOut('\033[1A')
  123.             # 清空光标所在行
  124.             self.streamOut('\r\033[K')
  125.         if breakflag:
  126.             self.streamOut('User interrupt!\n')
  127.         else:
  128.             self.streamOut('Finished!\n')


  129. if __name__ == '__main__':
  130.     v2char = V2Char('vedio.mp4')
  131.     v2char.play()
复制代码
最佳答案
2020-3-21 00:18:16
鱼子酱POI 发表于 2020-3-20 23:43
搬来的代码,我也不是很了解

你好,我刚才的的回答逻辑是有问题的

你先 pip install pyprind 这个包,如果提示找不到,很大可能就是没有这个包
如果install successing 那么就是你没有安装这个包
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-20 22:35:28 | 显示全部楼层
有pyprind这个模块吗?
是pyprint吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-20 22:42:05 | 显示全部楼层
qiuyouzhi 发表于 2020-3-20 22:35
有pyprind这个模块吗?
是pyprint吧

好的,我试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-20 22:44:34 | 显示全部楼层
qiuyouzhi 发表于 2020-3-20 22:35
有pyprind这个模块吗?
是pyprint吧

还是一样报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-20 22:54:40 | 显示全部楼层
  1. import pyprind
  2. print(pyprind.path)
复制代码


这个库没见过,和pprint类似么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-20 23:26:16 | 显示全部楼层
先去命令行
  1. pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com pyprind
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +1 收起 理由
鱼子酱POI + 3 + 3 + 1

查看全部评分

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

使用道具 举报

 楼主| 发表于 2020-3-20 23:43:14 | 显示全部楼层
乍惊乍喜 发表于 2020-3-20 22:54
这个库没见过,和pprint类似么

搬来的代码,我也不是很了解
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-20 23:44:13 | 显示全部楼层

小白还是不太明白,用了大神你的方法,cmd最后报错还是一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-20 23:47:25 | 显示全部楼层
鱼子酱POI 发表于 2020-3-20 23:44
小白还是不太明白,用了大神你的方法,cmd最后报错还是一样

你得先安装了这个模块啊……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-21 00:18:16 | 显示全部楼层    本楼为最佳答案   
鱼子酱POI 发表于 2020-3-20 23:43
搬来的代码,我也不是很了解

你好,我刚才的的回答逻辑是有问题的

你先 pip install pyprind 这个包,如果提示找不到,很大可能就是没有这个包
如果install successing 那么就是你没有安装这个包
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-14 22:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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