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

Camera board • Re: Newbie alert! Help with Rpi5 and camera distance recording

$
0
0
This is a bit rough (as I keep saying, I'm not a Python programmer), but I've used it on a Pi 2B to record a set of fixed length video files with a time/date/ID caption on the video.

Feel free to do what you like with it, it is based on some of the examples.

There may be better ways to do it.

Code:

#!/usr/bin/python3import cv2from picamera2 import MappedArray, Picamera2from picamera2.encoders import H264Encoder, Qualityfrom picamera2.outputs import FileOutput, FfmpegOutputimport timeimport sys# Parameterscount = 4   # Number of videos to recordlength = 60 # Video length in minutesdestdir = "/home/rpdom/"size="640x480"  # Available 640x480, 1640x1232, 1920x1080, 3280x2464width = 640height = 480vidnum = 1picam2 = Picamera2()picam2.configure(picam2.create_video_configuration(main={"size": (width, height)}))# Caption datacolour = (0, 255, 0) # Font colourorigin_time = (width - 145, 70)origin_date = (width - 208, 30)origin_name = (0, 30)font = cv2.FONT_HERSHEY_SIMPLEXscale = 1thickness = 2start_timestamp = Nonedef apply_timestamp(request):    global start_timestamp, vidnum    with MappedArray(request, "main") as m:        cv2.putText(m.array, "Camera 1", origin_name, font, scale, colour, thickness) # Camera name caption        cv2.putText(m.array, time.strftime("%d/%m/%Y"), origin_date, font, scale, colour, thickness) # Date caption        cv2.putText(m.array, time.strftime("%H:%M:%S"), origin_time, font, scale, colour, thickness) # Time captionpicam2.pre_callback = apply_timestampencoder = H264Encoder()picam2.start()try:    while ( vidnum <= count ):        filename = time.strftime("%Y%m%d-%H%M.mp4") # Filename will be YYYYMMDD-HHMM.mp4        print(f"File: {filename} Part: {vidnum}", flush=True)        output = FfmpegOutput(destdir + '/' + filename)        picam2.start_recording(encoder, output)        time.sleep(length * 60)        picam2.stop_recording()        vidnum += 1except:    print("Something went wrong?")picam2.stop()print("Finished")

Statistics: Posted by rpdom — Sat Dec 28, 2024 4:59 pm



Viewing all articles
Browse latest Browse all 4802

Trending Articles