Text Extraction from image after detecting text region with contours

2024/9/8 10:44:25

I want to build an OCR for an image using machine learning in python. I have preprocessed image by converting it to grayscale , applied otsu thresholding . I then used the contours to find the text regions and draw rectangular boxes over it . But how do i extract the detected text after that . I dont want to use pytesseract . I want to use knn or SVM or CNN for prediction But The main problem I am facing is to how to get the detected text from image using contours .

Image=cv2.imread('DL.png')
I=Image.copy()
i=Image.copy()
G_Image=cv2.cvtColor(Image,cv2.COLOR_BGR2GRAY)#Otsu Thresholding
blur = cv2.GaussianBlur(G_Image,(1,1),0)
ret,th = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
image, contours, hierarchy = cv2.findContours(th,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#img = cv2.drawContours(Image, contours, -1, (0,255,0), 3)for contour in contours:# get rectangle bounding contour[x, y, w, h] = cv2.boundingRect(contour)if h>20:continue# draw rectangle around contour on original imagecv2.rectangle(I, (x, y), (x + w, y + h), (255, 0, 255), 0)

Above is the code i wrote . This is the output image after contour rectangles are formed on detected text

enter image description here

Now how do I only use these detected regions and send them to my machine learning algorithm (KNN,SVM or CNN) for getting the text from image .

Answer

To crop the text areas you can use numpy slicing (as the image is effectively a numpy array):

letter = I[y:y+h, x:x+w]

In your loop that could create a new numpy array (cropped image) for every letter. Resize each of these to e.g. 28x28 and you have the right shape for the popular MNIST example.

For further ideas I can recommend the following git-repo, which creates a ML model for handwritten letters: EMNIST

It will be interesting how you handle incorrect/too coarse grained text detections like the "DE" or "RT" in DEPARTMENT. Andrew NG suggested in his Coursera Course for Machine Learning to use a ML model to detect the gaps between letters and split by these.

https://en.xdnf.cn/q/72576.html

Related Q&A

Pyinstaller executable fails importing torchvision

This is my main.py:import torchvision input("Press key")It runs correctly in the command line: python main.pyI need an executable for windows. So I did : pyinstaller main.pyBut when I launche…

Embedding Python in C: Having problems importing local modules

I need to run Python scripts within a C-based app. I am able to import standard modules from the Python libraries i.e.:PyRun_SimpleString("import sys")But when I try to import a local module …

Primitive Calculator - Dynamic Approach

Im having some trouble getting the correct solution for the following problem: Your goal is given a positive integer n, find the minimum number ofoperations needed to obtain the number n starting from …

Cant pickle : attribute lookup builtin.function failed

Im getting the error below, the error only happens when I add delay to process_upload function, otherwise it works without a problem.Could someone explain what this error is, why its happening and any…

Pandas Merge two DataFrames without some columns

ContextIm trying to merge two big CSV files together.ProblemLets say Ive one Pandas DataFrame like the following...EntityNum foo ... ------------------------ 1001.01 100 1002.02 50 1003…

Getting Youtube data using Python

Im trying to learn how to analyze social media data available on the web and Im starting with Youtube.from apiclient.errors import HttpError from outh2client.tools import argparser from apiclient.disco…

matplotlib does not show legend in scatter plot

I am trying to work on a clustering problem for which I need to plot a scatter plot for my clusters.%matplotlib inline import matplotlib.pyplot as plt df = pd.merge(dataframe,actual_cluster) plt.scatte…

Correct usage of asyncio.Conditions wait_for() method

Im writing a project using Pythons asyncio module, and Id like to synchronize my tasks using its synchronization primitives. However, it doesnt seem to behave as Id expect.From the documentation, it se…

displaying newlines in the help message when using pythons optparse

Im using the optparse module for option/argument parsing. For backwards compatibility reasons, I cant use the argparse module. How can I format my epilog message so that newlines are preserved?In th…

How to use a learnable parameter in pytorch, constrained between 0 and 1?

I want to use a learnable parameter that only takes values between 0 and 1. How can I do this in pytorch? Currently I am using: self.beta = Parameter(torch.Tensor(1)) #initialize zeros(self.beta)But I…