hello

#2
by userbox - opened

how this ocr can be tested?

Techie Traders org

hey, here is how to predict

def transform_image(image):
transform = T.Compose([T.ToTensor()]) #torchvision.transforms
device = 'cuda' if torch.cuda.is_available() else 'cpu'
image = image.resize((200,50))
image = transform(image)
image = Variable(image).to(device)
image = image.unsqueeze(1)
return image

def model(model_path,input):
session = ort.InferenceSession(model_path)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
result = session.run([output_name], {input_name: input.numpy()})[0]
return torch.tensor(result)

def get_label(model_prediction):
lab = ''
for idx in range(max_captcha_len): #refer captcha images in the model card to find out length
get_char = _cls[np.argmax(model_prediction.squeeze().cpu().tolist()[ _cls_dim * idx : _cls_dim * (idx + 1)])] #_cls = all_classification_tokens_in_captcha_image+'$' {'$' as padding}, _cls_dim = len_of_cls_tokens
lab += get_char
return lab

image = Image.open(path)
image = image.convert('L')
image = transform_image(image)
model_pred = model(path,image)
pred_lab = get_label(model_pred)

can you share any notebook or full code to test the model?
because this code is not working properly i guess...
also image resizing is reversed...it is [50,200] actually, i have made the model work somehow but not getting the exact results even in the example given in REDME file,
So what exactly can be done in this case? your guidance will be helpful.
thanks

Techie Traders org
edited Jul 24

I am trying to use the v4 model and I no have results, also, I get this error: “ValueError: attempt to get argmax of an empty sequence”.

The code is the same as your example.

I get a error:

Traceback (most recent call last):
File "d:\Temp\PythonProjects\AutoEasyConnect\test_captcha_1.py", line 44, in
pred_lab = get_label(model_pred)
File "d:\Temp\PythonProjects\AutoEasyConnect\test_captcha_1.py", line 34, in get_label
get_char = _cls[np.argmax(model_prediction.squeeze().cpu().tolist()[ _cls_dim * idx : _cls_dim * (idx + 1)])] #_cls = all_classification_tokens_in_captcha_image+'$' {'$' as padding}, _cls_dim = len_of_cls_tokens
File "D:\Temp\PythonProjects\AutoEasyConnect.venv\lib\site-packages\numpy_core\fromnumeric.py", line 1359, in argmax
return _wrapfunc(a, 'argmax', axis=axis, out=out, **kwds)
File "D:\Temp\PythonProjects\AutoEasyConnect.venv\lib\site-packages\numpy_core\fromnumeric.py", line 54, in _wrapfunc
return _wrapit(obj, method, *args, **kwds)
File "D:\Temp\PythonProjects\AutoEasyConnect.venv\lib\site-packages\numpy_core\fromnumeric.py", line 46, in _wrapit
result = getattr(arr, method)(*args, **kwds)
ValueError: attempt to get argmax of an empty sequence

when i modify get_label to:
def get_label(model_prediction):
lab = ''
# 检查并打印预测的形状和内容
predictions = model_prediction.squeeze().cpu().tolist()
print(f"Predictions length: {len(predictions)}")

for idx in range(max_captcha_len):  
    start_index = _cls_dim * idx  
    end_index = _cls_dim * (idx + 1)  
    
    # 验证切片范围  
    print(f"Computing slice from {start_index} to {end_index}")  
    
    slice_ = predictions[start_index:end_index]  
    
    if not slice_:  # 检查切片是否为空  
        print(f"Warning: Empty slice encountered at index {idx}")  
        continue  
    
    print(f"Slice for index {idx}: {slice_}")  
    
    # 确保我们使用的是非空的切片  
    get_char = _cls[np.argmax(slice_)]  
    lab += get_char  

return lab

I get:

