def AttachCamera(): RDK = Robolink() item2station_pose = eye(4) view_pose_last = eye(4) last_item = None RDK.Render() run = RunApplication() while run.Run(): # Retrieve user selection selected_items = RDK.Selection() if len(selected_items) <= 0: last_item = None continue # Use the first selected item to attach the camera item = selected_items[0] # Prevent selecting programs or instructions or anything that doesn't move if item.type == ITEM_TYPE_ROBOT or item.type == ITEM_TYPE_TOOL or item.type == ITEM_TYPE_FRAME or item.type == ITEM_TYPE_OBJECT or item.type == ITEM_TYPE_TARGET: item_pose = item.PoseAbs( ) # Selected item pose with respect to the station reference item2station_pose = item_pose.inv() if last_item != item: # If it is the first time we select this item: update the relationship camera 2 item pose view_pose = RDK.ViewPose( ) # View Pose (camera pose with respect to the station reference) camera2item_pose = (item2station_pose * view_pose.inv()).inv() msg = 'Camera attached to %s' % item.Name() print(msg) RDK.ShowMessage(msg, False) last_item = item else: # calculate the new view pose and udpate it view_pose = camera2item_pose * item2station_pose # Only update if the view pose changed if view_pose != view_pose_last: view_pose_last = view_pose RDK.setViewPose(view_pose) RDK.Render()
def RecordProgram(): # Define the frames per second: FRAMES_PER_SECOND = 24 # Define the video extension (you may need to change the codec) #VIDEO_EXTENSION = ".avi" VIDEO_EXTENSION = ".mp4" # Choose the codec (mp4v, XVID or DIVX) #FOURCC = cv2.VideoWriter_fourcc(*'DIVX') # good quality (avi) #FOURCC = cv2.VideoWriter_fourcc(*'XVID') # low quality (avi) FOURCC = cv2.VideoWriter_fourcc(*'mp4v') # good quality (mp4) # Default screenshot style: SNAPSHOT_STYLE = "Snapshot" # Other snapshot styles are not recommended, otherwise it will create a delay and a lot of flickering # Instead, change the appearance of RoboDK in the menu Tools-Options-Display #SNAPSHOT_STYLE = "SnapshotWhite" #SNAPSHOT_STYLE = "SnapshotWhiteNoText" #SNAPSHOT_STYLE = "SnapshotNoTextNoFrames" PREFER_SOCKET = True # If available, prefer socket (usually faster) over temporary file to retrieve the image #------------------------------------------------------------------- # Use a temporary folder with tempfile.TemporaryDirectory(prefix='Record_') as td: RDK = Robolink() file_video_temp = os.path.join(td, 'RoboDK_Video' + VIDEO_EXTENSION) video = None frame = None use_socket = PREFER_SOCKET render_time_last = 0 time_per_frame = 1 / FRAMES_PER_SECOND app = RunApplication() while app.Run(): tic() # There's no need to get a new image if no render as occurred. render_time = int(RDK.Command("LastRender")) if (render_time_last != render_time): render_time_last = render_time def getSnapshot(use_socket, tempdir): fallback = False if use_socket: # Socket method. Added in version 5.3.2 try: bytes_img = RDK.Cam2D_Snapshot('', None) # None means station and will throw before version 5.3.2 if isinstance(bytes_img, bytes) and bytes_img != b'': return True, cv2.imdecode(np.frombuffer(bytes_img, np.uint8), cv2.IMREAD_UNCHANGED) except Exception: fallback = True pass # Fallback to tempfile method tf = tempdir + '/temp.png' if RDK.Command(SNAPSHOT_STYLE, tf) == 'OK': return not fallback, cv2.imread(tf) return False, None # Get the station snapshot success, frame = getSnapshot(use_socket, td) if not success: if use_socket: use_socket = False else: RDK.ShowMessage("Problems retrieving the RoboDK image buffer", False) break # Write the frame to the video file if video is None: # Requires at least one frame to extract the frame size. height, width = frame.shape[:2] video = cv2.VideoWriter(file_video_temp, FOURCC, FRAMES_PER_SECOND, (width, height)) video.write(frame) # Wait some time, if necessary, to have accurate frame rate elapsed = toc() if elapsed < time_per_frame: t_sleep = time_per_frame - elapsed print("Waiting for next frame: " + str(t_sleep)) pause(t_sleep) print("Done recording") # Check if nothing was saved if video is None: quit() video.release() # Save the file msg_str = "Saving video recording... " print(msg_str) RDK.ShowMessage(msg_str, False) # Create a suggested file name print("Saving video...") date_str = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") path_rdk = RDK.getParam('PATH_DESKTOP') # or path_rdk = RDK.getParam('PATH_OPENSTATION') file_name = "RoboDK-Video-" + date_str + VIDEO_EXTENSION # Ask the user to provide a file to save file_path = getSaveFileName(path_preference=path_rdk, strfile=file_name, defaultextension=VIDEO_EXTENSION, filetypes=[("Video file", "*" + VIDEO_EXTENSION)]) if not file_path: quit() # Save the file print("Saving video to: " + file_path) if os.path.exists(file_path): print("Deleting existing file: " + file_path) os.remove(file_path) # Move the file os.rename(file_video_temp, file_path) # Done msg_str = "Video recording saved: " + file_path print(msg_str) RDK.ShowMessage(msg_str, False)