1

I'm using the Python bindings for OpenCV and have run into a little annoyance using CreateVideoWriter where when I call the function, it prints something similar to the below to the console and I can't seem to surpress it or ideally redirect it into a variable.

Output #0, avi, to 'temp/Temp.0433.avi':
    Stream #0.0: Video: mjpeg, yuvj420p, 320x240, q=2-31, 9830 kb/s, 90k tbn, 25
 tbc

The command I'm using for testing is this:

self.file = cvCreateVideoWriter(nf,CV_FOURCC('M','J','P','G'),self.fps,cvSize(320,240),1)

Although in the long run this app will have a control GUI its currently console based, the function is called every minute so this means its difficult to present even a simple menu or more useful status information without this call filling up the console.

Just wondering if anyone has experienced the same and/or has any ideas how I might be able to prevent this happening or can offer pointers as to what I'm doing wrong?

2
  • If all else fails, you could still hack around it by setting sys.stdout.write to def dummy(_): pass before calling it and restoring it later. Commented Nov 7, 2010 at 15:57
  • I just tried this by setting sys.stdout to a file object just before the call and then setting it back afterwards but its still going to the console which I don't truly understand! Commented Nov 7, 2010 at 16:26

1 Answer 1

1

I think the easiest way for you to do this is temporarily to redirect sys.stdout while calling the messy function -- anything else will force you to change the Python bindings.

Fortunately, this is easy: just use a contextmanager:

>>> import contextlib
>>> @contextlib.contextmanager
... def stdout_as(stream):
...     import sys
...     sys.stdout = stream
...     yield
...     sys.stdout = sys.__stdout__
...
>>> print("hi")
hi
>>> import io
>>> stream = io.StringIO()
>>> with stdout_as(stream):
...     print("hi")
...
>>> stream.seek(0)
0
>>> stream.read()
'hi\n'
>>> print("hi")
hi
Sign up to request clarification or add additional context in comments.

5 Comments

In answer to your question above, no its not writing to stderr either which is really confusing! I tried your suggestion of a context manager with stdout and stderr but its still displaying in the console.
@Duncan: hmm, that's odd. Unfortunately I can't immediately think of any reason why that could be. You might have to go and rootle around in the library's source code to see what it's doing. Sorry.
Thanks for your help so far all the same, Python is still very much a learning curve for me! Have started looking back through OpenCV but will have to carry on tomorrow.
@Duncan: it has just struck me that you are using the openCV call as part of an instance attribute of a class, which makes me think you may not be redirecting stdout in the write place. What happens if you wrap the entire program in a stdout redirector? (Or alternatively, do sys.stdout = StringIO().)
Sorry, havnt had a chance to work on this for a week, yes your correct it is being called within a class however it also does the same if I run a simple test outside of a class. I tried wrapping the whole program with no success which has really got me, however the good news is that with a wx gui it doesnt pop up in the background which is what I was afraid of. So its annoying but I can work around it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.