1

I tried to extract the chars from image which is handwritten devanagari script using python, But it only outputs one letter rather than whole paragraph.

`from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np
import argparse
import imutils
import cv2

labels = [u'\u091E',u'\u091F',u'\u0920',u'\u0921',u'\u0922',u'\u0923',u'\u0924',u'\u0925',u'\u0926',u'\u0927',u'\u0915',u'\u0928',u'\u092A',u'\u092B',u'\u092c',u'\u092d',u'\u092e',u'\u092f',u'\u0930',u'\u0932',u'\u0935',u'\u0916',u'\u0936',u'\u0937',u'\u0938',u'\u0939','ksha','tra','gya',u'\u0917',u'\u0918',u'\u0919',u'\u091a',u'\u091b',u'\u091c',u'\u091d',u'\u0966',u'\u0967',u'\u0968',u'\u0969',u'\u096a',u'\u096b',u'\u096c',u'\u096d',u'\u096e',u'\u096f']
#
import numpy as np
from keras.preprocessing import image
test_image = cv2.imread("out.jpg")
image = cv2.resize(test_image, (32,32))
image = image.astype("float") / 255.0
image = img_to_array(image)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = np.expand_dims(image, axis=0)
image = np.expand_dims(image, axis=3)
print("[INFO] loading network...")
import tensorflow as tf
model = tf.keras.models.load_model("HindiModel2.h5")
lists = model.predict(image)[0]
print("The letter is ",labels[np.argmax(lists)])`

this is the code I used, and also added the HindiModel2.h5 and out.jpg file.

link - https://drive.google.com/drive/folders/12jbrYg9Dj4QAPjj_887Q-jsQmgUM7AYz?usp=sharing

link for main github - https://github.com/darklord0303/Hindi-OCR

ranto berk
  • 11
  • 2
  • you are doing `model.predict(image)[0]` which takes only the 0th answer, can you try removing the `[0]` and then looping over the lists instead? – SajanGohil Apr 28 '21 at 05:09
  • tried that too, it still prints only one character, not even a sentence. – ranto berk Apr 28 '21 at 05:25
  • @SajanGohil when I try the method you said I get the following error `ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (32, 32, 1)` and i changed the code follows. `for img in image: lists = model.predict(img)`. i dont know what i made wrong. please help bro – ranto berk Apr 28 '21 at 08:58
  • you still have only one image, so looping over image makes no sense. what i wanted you to try was: `lists = model.predict(image)` and then `print([labels[np.argmax(list)] for list in lists])` – SajanGohil Apr 28 '21 at 09:01
  • @SajanGohil Its still same bro, Prints only one character. `[INFO] loading network... ['२']` also when I run `print(len(lists))` it prints 1 – ranto berk Apr 28 '21 at 09:04
  • Then I'll need info about your model, where did you get it from or how was it trained, without that I can't tell for sure if what's happening is expected or not. please update your question with that – SajanGohil Apr 28 '21 at 09:07
  • @SajanGohil Hai bro, i have added the link of github from where i get the py file and dataset, and also have attached here in this comment. https://github.com/darklord0303/Hindi-OCR – ranto berk Apr 28 '21 at 09:10
  • This looks like a classifier model and not OCR, so it will give only one output for the whole image, which is most probable class. Take a look at the training set and see if your image is of the same format as the training data (pretty sure it's not). The input is probably supposed to be like mnist, one character in the image. – SajanGohil Apr 28 '21 at 09:15
  • @SajanGohil. Sorry for bugging again. but can you please explain it a bit, Because am new to ML and just trying out things. – ranto berk Apr 28 '21 at 09:20
  • What you want to do is OCR, which is detect all characters and classify them, but the model you are using is for classification (give a class to one thing). For understanding classification you can look at some MNIST tutorial (MNIST is similar to dataset this model is trained on and is one of the most basic classification dataset). For understanding OCR, you can look at some article/paper for that. You can look into easyOCR, PaddleOCR and tesseract for OCR, all of them most likely have devnagri/specific language support. – SajanGohil Apr 28 '21 at 10:13

1 Answers1

0

The model you have shown can only recognize one character at a time based on the test image test2.png on its Github.

You can either move on to a new model like this one or you can divide the images into characters manually or make/find a new model that can divide the image into characters for you.

kinshukdua
  • 1,944
  • 1
  • 5
  • 15
  • The one model that you mentioned is for sanskrit right? But i need for Devanagari script. And thanks – ranto berk Apr 28 '21 at 09:28
  • Sanskrit uses the Devanagari script, furthermore this is the next iteration of this project- https://github.com/avadesh02/OCR-Devanagari. If the answer solved your problem please select it as the answer. – kinshukdua Apr 28 '21 at 10:20
  • sure bro, will check it and if it helps, sure to mark it as answer – ranto berk Apr 28 '21 at 10:34
  • am getting the following error bro, tried to fix, but nothing helps. `error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'` getting this error on pagesegmenter.py file at line 114 bro. can you please help me out. – ranto berk Apr 29 '21 at 04:59