File size: 1,056 Bytes
de04f9b
 
82f712b
de04f9b
47ed111
de04f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc6e69c
de04f9b
 
 
 
 
 
 
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
from paddleocr import PaddleOCR
from PIL import Image
from numpy import asarray

ocr = PaddleOCR(use_angle_cls=True, lang="en") #will be italian

def normalize_bbox(bbox, width, height):

    return [
        int(1000 * (bbox[0] / width)),
        int(1000 * (bbox[1] / height)),
        int(1000 * (bbox[2] / width)),
        int(1000 * (bbox[3] / height)),
    ]

def unnormalize_box(bbox, width, height):

    return [
        width * (bbox[0] / 1000),
        height * (bbox[1] / 1000),
        width * (bbox[2] / 1000),
        height * (bbox[3] / 1000),
    ]


def OCR(image):
    
    result = ocr.ocr(asarray(image), cls=True)
    bboxes = []
    words = []

    for idx in range(len(result)):
        res = result[idx]

        for line in res:
            if(line[1][0] == ""): continue
            # print(line)
            # print(line[0][0] + line[0][2])
            bboxes.append(normalize_bbox(line[0][0]+line[0][2], image.width, image.height))
            # print(line[1][0])
            words.append(line[1][0])

    return bboxes, words