Predictions length: 185
Computing slice from 0 to 63
Slice for index 0: [-6.011777400970459, -11.153398513793945, -7.249768257141113, -9.971173286437988, -5.964890003204346, -9.804941177368164, -1.7018616199493408, -9.003511428833008, -9.19326400756836, -6.538347244262695, -7.317095756530762, -7.542794227600098, -5.662759780883789, -8.603363990783691, -9.275945663452148, -6.881734848022461, -2.6126089096069336, -8.45109748840332, -9.463533401489258, -9.464624404907227, -5.983384132385254, -3.9140233993530273, -7.682295322418213, -4.354710102081299, -7.446990013122559, -9.116670608520508, -9.47895622253418, -10.689746856689453, -8.91401481628418, -7.626779556274414, -5.8238019943237305, -9.292308807373047, -6.1750311851501465, -7.1464385986328125, -10.740039825439453, -10.318199157714844, -9.997430801391602, -5.39472770690918, -8.035531997680664, -4.670130729675293, -5.362524032592773, -7.912507057189941, -5.554369926452637, -5.717945098876953, -6.062051296234131, -9.896303176879883, -3.635836601257324, -4.838284492492676, -2.2408177852630615, -6.977108955383301, -8.49797534942627, -9.183833122253418, -8.484207153320312, -8.569570541381836, -8.000297546386719, -9.769613265991211, -9.604846954345703, -8.581310272216797, -6.709412574768066, -9.209495544433594, -8.723678588867188, -6.716584205627441, -10.691947937011719]
Computing slice from 63 to 126
Slice for index 1: [-9.964028358459473, -9.091068267822266, -10.978836059570312, -4.740383625030518, -7.417506217956543, -9.429573059082031, -9.85842514038086, -8.36265754699707, -9.50052261352539, -8.281058311462402, -9.134180068969727, -1.6840827465057373, -9.16760540008545, -3.3609724044799805, -9.869020462036133, -6.857561111450195, -4.960903167724609, -4.025699615478516, -6.962646484375, -5.680966377258301, -7.846404075622559, -5.358519554138184, -10.438152313232422, -8.026275634765625, -4.595043182373047, -10.13081169128418, -10.08181381225586, -1.115409255027771, -7.08476448059082, -11.069819450378418, -9.460857391357422, -10.075429916381836, -7.488295078277588, -8.681174278259277, -8.28897476196289, -7.401867866516113, -7.6863694190979, -10.36233139038086, -9.263040542602539, -8.184553146362305, -1.8378735780715942, -10.852747917175293, -10.521688461303711, -6.921008586883545, -8.215168952941895, -10.253421783447266, -8.777148246765137, -10.48219108581543, -6.78596830368042, -9.354012489318848, -3.003751039505005, -4.596195220947266, -7.260098934173584, -8.498811721801758, -6.440617561340332, -9.5824556350708, -9.691389083862305, -8.075456619262695, -7.964419364929199, -7.57810115814209, -6.615172863006592, -8.881489753723145, -9.7939453125]
Computing slice from 126 to 189
Slice for index 2: [-5.434416770935059, -5.282707691192627, -3.786999225616455, -11.020828247070312, -10.382978439331055, -8.250228881835938, -6.586619853973389, -1.406485676765442, -6.419070243835449, -10.764716148376465, -9.24475383758545, -10.184892654418945, -10.6348876953125, -9.458208084106445, 0.7452764511108398, -12.720460891723633, -9.787500381469727, -12.081489562988281, -9.45942497253418, -10.316030502319336, -10.11520767211914, -10.07498836517334, -9.081950187683105, -8.600582122802734, -7.065145492553711, -5.959444999694824, -7.783405303955078, -6.053805351257324, -8.947548866271973, -9.592658996582031, -8.77397632598877, -10.002896308898926, -8.205284118652344, -8.314950942993164, -8.37647819519043, -5.8926591873168945, -9.150171279907227, -0.22568762302398682, -7.506771564483643, -7.027074337005615, -8.535673141479492, -10.31076431274414, -8.877788543701172, -9.648378372192383, -6.387914180755615, -6.411273956298828, -4.710943698883057, -9.840852737426758, -8.922078132629395, -9.63744831085205, -9.063692092895508, 1.5989184379577637, -11.113716125488281, -9.804612159729004, -10.405682563781738, -0.6999501585960388, -8.440637588500977, -9.309297561645508, -9.064154624938965]
Computing slice from 189 to 252
Warning: Empty slice encountered at index 3
Computing slice from 252 to 315
Warning: Empty slice encountered at index 4
Computing slice from 315 to 378
Warning: Empty slice encountered at index 5

I use the image:
image.png

Sign up or log in to comment