I am unable to resolve the issue: Invoke Python Method: One or more errors occurred. (Error invoking Python method)

Hello, I’m a student who is currently learning UiPath. I found a tutorial on this method at GitHub - YenLinWu/RPA_UiPath: RPA(Robotic Process Automation) UiPath 實戰開發範例, but I’m facing an issue with “Invoke Python Method: One or more errors occurred. (Error invoking Python method).” I’m not sure how to resolve it

The file that he’s reading can be used in VS Code but not in UiPath. He created a virtual environment using Anaconda and confirmed that the paths are correct. He has also searched for relevant articles but is still encountering this issue.

Since I am a new user, I cannot upload files. Here is the content of the code:
import os
import numpy as np
from pathlib import Path
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras import layers
from tensorflow.keras.models import Model, load_model

os.environ[‘TF_FORCE_GPU_ALLOW_GROWTH’] = ‘true’

def CRNN( ImageFolderPath, ImageName ):

# Pretained model path
model_path = "CRNN.h5"
       
# Get list of all the captcha testing images
data_dir = Path( ImageFolderPath )
Image = list(map(str, list(data_dir.glob(ImageName)))) 

# Mapping characters to integers
Characters = {'0','1','2','3','4','5','6','7','8','9'}
char_to_num = layers.experimental.preprocessing.StringLookup(
                      vocabulary=sorted(list(Characters)), num_oov_indices=0, mask_token=None )
# Mapping integers back to original characters
num_to_char = layers.experimental.preprocessing.StringLookup(
                      vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True )  

def encode_single_sample( img_path ): 
    # 1. Read image
    img = tf.io.read_file( img_path )     
    # 2. Decode and convert to grayscale
    img = tf.io.decode_png( img, channels=1 )
    # 3. Convert to float32 in [0, 1] range
    img = tf.image.convert_image_dtype( img, tf.float32 )
    # 4. Resize to the desired size
    img = tf.image.resize( img, [32,104] )
    # 5. Transpose the image because we want the time dimension to correspond to the width of the image, 
    #    i.e., shape = (img_weight,img_height,1).
    img = tf.transpose( img, perm=[1,0,2] )
    return { "image": img }

# Create validation dataset object
validation_dataset = tf.data.Dataset.from_tensor_slices( (np.array(Image)) ) 
validation_dataset = ( validation_dataset.map(
                       encode_single_sample, num_parallel_calls=tf.data.experimental.AUTOTUNE
                       ).batch(1).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
                      )

# Build an custom endpoint layer of the pretrained model.
class CTCLayer( layers.Layer ):
    def __init__( self, name=None, **kwargs ):
        super().__init__( name=name )
        self.loss_fn = K.ctc_batch_cost

    def call( self, y_true, y_pred ):
        batch_len = tf.cast( tf.shape(y_true)[0], dtype='int64' )
        input_length = tf.cast( tf.shape(y_pred)[1], dtype='int64' )
        label_length = tf.cast( tf.shape(y_true)[1], dtype='int64' )
    
        input_length = input_length*tf.ones( shape=(batch_len,1), dtype='int64' )
        label_length = label_length*tf.ones( shape=(batch_len,1), dtype='int64' )

        loss = self.loss_fn( y_true, y_pred, input_length, label_length )
        self.add_loss(loss)

        return y_pred

model = load_model( model_path, custom_objects={'CTCLayer': CTCLayer} )

# Get the prediction model by extracting layers till the output layer
prediction_model = Model( model.get_layer( name='Input' ).input, model.get_layer( name='Softmax' ).output, name='Prediction' )

# A utility function to decode the output of the network
def decode_batch_predictions( pred ):
    input_len = np.ones(pred.shape[0]) * pred.shape[1]
    # Use greedy search. For complex tasks, you can use beam search
    results = K.ctc_decode( pred, input_length=input_len, greedy=True )[0][0][:,:4]
    # Iterate over the results and get back the text
    output_text = []
    for res in results:
        res = tf.strings.reduce_join(num_to_char(res)).numpy().decode('utf-8')
        output_text.append(res)
    return output_text    

for batch in validation_dataset.take(1):
    batch_image = batch['image']
    pred = prediction_model.predict(batch_image)
    pred_text = decode_batch_predictions(pred)

return pred_text[0]

Hi @JunYan_Wu

Could you share what is showing in Locals Panel > Exception Section ?

No problem, it seems she encountered this issue
Issue 1
RemoteException wrapping System.AggregateException: One or more errors occurred. (Error invoking Python method) —> RemoteException wrapping System.InvalidOperationException: Error invoking Python method —> RemoteException wrapping System.IO.IOException: Pipe is broken.
at System.IO.Pipes.PipeStream.CheckWriteOperations()
at System.IO.Pipes.PipeStream.Flush()
at System.IO.StreamWriter.Flush(Boolean flushStream,
Boolean flushEncoder)
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.TextWriter.Dispose()
at UiPath.Python.Service.PythonProxy.RequestAsync(PythonRequest request,
CancellationToken ct)
at UiPath.Python.Service.PythonProxy.InvokeMethod(Guid instance,
String method,
IEnumerable1 args) at UiPath.Python.Impl.OutOfProcessEngine.<>c__DisplayClass14_0.<InvokeMethod>b__0() at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread,
ExecutionContext executionContext,
ContextCallback callback,
Object state)
— End of stack trace from previous location —
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread,
ExecutionContext executionContext,
ContextCallback callback,
Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot,
Thread threadPoolThread)
— End of stack trace from previous location —
at UiPath.Python.Activities.InvokeMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Python.Activities.InvokeMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

Issue 2
“Id”: “1.103”,
“InstanceId”: “11”,
“Name”: “Invoke Python Method”,
“TypeName”: “UiPath.Python.Activities.InvokeMethod”



In the information I’ve gathered, I’ve also come across the possibility that it might be due to missing pip-installed packages or path errors. However, I’ve encountered these errors even in the correct paths and locations, and I can successfully execute the code in VS Code. The practice examples I’ve found are ones that others have been able to run successfully, so there shouldn’t be any errors

@JunYan_Wu

Please check the event viewer logs at the time of failure

It can be because of missing .net runtime version as well…5+ is what is needed

Cheers