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
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 .