sunny333's picture
new changes for keras OCR
1fe841e
import cv2 as cv
import easyocr
import keras_ocr
def pre_process(image):
print("doing pre-processing")
#image = cv2.imread(imagePath)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
_, binary_image = cv.threshold(gray, 150, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
processed_image = cv.medianBlur(binary_image, 3)
return processed_image
def extract_text_easyOCR(image):
#image = pre_process(image)
reader = easyocr.Reader(['en'])
results = reader.readtext(image)
text = " ".join([result[1] for result in results])
for (bbox, text1, prob) in results:
image = draw_box(image,bbox,text1)
return text,image
def ocr_with_keras(img):
output_text = ''
pipeline=keras_ocr.pipeline.Pipeline()
images=[keras_ocr.tools.read(img)]
predictions=pipeline.recognize(images)
print("keras----:-",predictions)
first=predictions[0]
for text,box in first:
output_text += ' '+ text
return output_text,img
#-------------
def draw_box(image,bbbox,text):
print("box",bbbox)
start_p =(bbbox[0])
end_p = (bbbox[2])
print(f"start:{start_p[0]},end:{end_p}")
color = (0, 255, 0)
thick = 5
image = cv.rectangle(image, (int(start_p[0]),int(start_p[1])), (int(end_p[0]),int(end_p[1])), color, thick)
image = put_text(image,bbbox[0],text)
return image
def put_text(image,bbox,text):
font = cv.FONT_HERSHEY_SIMPLEX
org = (int(bbox[0]), int(bbox[1]))
fontScale = 2
color = (245, 15, 57)
thickness = 4
image = cv.putText(image, text, org, font,
fontScale, color, thickness, cv.LINE_AA)
return image