Swiggolas
Posts: 7
Joined: Sun Nov 04, 2018 8:01 pm

Text overlay over split screen live preview

Mon Jun 17, 2019 8:16 pm

Hello,
I would like to put an overlay with some text over the live preview but for some reason it won't really work when i try it with my code. I used a combination of this (https://raspberrypi.stackexchange.com/q ... ed-display) and this (https://hackaday.io/project/12450/logs) code. I came up with the following code, but i can't really see what i did wrong. If you know something please let me know.
Thanks!

Code: Select all

import time
import numpy as np
import string
import random
import os
from picamera import mmalobj as mo, mmal
from signal import pause
from PIL import Image, ImageDraw

camera = mo.MMALCamera()
splitter = mo.MMALSplitter()
render_l = mo.MMALRenderer()
render_r = mo.MMALRenderer()

camera.outputs[0].framesize = (320, 480)
camera.outputs[0].framerate = 60
camera.outputs[0].commit()

p = render_l.inputs[0].params[mmal.MMAL_PARAMETER_DISPLAYREGION]
p.set = mmal.MMAL_DISPLAY_SET_FULLSCREEN | mmal.MMAL_DISPLAY_SET_DEST_RECT
p.fullscreen = False
p.dest_rect = mmal.MMAL_RECT_T(0, 0, 320, 480)
render_l.inputs[0].params[mmal.MMAL_PARAMETER_DISPLAYREGION] = p
p.dest_rect = mmal.MMAL_RECT_T(320, 0, 320, 480)
render_r.inputs[0].params[mmal.MMAL_PARAMETER_DISPLAYREGION] = p

splitter.connect(camera.outputs[0])
render_l.connect(splitter.outputs[0])
render_r.connect(splitter.outputs[1])
splitter.enable()
render_l.enable()
render_r.enable()

topText = "Alt: 310m       Spd: 45km/h         Dir: N"
bottomText = "47.6062 N, 122.3321 W   Home: 322m    Rec: 3:22"

topOverlayImage = textOverlayCanvas.copy()
bottomOverlayImage = textOverlayCanvas.copy()

# Attach overlays 
topOverlay = camera.add_overlay(topOverlayImage.tobytes(), size=(320,60), layer=5, alpha=128, fullscreen=False, window=(0,0,320,60))
bottomOverlay = camera.add_overlay(bottomOverlayImage.tobytes(), size=(320,60), layer=4, alpha=128, fullscreen=False, window=(320,0,320,60))
  
try:
      while True:
         topOverlayImage = textOverlayCanvas.copy()
         bottomOverlayImage = textOverlayCanvas.copy()


         drawTopOverlay = ImageDraw.Draw(topOverlayImage)
         drawTopOverlay.text((200, 15), topText, font=font, fill=(0, 0, 0))

         topOverlay.update(topOverlayImage.tobytes())

         drawBottomOverlay = ImageDraw.Draw(bottomOverlayImage)
         drawBottomOverlay.text((150, 20), bottomText, font=font, fill=(0, 0, 0))

         bottomOverlay.update(bottomOverlayImage.tobytes())

         time.sleep(1)

except KeyboardInterrupt:
    camera.remove_overlay(topOverlay)
    camera.remove_overlay(bottomOverlay)

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6895
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Text overlay over split screen live preview

Tue Jun 18, 2019 7:53 am

What "doesn't really work"? Please describe the results you do get.
At a guess your layering is wrong, but it would take a fair amount to recreate. You can use "vcgencmd dispmanx_list" to see the list of elements that have been requested to be put on the screen.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

gordon77
Posts: 4003
Joined: Sun Aug 05, 2012 3:12 pm

Re: Text overlay over split screen live preview

Tue Jun 18, 2019 8:53 am

I tried the OPs script and firstly it says..

Code: Select all

Traceback (most recent call last):
  File "/home/pi/split1.py", line 37, in <module>
    topOverlayImage = textOverlayCanvas.copy()
NameError: name 'textOverlayCanvas' is not defined
so I added..

Code: Select all

# Create empty images to store text overlays
textOverlayCanvas = Image.new("RGB", (320, 60))
textOverlayPixels = textOverlayCanvas.load()
split image appears and then it says...

Code: Select all

Traceback (most recent call last):
  File "/home/pi/split1a.py", line 45, in <module>
    topOverlay = camera.add_overlay(topOverlayImage.tobytes(), size=(320,60), layer=5, alpha=128, fullscreen=False, window=(0,0,320,60))
AttributeError: 'MMALCamera' object has no attribute 'add_overlay'

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6895
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Text overlay over split screen live preview

Tue Jun 18, 2019 9:19 am

Thanks gordon77. I was expecting errors, hence being reluctant to spend the time trying to run it myself.

add_overlay is a function of the PiCamera class, not MMALCamera https://picamera.readthedocs.io/en/late ... dd_overlay The code for it is trivial though, so a quick copy/paste should cover it.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Return to “Camera board”