#!/usr/local/bin/python3进口cv2 #如果没有安装在你的系统运行在终端“pip安装opencv-python”导入操作系统导入datetime进口tempfile进口_thread从robolink进口进口shutil * # API与RoboDK RDK = robolink () RDK.Cam2D_Close() #关闭任何相机RoboDK tempfile.TemporaryDirectory() #视频配置(稳定的640 x480 @ 12 FPS)高度宽度= 640 = 480 frames_per_second = 12 #在帧每秒帧#检索相机参考系RDK. setselection ([]) camref = RDK. setselect ([])如果不是camref.Valid(): RDK,则ItemUserPick('为摄像机记录选择一个参考帧',ITEM_TYPE_FRAME)。ShowMessage('No valid Reference frame') quit(0) cam_id = RDK。Cam2D_Add(camref, ' focal_lengt =6 FOV=32 far_lengt =10000 SIZE=' + str(width) + "x" + str(height)) #Time to Record Question def time_question(title_msg, show_value, expected_values): #import re if type(show_value) == Mat: show_value = show_value.tolist() answer = mbox(title_msg, entry=str(show_value)) if answer是False: print('操作被用户取消')RDK. cam2d_close () quit(0) try: answer = int(answer) except: print('错误的时间数字')RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close () RDK. cam2d_close ()ShowMessage("Not a valid time number") quit(0) else:时间=答案返回time_record = time_question(“输入秒中的时间记录”,10,20)#辅助项目&操作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) #生成图像线程def take_shot(): file_name = dir_path + "/Image_" + str(counter) + ".png" RDK.Cam2D_Snapshot(file_name) print("Saving camera snapshot to the file:" + file_name) Images .append(file_name) #图像捕获循环while counter < n_photos: _thread. png"start_new_thread(take_shot, ()) counter = counter +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)) # Compose the video with the images and the parameters for image in images: 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)