#!/usr/local/bin/python3进口cv2 #如果没有安装在你的系统运行在终端“pip安装opencv-python”导入操作系统导入datetime进口tempfile进口shutil进口robodk #进口robodk库(机器人工具箱)robolink进口* # API与robodk RDK = robolink () RDK.Cam2D_Close() #关闭任何相机robodk tempfile.TemporaryDirectory () simulation_speed = RDK.SimulationSpeed() #视频配置(慢(480 p) = 640 x480@30fps -中等(720 p) = 1280 x720@60fps- High(1080p)=1920x1080@60FPS) width =1920 height = 1080 frames_per_second = 60 #in frames per second FPS #检索相机参考帧RDK. setselection ([]) camref = RDK. setselection ([])如果不是camref.Valid(): RDK,则ItemUserPick('为摄像机记录选择一个参考帧',ITEM_TYPE_FRAME)。ShowMessage('No valid Reference frame') quit(0) #Time to Record Question def time_question(title_msg, show_value, expected_values): if type(show_value) == Mat: show_value = show_value.tolist() answer = mbox(title_msg, entry=str(show_value)) if answer为False: print('Operation cancelled by user') RDK. cam2d_close () quit(0) try: answer = int(answer) except: print('Bad Time number') RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close ()ShowMessage("Not a valid time number") quit(0) else: time = answer return time time_record = time_question("Enter the time to record in seconds", 10,20) cam_id = RDK。Cam2D_Add (camref, ' FOCAL_LENGHT = 5视场大小= 30 FAR_LENGHT = 10000 = ' + str(宽度)+ x + str(高度))暂停(1)#辅助项目&操作date_str = datetime.datetime.now () .strftime (Y“% - % - % d % H - % - % S”)dir_path = tempfile.gettempdir () + ' \ Record_ + date_str out_path = RDK.getParam (PATH_DESKTOP) frame_rate = 1 / int (frames_per_second) n_photos = frames_per_second * int图像(time_record) counter = 0 =[] #创建工作目录如果不是os.path.exists (dir_path):os.makedirs(dir_path) #当counter < n_photos:counter = counter +1 RDK.setSimulationSpeed(0.001) file_name = dir_path + "/Image_" + str(counter) + ".png" images.append(file_name) RDK.Cam2D_Snapshot(file_name) print("Saving camera snapshot to the file:" + file_name) RDK.setSimulationSpeed(1) pause(frame_rate) #定义编解码器并创建VideoWriter对象fourcc = cv2.VideoWriter_fourcc(*'XVID') #选择编解码器(mp4v, XVID或DIVX) output = out_path + "/" + date_str + "。Avi " # ".mp4"为mp4v....".avi" for XVID, DIVX out = cv2。VideoWriter(output, fourcc, frames_per_second, (width, height)) #用图像和图像中的图像参数组合视频:image_path = os.path。join(dir_path, image) frame = cv2.imread(image_path) out.write(frame) # Write out frame to video cv2.imshow('video',frame) # Release everything if job is finished out.release() cv2.destroyAllWindows() msg_str = "The output video is in " + out_path + format(output) print(msg_str) RDK.Cam2D_Close() RDK.ShowMessage(msg_str) #Delete de trash (folder&files) shutil.rmtree(dir_path, ignore_errors=True)