File size: 1,657 Bytes
1fe841e
52d4971
1fe841e
52d4971
 
 
 
1fe841e
 
 
52d4971
 
 
1fe841e
52d4971
 
 
1fe841e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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