Load Onnx Model in Opencv Dnn

Forward method error in DNN module of OpenCV (Ptyhon) using .onnx model

Your problem is actually that the input data you feed to your model doesn't match the shape of the data the model was trained on.

I used this answer to inspect your onnx model and it appears that it expects an input of shape (1, 1, 32, 100). I modified your code to reshape the image to 1 x 32 x 100 pixels and the inference actually runs without error.

EDIT

I've added some code to interpret the result of the inference. We now display the image and the inferred OCR text.
This doesn't seem to be working, but reading the tutorial on OpenCV, there should be two models:

  1. one that detects where there is text in the image. This network accepts images of various sizes, it returns the locations of text within the image and then cropped parts of the image, of sizes 100x32 are passed to the second
  2. one that actually does the "reading" and given patches of image, returns the characters. For this, there a file alphabet_36.txt that is provided together with the pre-trained models.

It isn't clear to me though which network to use for text detection. Hope the edited code below helps you develop your application further.

import cv2 as cv
import os
import numpy as np
import matplotlib.pyplot as plt
# The model is downloaded from here https://drive.google.com/drive/folders/1cTbQ3nuZG-EKWak6emD_s8_hHXWz7lAr
# model path
MODELS_PATH = './'
modelRecognition = os.path.join(MODELS_PATH,'CRNN_VGG_BiLSTM_CTC.onnx')

# read net
recognizer = cv.dnn.readNetFromONNX(modelRecognition)

# Download sample_image.png from https://i.ibb.co/fMmCB7J/sample-image.png (image host website)
sample_image = cv.imread('sample-image.png', cv.IMREAD_GRAYSCALE)
sample_image = cv.resize(sample_image, (100, 32))
sample_image = sample_image[:,::-1].transpose()

# Height and Width of the image
H,W = sample_image.shape

# Create a 4D blob from image
blob = cv.dnn.blobFromImage(sample_image, size=(H,W))
recognizer.setInput(blob)

# network inference
result = recognizer.forward()

# load alphabet
with open('alphabet_36.txt') as f:
alphabet = f.readlines()
alphabet = [f.strip() for f in alphabet]

# interpret inference results
res = []
for i in range(result.shape[0]):
ind = np.argmax(result[i,0])
res.append(alphabet[ind])
ocrtxt = ''.join(res)

# show image and detected OCR characters
plt.imshow(sample_image)
plt.title(ocrtxt)
plt.show()

Hope it helps.
Cheers

OpenCV: Error in loading Net from onnx file

As @berak suggestet, the issue was related to the OpenCV version (was 4.1.2). Updating to 4.5.5 solved the issue.



Related Topics



Leave a reply



Submit