from asyncio import subprocess import sys import os import pyzed.sl as sl import threading import time from signal import signal, SIGINT #FIXME: 2022/02/04 completed #FIXME: NOTE: rec_count.txt contains number of flies in target_path include itself. zed_list = [] timestamp_list = [] thread_list = [] stop_signal = False frame_list = [] record_time_per_svo = 1800 name_time_list = [] root_path = os.path.abspath(os.path.expanduser('~/data/')) print(root_path) filenames = os.listdir(root_path) for f in filenames: w_time = os.path.getctime(root_path + '/'+f) name_time_list.append((f, w_time)) sorted_list = sorted(name_time_list, key=lambda x: x[1], reverse=True) print(sorted_list) recent_file = sorted_list[0] target_dir = str(root_path) + '/' + str(recent_file[0]) def handler(signal, frame): global stop_signal global zed_list global target_dir stop_signal = True #os.call(('wall', 'catch SIGINT')) #STOP RECORDING CAMERA for cam in zed_list: cam.disable_recording() cam.close() nowtime = time.localtime() rec_done = "%04d/%02d/%02d %02d:%02d:%02d" % (nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec) filepath = '%s/log.txt'%(target_dir) f = open(filepath, 'a') f.write('\n[RECORD] record succesfully stopped at ' + rec_done + '\n') f.close() with open ('%s/rec_count.txt'%(target_dir), "w") as rec_count: rec_count.write(str(len(os.listdir(target_dir)))) def grab_run(index, cameras): global stop_signal global zed_list global timestamp_list global left_list global depth_list global frame_list global taget_dir runtime = sl.RuntimeParameters() shard_count = 0 while not stop_signal: #I need loop and 1800ms if frame_list[index] % record_time_per_svo == 0: filepath = '%s/%s_%s.svo'%(target_dir,str(shard_count).zfill(3),cameras[index].id+1) record_params = sl.RecordingParameters(filepath, sl.SVO_COMPRESSION_MODE.H264) err = zed_list[index].enable_recording(record_params) #print('new shard created') shard_count += 1 if zed_list[index].grab(runtime) == sl.ERROR_CODE.SUCCESS : frame_list[index] += 1 #if zed_list[index].grab() == "SUCCESS": # zed_list[index].record() #print("Frame count: " + str(frame_list[index]), end='\r') if zed_list[index].grab(runtime) == sl.ERROR_CODE.SUCCESS : frame_list[index] += 1 zed_list[index].disable_recording() #print('saved') def main(): global target_dir #os.call(('wall', target_dir)) signal(SIGINT, handler) #print('Running...') init = sl.InitParameters() init.camera_resolution = sl.RESOLUTION.HD1080 init.camera_fps = 30 name_list = [] cameras = sl.Camera.get_device_list() print(cameras) index = 0 for cam in cameras: init.set_from_serial_number(cam.serial_number) name_list.append("ZED {}".format(cam.serial_number)) print("Opening {}".format(name_list[index])) zed_list.append(sl.Camera()) timestamp_list.append(0) frame_list.append(0) status = zed_list[index].open(init) if status != sl.ERROR_CODE.SUCCESS: print(repr(status)) zed_list[index].close() index = index +1 #Start camera threads for index in range(0, len(zed_list)): if zed_list[index].is_opened(): thread_list.append(threading.Thread(target=grab_run, args=(index,cameras))) thread_list[index].start() if __name__ == "__main__": main()