Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlay Text with OpenCV in CustomEncoder #338

Closed
Altair68 opened this issue Oct 13, 2016 · 5 comments
Closed

Overlay Text with OpenCV in CustomEncoder #338

Altair68 opened this issue Oct 13, 2016 · 5 comments

Comments

@Altair68
Copy link

Altair68 commented Oct 13, 2016

Is it possible to overlay a given Text with a CustomEncoder?
This is a snippet of what I'm trying to do:

import picamera
import picamera.mmal as mmal
import sys
import numpy as np

class MyEncoder(picamera.PiCookedVideoEncoder):
    def start(self, output, motion_output=None):
        super(MyEncoder, self).start(output, motion_output)

    def _callback_write(self, buf):

        bufData = buf[0].data

        data = np.fromstring(bufData, dtype=np.uint8)
        image = cv2.imdecode(data, 1)

        #This is where I'd cv2.putText(...)

        super(MyEncoder, self)._callback_write(buf)


class MyCamera(picamera.PiCamera):
    def __init__(self):
        super(MyCamera, self).__init__();

    def _get_video_encoder(self, camera_port, output_port, format, resize, **options):
        return MyEncoder(self, camera_port, output_port, format, resize, **options)


with MyCamera() as camera:
    while True:
        camera.start_recording(sys.stdout, format='h264')
        camera.wait_recording(60 *60 * 24)
        camera.stop_recording()

With that code "camera.stop_recording()" raises "TypeError: 'MMALBuffer' object does not support indexing".

Or is there an easier way of doing what I'm trying to do here?

@Berryfier
Copy link

See my issue #337 on removing the indexing error.

@Altair68
Copy link
Author

So basically adding key=0 as a parameter should do the trick?

@Berryfier
Copy link

The interactive help identifies key=0, but this may be a default. For me the removal of [0] after buf was sufficient.

@Altair68
Copy link
Author

Ahhh now I see! 👍 Thanks a lot :)

@waveform80
Copy link
Owner

If you just want to overlay text, see the annotate_text property.

Unfortunately what you're trying to do (modify the image in the encoder class) isn't going to work: the encoder callback is at the very end of the pipeline after the image has been encoded and is about to be written to the output. What I think you're wanting to do is modify the image before it gets to the encoder; that's not (currently, easily) possible with picamera.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants