|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import cv2
- from multiprocessing import Pool
- ERASE_LINE = '\x1b[2K'
- CURSOR_UP_ONE = '\x1b[1A'
- def process_frame(frame):
- resized_frame = cv2.resize(frame, (160, 48))
- lines = []
- for row in resized_frame:
- line = ''
- for pixel in row:
- intensity = sum(pixel[:3]) / 3
- if intensity >= 128: # 判断颜色是否较亮
- line += '\033[48;2;{};{};{}m \033[m'.format(*pixel[:3])
- else:
- line += '\033[38;2;{};{};{}m█\033[m'.format(*pixel[::-1][:3])
- lines.append(line)
- return lines
- if __name__ == '__main__':
- video = cv2.VideoCapture('1.mp4')
- total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
- pool = Pool()
- i = 0
- while True:
- ret, frame = video.read()
- if not ret:
- break
- result = pool.apply_async(process_frame, (frame,))
- lines = result.get()
- for line in lines:
- print(ERASE_LINE + line)
- print(CURSOR_UP_ONE * len(lines), end='')
- i += 1
- video.release()
复制代码
这段代码,可以打印彩色输出,但不知道怎么的,颜色不太对(好像有些地方会反色,有些地方不会)
这段代码中,在使用颜色时有些地方使用了 RGB 颜色表示法,而有些地方使用了 BGR 颜色表示法,导致了颜色有些地方反色了。
具体来说,可以将 process_frame 函数中的以下代码:
- if intensity >= 128:
- line += '\033[48;2;{};{};{}m \033[m'.format(*pixel[:3])
- else:
- line += '\033[38;2;{};{};{}m█\033[m'.format(*pixel[::-1][:3])
复制代码
修改为:
- if intensity >= 128:
- line += '\033[48;2;{};{};{}m \033[m'.format(*pixel[:3][::-1])
- else:
- line += '\033[38;2;{};{};{}m█\033[m'.format(*pixel[:3])
复制代码
这样就能够保证颜色的一致性了。
|
|