|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这段代码,每打印一帧就会清除,而我用的又是多进程,非常晃,时间长了眼睛容易瞎
就是下面这样(真实情况比gif更晃):
https://www.123pan.com/s/Hhs8Vv-RyCRh.html
- import cv2
- import os
- from multiprocessing import Pool
- def process_frame(frame):
- gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- resized_frame = cv2.resize(gray_frame, (160, 48))
- lines = []
- for row in resized_frame:
- line = ''.join(['█' if pixel >= 128 else ' ' for pixel in row])
- 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,)) # 异步调用process_frame函数
- lines = result.get() # 获取处理结果
- for line in lines:
- print(line)
- os.system("cls")
- i += 1
- print("已经打印的帧数:", i)
- print("总帧数:", total_frames)
- video.release()
复制代码
这段代码每打印一帧就会清除,可以通过在控制台上打印 ANSI 转义序列来实现在同一位置上更新帧。以下是修改后的代码:
- import cv2
- import os
- from multiprocessing import Pool
- # ANSI 转义序列,用于在同一位置上更新帧
- ERASE_LINE = '\x1b[2K'
- CURSOR_UP_ONE = '\x1b[1A'
- def process_frame(frame):
- gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- resized_frame = cv2.resize(gray_frame, (160, 48))
- lines = []
- for row in resized_frame:
- line = ''.join(['█' if pixel >= 128 else ' ' for pixel in row])
- 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,)) # 异步调用process_frame函数
- lines = result.get() # 获取处理结果
- # 在同一位置上更新帧
- for line in lines:
- print(ERASE_LINE + line)
- print(CURSOR_UP_ONE * len(lines), end='')
- i += 1
- print("已经打印的帧数:", i)
- print("总帧数:", total_frames)
- video.release()
复制代码
在这个修改后的代码中,我们使用 ANSI 转义序列来实现在同一位置上更新帧。 ERASE_LINE 变量用于清除当前行, CURSOR_UP_ONE 变量用于将光标移动到上一行,以便在同一位置上更新帧。 print(CURSOR_UP_ONE * len(lines), end='') 用于将光标移动到帧的第一行,以便下一帧能够在正确的位置上打印。
|
|