Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4774

Camera board • Camera stream/preview - best practice

$
0
0
Hey

I'm installing my camera on a small car that I control over an ssh connection to my PC, and I just put together some code from different places that luckily worked. This is in the function start_camera(). I just want to have a stream to my PC when I'm running my script, but I wonder if there are better ways to do it then how I currently have it. If you have any better ways to do it, then please give some feedback.

The low resolution is because the feed got too bad on normal resolution. This camera will often be in motion.
The reason for the choise of Preview.QT is because I read somewhere that this is the only one that works over an ssh connection.

Code:

from CarHandling import CarHandlingfrom SerialCommunicator import SerialCommunicatorimport RPi.GPIO as GPIOfrom threading import Thread, Eventfrom time import sleepfrom picamera2 import Picamera2, PreviewGPIO.setmode(GPIO.BOARD)# define GPIO pinsleftBackward = 16leftForward = 15rightBackward = 22rightForward = 18enA = 11enB = 13def handle_car(event):    handler = CarHandling(leftBackward, leftForward, rightBackward, rightForward, enA, enB)    handler.handle_xbox_input(event)def get_serial_data(event):    serialObj = SerialCommunicator('/dev/ttyACM0', 9600)  # serial connection to USB port    serialObj.open_communication(event)def start_camera(event):    # initialize object    picam2 = Picamera2()    # lowering resolution to 60% to increase framerate    camera_config = picam2.create_preview_configuration(main={"size": (384, 288)})    picam2.configure(camera_config)    picam2.start_preview(Preview.QT)  # must use this preview to run over ssh    picam2.start()  # start camera    while not event.is_set():        sleep(0.5)    picam2.close()myEvent = Event()thread1 = Thread(target=handle_car, args=(myEvent,))thread1.start()thread2 = Thread(target=get_serial_data, args=(myEvent,))thread2.start()thread3 = Thread(target=start_camera, args=(myEvent,))thread3.start()try:    while True:        sleep(0.5)except KeyboardInterrupt:    myEvent.set()    thread1.join()    thread2.join()    thread3.join()

Statistics: Posted by Siggerud — Wed Jun 19, 2024 6:25 am



Viewing all articles
Browse latest Browse all 4774

Trending Articles