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

Camera board • Skipped frames and distortion that looks like file corruption/encoder issues

$
0
0
Hello, I am trying to use my sunfounder camera sensor that came with a raspberry pi kit I got for Christmas. It, and my program I wrote works (as in runs without errors), but it produces distorted footage and skipped frames in the output footage. I am using a raspberry pi 4 with a small 4 in DSI screen and the sunfounder camera connected to it. The idea is a program that continuously records footage with a preserve footage feature. There is the normal "capture.h264" video file, which overwrites itself after each minute. That is, until the user presses the connected panic button, which copies the last 60 of recorded footage over to a separate video file "preserved-x.h264" before the capture.h264 gets overwritten again. Of course you should expect capture.h264 to have this issue, but preserved-x.h264 also has this issue despite being copped over from capture.h264 beforehand.

BTW, this was occurring even when I had each new capture.h264 saved as a seperate clip instead of overwriting each other, which is to save space on the later planned saving the footage on external media.

THINGS I TRIED ALREADY:
reseating camera cables on both pi and the module
Wrapping DSI and CSI cables in tinfoil - thought interference was happening :lol:
>>first 2 due to thinking there was interfearence from the sceeen cable

sudo apt update && sudo apt upgrade && sudo rpi-eeprom-update -a
changing from h264 encoder to fmpeg encoder
deleting the capture.h264 before re-running the python app.

VIDEO LINK:
https://www.dropbox.com/scl/fi/ix6fepnz ... t=c0xltdit

Program code:

Code:

from gpiozero import Button, Buzzerfrom picamera2 import Picamera2, Previewfrom picamera2.encoders import H264Encoder, MJPEGEncoder, Qualityimport timeimport subprocess# setup gpio deff and variablespanicBtn = Button(26)menuBtn = Button(5)speaker = Buzzer(6)panicCapture = False # used to control weather a capture should be saved# Define the functions to call upon press of the respective buttons.# Set panic capture to true when the panic button presseddef triggerPanic():    global panicCapture    panicCapture = True    print("MAIN.PY: PANIC NOW TRUE!")    for i in range(3): # make audible alert sound        speaker.on()        time.sleep(.1)        speaker.off()        time.sleep(.3)def openMenu():    subprocess.run(['python3', 'python/menu.py'])# event listeners for the buttonspanicBtn.when_pressed = triggerPanic # Event listener for the triggerPanic funmenuBtn.when_pressed = openMenu# initalize camera libary, and camera, and create a camera ufinder.picam = Picamera2() #start the libvideo_config = picam.create_preview_configuration() # make config for preview#picam.configure(video_config) # apply the configencoder = H264Encoder() # set the encoder to be usedpicam.start_preview(Preview.QTGL) # open the preview window# Get value of total captures from txt file saved by previous instance of main.pywith open("panicCounter.txt", "r") as file:    content = file.read()    print("MAIN.PY: read the contents of panicCounter as", content)    captureNo = int(content)    print("MAIN.PY: captureNo now", captureNo)while True:    captureNo += 1    panicCapture = False    print("MAIN.PY: started capture", captureNo)    picam.start_recording(encoder, 'captures/capture.h264', quality=Quality.HIGH) # start the camera    time.sleep(60)    picam.stop_recording()    print("MAIN.PY: STOPPED the previous capture")    # preserve the clip just captured if panicCapture is true    if panicCapture == True:        subprocess.run(["mv", "captures/capture.h264", f"captures/preserved{captureNo}.h264"])        print("MAIN.PY: Panic button pressed; clip", captureNo, "preserved")    # This captureNo var saved to a txt file so it dosn't    # reset  to 0 when new main.py launched, preventing    # file corruption    with open("panicCounter.txt", "w") as file:        file.write(str(captureNo))        print("MAIN.PY: captureNo written as", captureNo)
I'm wondering if I'm incorrectly configuring the encoder or if there's an issue with the OS or even the hardware. All help is appreciated.

Statistics: Posted by thisissigma — Fri Jan 02, 2026 4:25 pm



Viewing all articles
Browse latest Browse all 8051

Trending Articles