def work_function(data, parameters): global vis global global_var_1 global global_var_2 global global_var_3 global global_var_4 ''' # This is a 2nd way to initialise the parameters if no initialisation function is used. if need_parameters: try: global_var_1 = parameters[1] global_var_2 = parameters[2] global_var_3 = parameters[3] global_var_1 = parameters[4] need_parameters = True except: pass else: # Do the rest of the code in here so that it doesn't run if the parameters are not set ''' # If any parameters need to be updated during runtime then do that here, e.g. # Also update the visualisation parameter. This allows to turn on and off the visualisation window during # run time try: global_var_1 = parameters[1] vis.visualisation_on = parameters[0] except: pass # In the case of multiple inputs the topic will tell you which input the message has come from. The topic is a # string that is formatted as follows: # previous_node_output_name##previous_node_name##previous_node_index -> this_node_input_name##this_none_name##this_node_index # so you can see which input the data is coming from by looking at the this_node_input_name part. Also although the # names of the inputs and outputs can have spaces, these become underscores in the names of the topics. topic = data[0] print( topic ) # prints will not work if the operation is running on a different computer. message = data[1:] # data[0] is the topic image = Socket.reconstruct_array_from_bytes_message_cv2correction(message) # Now do stuff # Whatever data the Node must visualise should be put in the vis.visualised_data variable vis.visualised_data = image # For Operations with multiple outputs the work_function must return a list of numpy arrays with length the number # of outputs. Each array from left to right in the list gets passed to each output from top to bottom on the Node. # So in this example the data would go out of the 'Something Out 1' output and the np.array([ct.IGNORE]) would go # out of the 'Something Out 2' output. If you want to stop one or more outputs from sending out any data on the # current pass then put as an array the np.array([ct.IGNORE]) array. The com process knows to ignore this array. result = [vis.visualised_data, np.array([ct.IGNORE])] return result
def differencing(data, parameters): global vis global worker_object topic = data[0].decode('ascii') message = data[1:] try: vis.visualisation_on = parameters[0] frame2_minus_frame1 = parameters[1] except: vis.visualisation_on = differencing_com.ParametersDefaultValues[0] frame2_minus_frame1 = differencing_com.ParametersDefaultValues[1] image = Socket.reconstruct_array_from_bytes_message_cv2correction(message) for received_topic in worker_object.recv_topics_buffer: if topic in received_topic: worker_object.recv_topics_buffer[received_topic] = image all_topics = list(worker_object.recv_topics_buffer.keys()) if np.shape(worker_object.recv_topics_buffer[ all_topics[0]])[0] > 0 and np.shape( worker_object.recv_topics_buffer[all_topics[1]])[0] > 0: if worker_object.recv_topics_buffer[ all_topics[0]].shape == worker_object.recv_topics_buffer[ all_topics[1]].shape: worker_object.worker_result = worker_object.recv_topics_buffer[all_topics[0]] -\ worker_object.recv_topics_buffer[all_topics[1]] elif len(worker_object.recv_topics_buffer) == 2: if worker_object.verbose: print( 'Shapes of two arrays are not the same. Input 1 shape = {}, input 2 shape = {}. Passing through 1st input' .format( np.shape( worker_object.recv_topics_buffer[all_topics[0]]), np.shape( worker_object.recv_topics_buffer[all_topics[1]]))) worker_object.worker_result = worker_object.recv_topics_buffer[ all_topics[0]] if frame2_minus_frame1: worker_object.worker_result = worker_object.recv_topics_buffer[all_topics[1]] -\ worker_object.recv_topics_buffer[all_topics[0]] else: worker_object.worker_result = np.random.random((100, 100)) print('Differencing {} failed. The frame buffer is empty.'.format( worker_object.node_index)) vis.visualised_data = worker_object.worker_result return [worker_object.worker_result]
def save_video(data, parameters): global worker_object global need_parameters global time_stamp global write_proc global file_name global pixel_format_out global pixel_format_in global fps start = time.perf_counter() # Once the parameters are received at the starting of the graph then they cannot be updated any more. if need_parameters: try: file_name = parameters[0] time_stamp = parameters[1] pixel_format_in = parameters[2] pixel_format_out = parameters[3] fps = parameters[4] if time_stamp: add_timestamp_to_filename() need_parameters = False worker_object.num_of_iters_to_update_relics_substate = -1 worker_object.relic_create_parameters_df( file_name=file_name, time_stamp=time_stamp, pixel_format_in=pixel_format_in, pixel_format_out=pixel_format_out, fps=fps) except: return else: message = data[1:] # data[0] is the topic image = Socket.reconstruct_array_from_bytes_message_cv2correction( message) size = image.shape try: write_proc.stdin.write(image.astype(np.uint8).tobytes()) except Exception as e: write_proc = ffmpeg_write_process(file_name, fps, pixel_format_in, pixel_format_out, size[1], size[0]) worker_object.relic_update_substate_df(image_size=size) end = time.perf_counter()
def canny(data, parameters): global vis try: vis.visualisation_on = parameters[0] min_val = parameters[1] max_val = parameters[2] except: vis.visualisation_on = canny_com.ParametersDefaultValues[0] min_val = canny_com.ParametersDefaultValues[1] max_val = canny_com.ParametersDefaultValues[2] message = data[1:] # data[0] is the topic image = Socket.reconstruct_array_from_bytes_message_cv2correction(message) try: vis.visualised_data = cv2.Canny(image, min_val, max_val) except: vis.visualised_data = np.array((10, 10)) print('Canny operation failed') return [vis.visualised_data]
def cvt_color(data, parameters): global vis try: vis.visualisation_on = parameters[0] color_conv_string = parameters[1] except: vis.visualisation_on = cvtColor_com.ParametersDefaultValues[0] color_conv_string = cvtColor_com.ParametersDefaultValues[1] message = data[1:] # data[0] is the topic image = Socket.reconstruct_array_from_bytes_message_cv2correction(message) try: vis.visualised_data = cv2.cvtColor(image, getattr(cv2, color_conv_string)) except Exception as e: vis.visualised_data = np.array((10, 10)) print('cvtColor {} operation failed with exception {}'.format( worker_object.node_index, e)) return [vis.visualised_data]
def resize(data, parameters): global vis try: vis.visualisation_on = parameters[0] x_shape = parameters[1] y_shape = parameters[2] except: vis.visualisation_on = resize_com.ParametersDefaultValues[0] x_shape = resize_com.ParametersDefaultValues[1] y_shape = resize_com.ParametersDefaultValues[2] message = data[1:] # data[0] is the topic image = Socket.reconstruct_array_from_bytes_message_cv2correction(message) try: vis.visualised_data = cv2.resize(image, (x_shape, y_shape)) except Exception as e: vis.visualised_data = np.array((10, 10)) print('resize {} operation failed with exception {}'.format(worker_object.node_index, e)) return [vis.visualised_data]