You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shepherd-agent/shepherd/camtest.py

80 lines
2.2 KiB

#!/usr/bin/env python3
import cv2
from PIL import Image, ImageDraw, ImageFont
print(cv2.__version__)
gst_str = ('v4l2src device=/dev/video0 ! '
'videoconvert ! appsink drop=true max-buffers=1 sync=false')
print(gst_str)
logo_im = Image.open('smallshepherd.png')
overlayfont = "DejaVuSansMono.ttf"
try:
fnt = ImageFont.truetype(overlayfont, 50)
except IOError:
fnt = ImageFont.load_default()
loaded_fonts={}
loaded_logos={}
vidcap = cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
while True:
breakpoint()
vidcap.grab()
read_flag, frame = vidcap.read()
print(read_flag)
#overlay = frame.copy()
# You may need to convert the color.
#Convert over to PIL. Mostly so we can use our own font.
img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img)
font_size = int(im_pil.height/40)
margin_size = int(font_size/5)
if font_size not in loaded_fonts:
loaded_fonts[font_size] = ImageFont.truetype(overlayfont, int(font_size*0.9))
thisfont = loaded_fonts[font_size]
if font_size not in loaded_logos:
newsize = (int(logo_im.width*(font_size/logo_im.height)),font_size)
loaded_logos[font_size] = logo_im.resize(newsize, Image.BILINEAR)
thislogo = loaded_logos[font_size]
overlay = Image.new('RGBA',(im_pil.width,font_size+(2*margin_size)), (0,0,0))
overlay.paste(thislogo, (int((overlay.width-thislogo.width)/2),margin_size))
draw = ImageDraw.Draw(overlay)
draw.text((margin_size*2, margin_size), "SARDIcam-1", font=thisfont,
fill=(255, 255, 255, 255))
datetext = "2019-07-24 20:22:31"
datewidth, _ = draw.textsize(datetext,thisfont)
draw.text((overlay.width-(margin_size*2)-datewidth, margin_size), datetext, font=thisfont,
fill=(255, 255, 255, 255))
overlay.putalpha(128)
im_pil.paste(overlay, (0,im_pil.height-overlay.height),overlay)
im_pil.save("test.jpg", "JPEG", quality=90)
# For reversing the operation:
#im_np = np.asarray(im_pil)
#cv2.rectangle(overlay,(200,200),(500,500),(255,0,0),-1)
#cv2.addWeighted(overlay, 0.3, frame, 0.7, 0, frame)
#cv2.imwrite("frame.jpg", frame)
# print out build properties:
# print(cv2.getBuildInformation())