def main(): camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 10 #camera.resolution = (1920, 1080) #camera.framerate = 30 #camera.resolution = (2592, 1944) #camera.framerate = 15 #start warm up befor recording to get exposure right camera.start_preview() time.sleep(2) #setup circular io buffer stream = circular(camera, seconds=10) #start actual recording camera.start_recording(stream, format="h264") #camera.start_recording('my_video.mjpg') camera.start_recording('my_video.mp4', format="h264", splitter_port=2) camera.wait_recording(20) camera.stop_recording(splitter_port=2) camera.stop_recording() stream.copy_to("my_stream.mp4", seconds=10) camera.stop_preview()
def loop(loglevel=1): global motion_detected global fname camera = PiCamera() #Full view but 4 times lower resolution #camera.resolution = (1640,1232) camera.resolution = (640, 480) camera.framerate = 1 #start warm up befor recording to get exposure right camera.start_preview() time.sleep(2) #Use circular io buffor stream = circular(camera, seconds=20) camera.start_recording(stream, format='h264', motion_output=MyMotionDetector(camera)) start = dt.now() #Do some stuff while motion is not detected and wait while dt.now() - start < tidt(seconds=20.): fname = "{}".format(dt.strftime(dt.now(), "%Y%m%d_%H%M%S")) print("waiting") camera.wait_recording(1) #Stop all recording camera.stop_recording() stream.copy_to("fname.mp4".format(fname), seconds=10) camera.stop_preview()
def main(): f = h5py.File("demo.hdf5","w",driver="sec2",libver="latest") f.swmr_mode = True camera = PiCamera() camera.resolution = (1920,1080) #camera.resolution = (1296,730) #camera.resolution = (640,480) camera.framerate = 30 #start warm up befor recording to get exposure right camera.start_preview() time.sleep(2) #dummy = array.PiYUVArray(camera,size=(1296,730)) #camera.capture(dummy,use_video_port=True,format="yuv",resize=(1296,730)) dummy = array.PiYUVArray(camera) camera.capture(dummy,use_video_port=True,format="yuv") dset = f.create_dataset( "sequence", data=[dummy.array[:,:,0]], dtype="u1", compression="gzip", compression_opts=6, maxshape=( None, dummy.array.shape[0], dummy.array.shape[1]), chunks=True) dset.resize(0,axis=0) #setup circular io buffer stream = circular(camera, seconds=10) h5_writer = hdf5_storage(camera,dset,f) #start actual recording camera.start_recording(stream, format="h264") for i in range(10): camera.capture(h5_writer, format="yuv", use_video_port=True) camera.wait_recording(1) #stop all the recording camera.stop_recording() #save the ring buffer to file stream.copy_to("my_stream.mp4",seconds=10) camera.stop_preview() #close the HDF5 file print(dset.shape) f.close()
def loop(praefix="", loglevel=1, concat=False, buffer_time=5, motion_mask=np.ones((40, 30))): global motion_detected global keep_running #Full view but 4 times lower resolution camera = PiCamera() if camera.revision == "imx219": camera.resolution = (1640, 1232) else: camera.resolution = (1296, 972) camera.framerate = 30 #start warmup befor recording to get exposure right if loglevel == 0: print("Starting warmup") camera.start_preview() time.sleep(2) if loglevel == 0: print("Done with warmup") #Use circular io buffor if loglevel == 0: print("Create 10 seconds circular io buffer and start recording h264") stream = circular(camera, seconds=buffer_time) camera.start_recording(stream, format="h264") #Perform motion analysis from second splitter port with lowest resolution. #Reson is performance and enhanced noise removal if loglevel == 0: print("Set up motion detection on 640x480 resolution") camera.start_recording( '/dev/null', format='h264', splitter_port=2, resize=(640, 480), motion_output=MotionDetec(camera, size=(640, 480), num_no_motion_frames=camera.framerate * buffer_time, local_motion_mask=motion_mask)) #Do some stuff while motion is not detected and wait #start = dt.now() #while dt.now()-start < tidt(seconds=30.): while keep_running: if loglevel == 0: print("Waiting for motion") camera.wait_recording(1) if motion_detected: fname = "{}{}".format(praefix, dt.strftime(dt.now(), "%Y%m%d_%H%M%S")) if loglevel < 2: print("Motion at: {}".format(fname.split("/")[-1])) camera.split_recording("{}_during.mp4".format(fname), splitter_port=1) stream.copy_to("{}_before.mp4".format(fname), seconds=buffer_time) stream.clear() while motion_detected: camera.wait_recording(1) if loglevel == 0: print("Motion done, splitting back to circular io") camera.split_recording(stream, splitter_port=1) command = "ffmpeg -f concat -safe 0 -i {}_cat.txt -c copy {}.mp4 1> /dev/null 2> /dev/null && ".format( fname, fname) command += "rm -f {}_before.mp4 && ".format(fname) command += "rm -f {}_during.mp4 && ".format(fname) command += "rm -f {}_cat.txt &".format(fname) if loglevel == 0: print(command) with open("{}_cat.txt".format(fname), "w") as fi: fi.write("file '{}_before.mp4'\n".format(fname)) fi.write("file '{}_during.mp4'\n".format(fname)) fi.write("#{}".format(command)) #Only run this line if you have enough CPU grunt if concat: if loglevel == 0: print("Running ffmpeg command") os.system(command) #Stop all recording if loglevel == 0: print("Closing everything off") camera.stop_recording(splitter_port=2) camera.stop_recording() camera.stop_preview()
def loop(loglevel=1, concat=False): global motion_detected global stopped camera = PiCamera() #Full view but 4 times lower resolution camera.resolution = (1640, 1232) camera.framerate = 30 #start warm up befor recording to get exposure right camera.start_preview() time.sleep(2) #Use circular io buffor stream = circular(camera, seconds=10) camera.start_recording(stream, format="h264") #Perform motion analysis from second splitter port with lowest resolution. #Reson is performance and enhanced noise removal camera.start_recording('/dev/null', format='h264', splitter_port=2, resize=(640, 480), motion_output=MyMotionDetector(camera, size=(640, 480))) #Do some stuff while motion is not detected and wait #start = dt.now() #while dt.now()-start < tidt(seconds=30.): while keep_running: if loglevel == 0: print("Waiting") camera.wait_recording(1) if motion_detected: fname = "/videos/{}".format(dt.strftime(dt.now(), "%Y%m%d_%H%M%S")) if loglevel < 1: print("Motion at: {}".format(fname.split("/")[-1])) camera.split_recording("{}_during.mp4".format(fname), splitter_port=1) stream.copy_to("{}_before.mp4".format(fname)) stream.clear() while motion_detected: camera.wait_recording(1) camera.wait_recording(5) while motion_detected: camera.wait_recording(1) camera.split_recording(stream, splitter_port=1) command = "ffmpeg -f concat --framerate {} -safe 0 -i {}_cat.txt -c copy {}.mp4 1> /dev/null 2> /dev/null && ".format( int(camera.framerate), fname, fname) command += "rm -f {}_before.mp4 && ".format(fname) command += "rm -f {}_during.mp4 && ".format(fname) command += "rm -f {}_cat.txt &".format(fname) with open("{}_cat.txt".format(fname), "w") as fi: fi.write("file '{}_before.mp4'\n".format(fname)) fi.write("file '{}_during.mp4'\n".format(fname)) fi.write("#{}".format(command)) #Only run this line if you have enough CPU grunt if concat: os.system(command) #Stop all recording camera.stop_recording(splitter_port=2) camera.stop_recording() camera.stop_preview()
def loop(camera, fname_data, praefix="", loglevel=1, concat=False, buffer_time=120, motion_mask=np.ones((30, 40)), motion_mask_st=np.ones((30, 40))): global motion_detected global keep_running global flags global debug_file global mth_stan global mbl_stan global mth_roll global mbl_roll #setting up GPS buffer l_data_ti = 0. speed = np.ones(30) counter = 0 standing_mode = False duration = 0. wait_time = 0.5 max_duration = wait_time * 20 * 60 #Use circular io buffor if loglevel == 0: print("Create 10 seconds circular io buffer and start recording h264") stream = circular(camera, seconds=buffer_time) camera.start_recording(stream, format="h264") #Perform motion analysis from second splitter port with lowest resolution. #Reson is performance and enhanced noise removal if loglevel == 0: print("Set up motion detection on 640x480 resolution") mclass = MotionDetec(camera, size=(640, 480), threshold=mth_roll, num_blocks=mbl_roll, num_no_motion_frames=camera.framerate * 10, local_motion_mask=motion_mask) camera.start_recording('/dev/null', format='h264', splitter_port=2, resize=(640, 480), motion_output=mclass) #Do some stuff while motion is not detected and wait while keep_running: flags = read_status_file() if flags[1] and debug_file == None: debug_file = open("/tmp/debug_cam_motion.txt", "w") elif not (flags[1]) and debug_file != None: debug_file.close() if flags[2]: fname = "{}{}".format( praefix, dt.datetime.strftime(dt.datetime.now(), "%Y%m%d_%H%M%S")) camera.capture("{}.jpg".format(fname), use_video_port=True) flags[2] = not (flags[2]) write_status_file(flags) if loglevel == 0: print("Waiting for motion") print("thresh={}, num_blocks={}".format(mclass.threshold, mclass.num_blocks)) camera.wait_recording(0.2) if motion_detected or flags[0]: fname = "{}{}".format( praefix, dt.datetime.strftime(dt.datetime.now(), "%Y%m%d_%H%M%S")) if loglevel < 2: print("Motion at: {}".format(fname.split("/")[-1])) camera.split_recording("{}_during.mp4".format(fname), splitter_port=1) camera.capture("{}.jpg".format(fname), use_video_port=True) stream.copy_to("{}_before.mp4".format(fname), seconds=buffer_time) stream.clear() while (motion_detected or flags[0]) \ and keep_running \ and duration < max_duration: flags = read_status_file() #wait and increase video duration camera.wait_recording(wait_time) duration += wait_time #Handle GPS data once every second c_ti = time.time() if c_ti - l_data_ti >= 1.: if loglevel == 0: print("counter={}".format(counter)) #Speed in km/h with open("/tmp/gps.data", "r") as dafi: gps_point = dafi.readline() gps_point = gps_point.split("\t") if loglevel == 0: print(gps_point) try: speed = float(gps_point[-1]) spee = speed except: if loglevel == 0: print("could not Read Speed from GPS") if loglevel == 0: print("speed={}".format(spee)) if spee <= 5.0 and not (standing_mode): if loglevel == 0: print("Setting mode to standing mode") mclass.threshold = mth_stan mclass.num_blocks = mbl_stan mclass.set_mask(motion_mask_st) standing_mode = True elif spee > 5.0 and standing_mode: if loglevel == 0: print("Setting mode to rolling mode") mclass.threshold = mth_roll mclass.num_blocks = mbl_roll mclass.set_mask(motion_mask) standing_mode = False l_data_ti = c_ti counter += 1 counter %= 30 #Reset video duration for next run duration = 0. if loglevel == 0: print("Motion done, splitting back to circular io") camera.split_recording(stream, splitter_port=1) command = "ffmpeg -f concat -safe 0 -i {}_cat.txt -c copy {}.mp4 1> /dev/null 2> /dev/null && ".format( fname, fname) command += "rm -f {}_before.mp4 && ".format(fname) command += "rm -f {}_during.mp4 && ".format(fname) command += "rm -f {}_cat.txt &".format(fname) if loglevel == 0: print(command) with open("{}_cat.txt".format(fname), "w") as fi: fi.write("file '{}_before.mp4'\n".format(fname)) fi.write("file '{}_during.mp4'\n".format(fname)) fi.write("#{}".format(command)) #Only run this line if you have enough CPU grunt if concat: if loglevel == 0: print("Running ffmpeg command") os.system(command) #Handle GPS data once every second c_ti = time.time() if c_ti - l_data_ti >= 1.: if loglevel == 0: print("counter={}".format(counter)) #Speed in km/h with open("/tmp/gps.data", "r") as dafi: gps_point = dafi.readline() gps_point = gps_point.split("\t") if loglevel == 0: print(gps_point) try: speed = float(gps_point[-1]) spee = speed except: if loglevel == 0: print("could not Read Speed from GPS") if loglevel == 0: print("speed={}".format(spee)) if spee <= 5.0 and not (standing_mode): if loglevel == 0: print("Setting mode to standing mode") mclass.threshold = mth_stan mclass.num_blocks = mbl_stan mclass.set_mask(motion_mask_st) standing_mode = True elif spee > 5.0 and standing_mode: if loglevel == 0: print("Setting mode to rolling mode") mclass.threshold = mth_roll mclass.num_blocks = mbl_roll mclass.set_mask(motion_mask) standing_mode = False l_data_ti = c_ti counter += 1 counter %= 30 camera.stop_recording(splitter_port=2) camera.stop_recording()