本文介绍了捕获打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 opencv 并且需要使用 VideoCapture 读取视频帧,并且有打印语句自动在控制台上打印错误和信息,我想捕获这些输出并保存到文件中 ..

I’m using opencv and there is a call for video frame reading with VideoCapture and there is print statement automatically printing errors and information on console , and I want to catch these outputs and save to a file ..

VideoCapture 不返回此语句,它只是直接打印

VideoCapture is not returning this statements it’s just directly printing

我该怎么做?

推荐答案

我不知道这是否是最好的方法,但它会起作用.

I dont know if its the best way to do this but it will work.

您可以通过键入以下内容来读取程序打印到控制台的所有内容:

You can read in everything your program prints into the console by typing this:

这里我们将 print("test-test-test-test") 打印到控制台,就像 opencv 一样,还有 p.stdout.readline() 你可以再读一遍.

Here we print print("test-test-test-test") into the console, like opencv does it, and with p.stdout.readline() you can read it in again.

import os
import sys
from subprocess import Popen, PIPE, STDOUT

script_path = os.path.join('name_of_your_program.py')

p = Popen([sys.executable, '-u', script_path],
          stdout=PIPE, stderr=STDOUT, bufsize=1)

while True:
    print("test-test-test-test")

    string = p.stdout.readline()
    print(string[0:3])

输出:

test-test-test-test
b'tes'
test-test-test-test
b"b'T"
test-test-test-test
b'tes'

(它以二进制形式读取,因此您必须将其转换为字符串.)

(It reads in binary so you have to convert it to a string.)

这篇关于捕获打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:49