I experimented with this....
The script will capture RAW 640x480 frames into /run/shm (RAM) at 100fps, on a Pi4 with a v2 camera. Note 640x480 crops the image.
It will run from the start capturing pre-frames and when triggered by a button, between GPIO21 (pin40) and gnd, it will delete old frames prior to the pre-frames (default pre-frames 100) and then it will capture for the defined period (default 10 seconds ), giving approx 1100 raw files.
It will then rename the frames with the triggered time plus a number eg 260106120507_00001.raw.
It will then debayer and show the triggered frame and then stop.
Note it will be filling the RAM before the button is pressed so don't wait too long !! It should stop if it runs out of RAM.
You can't trigger it before the number of pre-frames have been captured.
The script will capture RAW 640x480 frames into /run/shm (RAM) at 100fps, on a Pi4 with a v2 camera. Note 640x480 crops the image.
It will run from the start capturing pre-frames and when triggered by a button, between GPIO21 (pin40) and gnd, it will delete old frames prior to the pre-frames (default pre-frames 100) and then it will capture for the defined period (default 10 seconds ), giving approx 1100 raw files.
It will then rename the frames with the triggered time plus a number eg 260106120507_00001.raw.
It will then debayer and show the triggered frame and then stop.
Note it will be filling the RAM before the button is pressed so don't wait too long !! It should stop if it runs out of RAM.
You can't trigger it before the number of pre-frames have been captured.
Code:
#!/usr/bin/env python3import timeimport os, subprocessimport signalimport datetimeimport globimport sysfrom gpiozero import Buttonimport cv2import numpy as np# setupframerate = 100 # fpspre_frames = 100 # Number of PRE Framesv_length = 10000 # in mSram_limit = 150 # in MB, stops if RAM below this# specify trigger buttontrigger = Button(21)# clear rampics = glob.glob('/run/shm/*.raw')for tt in range(0,len(pics)): os.remove(pics[tt])pics = glob.glob('/run/shm/*.jpg')for tt in range(0,len(pics)): os.remove(pics[tt]) # start camera with subprocesscommand = "rpicam-raw -n -t 0 --segment 1 --framerate " + str(framerate) + " -o /run/shm/temp_%d.raw"s = subprocess.Popen(command, shell=True, preexec_fn=os.setsid)poll = s.poll()while poll != None: print("waiting...") poll = s.poll()run = 0pre = 0# check ramst = os.statvfs("/run/shm/")freeram = (st.f_bavail * st.f_frsize)/1100000print("Starting...")# main loopwhile run == 0 and freeram > ram_limit: # wait for enough images pics = glob.glob('/run/shm/temp*.raw') while len(pics) < pre_frames: pics = glob.glob('/run/shm/temp*.raw') if pre == 0: print("Pre-Frames full...") print("Trigger when ready...") pre = 1 #pics.sort(reverse=True) w = len(pics) st = os.statvfs("/run/shm/") freeram = (st.f_bavail * st.f_frsize)/1100000 # trigger from button if trigger.is_pressed: print("Triggered...") now = datetime.datetime.now() timestamp = now.strftime("%y%m%d%H%M%S") for tt in range(pre_frames,w): os.remove(pics[tt]) del pics[pre_frames:w] start = time.monotonic() st = os.statvfs("/run/shm/") freeram = (st.f_bavail * st.f_frsize)/1100000 while time.monotonic() - start < v_length/1000 and freeram > ram_limit: pass # rename pre-frames pics.sort() fx = 1 for x in range(0,pre_frames): fxa = "00000" + str(fx) if os.path.exists(pics[x]): os.rename(pics[x],pics[x][0:9] + timestamp + "_" + str(fxa[-5:]) + '.raw') fx +=1 # rename new frames pics = glob.glob('/run/shm/temp*.raw') pics.sort() for x in range(0,len(pics)): fxa = "00000" + str(fx) if os.path.exists(pics[x]): os.rename(pics[x],pics[x][0:9] + timestamp + "_" + str(fxa[-5:]) + '.raw') fx +=1 os.killpg(s.pid, signal.SIGTERM) # show trigger frame fxa = "00000" + str(pre_frames) fd = open('/run/shm/' + timestamp + '_' + str(fxa[-5:]) + '.raw', 'rb') rows = 480 cols = 640 f = np.fromfile (fd,dtype=np.uint8,count=-1) f = f.reshape(int(f.size/5),5) f = np.delete(f, 4, 1) im = f.reshape((rows, cols)) #notice row, column format fd.close() cv2.imshow('output',im) # wait for a key press cv2.waitKey() # clear ram of temp files pics = glob.glob('/run/shm/temp*.raw') for tt in range(0,len(pics)): os.remove(pics[tt]) # stop run = 1
Statistics: Posted by gordon77 — Wed Feb 07, 2024 12:32 pm