Camera Preview with overlay for tracking with a close button

The Raspberry Pi is a series of credit card-sized single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and developing countries.

Post Reply
User avatar
/RaspberryPi
Corporate
Posts: 2449
Joined: Wed Jun 05, 2019 1:29 am

Camera Preview with overlay for tracking with a close button

Post by /RaspberryPi »


I have a picamera script that starts the camera_preview and adds a cross hair overlay so that the spotlight operator can target the right person.

As the displays are touch screen I would like to add a button to the top right that will stop the preview and shut down the Pi.

I also need to have the script start on launch of the Pi.

This is what I have so far
from picamera import PiCamera from PIL import Image, ImageDraw from time import sleep import tkinter as tk import os camera = PiCamera() #camera.resolution = (1600,1020) #camera.preview_fullscreen = False #camera.preview_window = (115,-100,1600,1500) camera.rotation = 180 camera.zoom = (0.25, 0.25, 0.5, 0.5) camera.start_preview() # Load the arbitrarily sized image img = Image.open('crosshair.png') # Create an image padded to the required size with # mode 'RGB' pad = Image.new('RGBA', ( ((img.size[0] + 31) // 32) * 32, ((img.size[1] + 15) // 16) * 16, )) # Paste the original image into the padded one pad.paste(img, (0, 0)) # Add the picture overlay with the padded image as the source, # but the original image's dimensions o = camera.add_overlay(pad.tobytes(), size=img.size, layer=3) # By default, the overlay is in layer 0, beneath the # preview (which defaults to layer 2). Here we make # the new overlay semi-transparent, then move it above # the preview o.fullscreen = False o.window = (750, 350, 400, 400) o.alpha = 255 o.layer = 3 # Create a function to close the overlays and stop the camera preview def dome_shutdown(event=None): camera.remove_overlay(o) camera.stop_preview() root.destroy() # Shutdown the Raspberry Pi os.system("sudo shutdown -h now") # Create a function to close the overlays and stop the camera preview def close_overlays(event=None): camera.remove_overlay(o) camera.stop_preview() root.destroy() # Create a tkinter window and add a button to close the overlays root = tk.Tk() root.title("Camera Preview") shutdown_button = tk.Button(root, text="Shut\nDown", font=("Helvetica", 24), command=dome_shutdown) shutdown_button.pack(side="top") #close_button = tk.Button(root, text="Close", font=("Helvetica", 24), command=dome_shutdown) #close_button.pack(side="bottom") # Bind the escape key to the close_overlays function root.bind("", dome_shutdown) root.bind("", close_overlays) # submitted by /u/aussiexile
[link] [comments]

Source: https://www.reddit.com/r/raspberry_pi/c ... ng_with_a/
/RaspberryPi
Post Reply

Return to “Raspberry Pi Forum”