Close

Software: Setting Up The Preview Screen

A project log for Raspberry Camcorder and Video Editing Station

Creating an all in one portable video production set up.

dustinDustin 11/29/2020 at 18:370 Comments

I started with my best guess at the easiest way to implement this, and it failed nicely. The goal is to get a preview up as soon as the program starts, and try to keep it running the entire time. I added all the stopping and starting as I know the different camera modes have different resolutions. Actually, this isn't the easiest way to do it, but it was the first thing that occured to me. The following code is really wonky and does not work.

import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'

while True: 
    camera.start_preview()
    try:   
        if keyboard.is_pressed('r'):
            sleep(2)
            camera.stop_preview()
            camera.resolution = (1920, 1080)
            camera.start_preview()
            camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
        
        if keyboard.is_pressed('s'):    
            camera.stop_recording(splitter_port=2)
            #camera.stop_preview()
            
        
        if keyboard.is_pressed('p'):
            sleep(2)
            camera.stop_preview()
            camera.resolution = (2592, 1944)
            camera.start_preview()
            camera.capture('/home/pi/Documents/test_files/testpic.png')
            #camera.stop_preview()
            
        if keyboard.is_pressed('x'):
            break
        sleep(.01)
    except:
        pass

That failed in a very interestng fashion. The preview did indeed come up, and stay up, but it would flash every few seconds, and I couldn't interrupt the program to stop it. I ended up having to spam the "x" button and reboot the Pi. It at least did capture a video and an image without interfering with the preview. I now need to stop the screen from flashing, and find a way to interrupt this program.

Let's try this out. I took out the sleep(2) and the camera.start_preview() from the while loop. I added the camera.start_preview() to the top, after the camera is initialized. The 2 second sleep I had in there seems to be about the same amount of time as the flashing. After pasting that in, I realized that I NEVER stop the preview, and that's probably why it's still up on the screen. Better add that into the end when "x" is pressed.

import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.start_preview() 

while True: 

    try:
        
        if keyboard.is_pressed('r'):
            sleep(2)
            camera.resolution = (1920, 1080)
            camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
        
        if keyboard.is_pressed('s'):    
            camera.stop_recording(splitter_port=2)            
        
        if keyboard.is_pressed('p'):
            sleep(2)
            camera.resolution = (2592, 1944)
            camera.capture('/home/pi/Documents/test_files/testpic.png')
            
        if keyboard.is_pressed('x'):
            camera.stop_preview()
            break
        sleep(.01)
    except:
        pass

 That worked out far better this time. The preview window came up, and never went away. It did change dimensions every time it changes functions, but it's far better and more usable. There was a live preview image on screen at all times. The image and video captures were also good. 

import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.start_preview() 

while True: 
    try:        
        if keyboard.is_pressed('r'):
            camera.start_preview()
            sleep(2)
            camera.resolution = (1920, 1080)
            camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
        
        if keyboard.is_pressed('s'):    
            camera.stop_recording(splitter_port=2)            
        
        if keyboard.is_pressed('p'):
            camera.start_preview()
            sleep(2)
            camera.resolution = (2592, 1944)
            camera.capture('/home/pi/Documents/test_files/testpic.png')
            
        if keyboard.is_pressed('x'):
            camera.stop_preview()
            break
        sleep(.01)
    except:
        pass

When hitting "r" to start recording, the preview goes away for a moment, and comes back. It then does this again. I suspect this is due to the following:

if keyboard.is_pressed('r'):
camera.start_preview()

 Hitting "s" to stop recodring does not interfere with the preview window. I need to add some way of knowing if it's recording or not. Perhaps an overlay or something.

Trying the following to stop the interruption from taking a picture, as well as the interruption from starting a recording:

import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.start_preview() 

while True: 
    try:        
        if keyboard.is_pressed('r'):
            sleep(2)
            camera.resolution = (1920, 1080)
            camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
        
        if keyboard.is_pressed('s'):    
            camera.stop_recording(splitter_port=2)            
        
        if keyboard.is_pressed('p'):
            sleep(2)
            camera.resolution = (2592, 1944)
            camera.capture('/home/pi/Documents/test_files/testpic.png')
            camera.resolution = (1920, 1080)
            camera.start_preview()
            
        if keyboard.is_pressed('x'):
            camera.stop_preview()
            break
        sleep(.01)
    except:
        pass

The changes to the picture section actually helped quite a bit. Usually, the preview would resize, it would take a picture, then leave the preview window at the new dimensions. I set it to change the resolution back to 1080p, as this camera will primarily be used to record 1080p video, with the occasional image capture. The preview still flickers when starting a video recording, but I am nearly done with this particular task. The following code might help a bit. I changed the order of a few things. Explaining every single change is tedious, and I'm not in a professional environment, so I won't be doing that all the time.

import keyboard
from time import sleep
from picamera import PiCamera

camera = PiCamera()
camera.resolution = (1920, 1080)
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.start_preview() 

while True: 
    try:        
        if keyboard.is_pressed('r'):
            camera.resolution = (1920, 1080)
            sleep(2)
            camera.start_recording('/home/pi/Documents/test_files/button.h264', splitter_port=2)
        
        if keyboard.is_pressed('s'):    
            camera.stop_recording(splitter_port=2)            
        
        if keyboard.is_pressed('p'):
            sleep(2)
            camera.resolution = (2592, 1944)
            camera.capture('/home/pi/Documents/test_files/testpic.png')
            camera.resolution = (1920, 1080)
            camera.start_preview()
            
        if keyboard.is_pressed('x'):
            camera.stop_preview()
            break
        sleep(.01)
    except:
        pass

I've gotten this running better than ever, and the preview portion seems to have hit a good stopping point. The screen still flashes when I start recording, but that's the worst of it. I'd still like to shorten the preview that pops up when a picture is taken, but that can wait until later. Doing some research, ate a turkey leg, and getting ready to take care of a few other things around the house.

I ended up making more changes to the code, but forgot what I was doing and reverted them to a known working version to be safe.

Discussions