鱼子酱POI 发表于 2020-3-20 22:33:31

关于路径问题

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

import sys
import os
import time
import threading
import cv2
import pyprind


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

    # 像素映射到字符
    def pixelToChar(self, luminance):
      return self.ascii_char

    # 将普通帧转为 ASCII 字符帧
    def convert(self, img, limitSize=-1, fill=False, wrap=False):
      if limitSize != -1 and (img.shape > limitSize or img.shape > limitSize):
            img = cv2.resize(img, limitSize, interpolation=cv2.INTER_AREA)
      ascii_frame = ''
      blank = ''
      if fill:
            blank += ' ' * (limitSize - img.shape)
      if wrap:
            blank += '\n'
      for i in range(img.shape):
            for j in range(img.shape):
                ascii_frame += self.pixelToChar(img)
            ascii_frame += blank
      return ascii_frame


class V2Char(CharFrame):
    charVideo = []
    timeInterval = 0.033

    def __init__(self, path):
      if path.endswith('txt'):
            self.load(path)
      else:
            self.genCharVideo(path)

    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(), cv2.COLOR_BGR2GRAY)
            frame = self.convert(rawFrame, os.get_terminal_size(), fill=True)
            self.charVideo.append(frame)
      cap.release()

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

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

    def play(self, stream=1):
      # Bug:
      # 光标定位转义编码不兼容 Windows
      if not self.charVideo:
            return
      if stream == 1 and os.isatty(sys.stdout.fileno()):
            self.streamOut = sys.stdout.write
            self.streamFlush = sys.stdout.flush
      elif stream == 2 and os.isatty(sys.stderr.fileno()):
            self.streamOut = sys.stderr.write
            self.streamFlush = sys.stderr.flush
      elif hasattr(stream, 'write'):
            self.streamOut = stream.write
            self.streamFlush = stream.flush
      breakflag = False

      def getChar():
            nonlocal breakflag
            try:
                # 若系统为 windows 则直接调用 msvcrt.getch()
                import msvcrt
            except ImportError:
                import termios
                import tty
                # 获得标准输入的文件描述符
                fd = sys.stdin.fileno()
                # 保存标准输入的属性
                old_settings = termios.tcgetattr(fd)
                try:
                  # 设置标准输入为原始模式
                  tty.setraw(sys.stdin.fileno())
                  # 读取一个字符
                  ch = sys.stdin.read(1)
                finally:
                  # 恢复标准输入为原来的属性
                  termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                if ch:
                  breakflag = True
            else:
                if msvcrt.getch():
                  breakflag = True

      # 创建线程
      getchar = threading.Thread(target=getChar)
      # 设置为守护线程
      getchar.daemon = True
      # 启动守护线程
      getchar.start()
      # 输出的字符画行数
      rows = len(self.charVideo) // os.get_terminal_size()
      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')


if __name__ == '__main__':
    v2char = V2Char('vedio.mp4')
    v2char.play()

qiuyouzhi 发表于 2020-3-20 22:35:28

有pyprind这个模块吗?
是pyprint吧

鱼子酱POI 发表于 2020-3-20 22:42:05

qiuyouzhi 发表于 2020-3-20 22:35
有pyprind这个模块吗?
是pyprint吧

好的,我试试

鱼子酱POI 发表于 2020-3-20 22:44:34

qiuyouzhi 发表于 2020-3-20 22:35
有pyprind这个模块吗?
是pyprint吧

还是一样报错{:5_107:}

乍惊乍喜 发表于 2020-3-20 22:54:40

import pyprind
print(pyprind.path)

这个库没见过,和pprint类似么

永恒的蓝色梦想 发表于 2020-3-20 23:26:16

先去命令行pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com pyprind

鱼子酱POI 发表于 2020-3-20 23:43:14

乍惊乍喜 发表于 2020-3-20 22:54
这个库没见过,和pprint类似么

搬来的代码,我也不是很了解

鱼子酱POI 发表于 2020-3-20 23:44:13

永恒的蓝色梦想 发表于 2020-3-20 23:26
先去命令行

小白还是不太明白,用了大神你的方法,cmd最后报错还是一样

永恒的蓝色梦想 发表于 2020-3-20 23:47:25

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

你得先安装了这个模块啊……

乍惊乍喜 发表于 2020-3-21 00:18:16

鱼子酱POI 发表于 2020-3-20 23:43
搬来的代码,我也不是很了解

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

你先 pip install pyprind 这个包,如果提示找不到,很大可能就是没有这个包
如果install successing 那么就是你没有安装这个包
页: [1]
查看完整版本: 关于路径问题