Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,40 +1,42 @@
|
|
1 |
-
from paddleocr import PaddleOCR
|
2 |
-
from PIL import Image
|
3 |
-
from numpy import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
int(1000 * (bbox[
|
11 |
-
int(1000 * (bbox[
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
width * (bbox[
|
20 |
-
height * (bbox[
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# print(line[
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
1 |
+
from paddleocr import PaddleOCR
|
2 |
+
from PIL import Image
|
3 |
+
from numpy import asarrayù
|
4 |
+
|
5 |
+
ocr = PaddleOCR(use_angle_cls=True)
|
6 |
+
|
7 |
+
def normalize_bbox(bbox, width, height):
|
8 |
+
|
9 |
+
return [
|
10 |
+
int(1000 * (bbox[0] / width)),
|
11 |
+
int(1000 * (bbox[1] / height)),
|
12 |
+
int(1000 * (bbox[2] / width)),
|
13 |
+
int(1000 * (bbox[3] / height)),
|
14 |
+
]
|
15 |
+
|
16 |
+
def unnormalize_box(bbox, width, height):
|
17 |
+
|
18 |
+
return [
|
19 |
+
width * (bbox[0] / 1000),
|
20 |
+
height * (bbox[1] / 1000),
|
21 |
+
width * (bbox[2] / 1000),
|
22 |
+
height * (bbox[3] / 1000),
|
23 |
+
]
|
24 |
+
|
25 |
+
|
26 |
+
def OCR(image):
|
27 |
+
|
28 |
+
result = ocr.ocr(asarray(image), cls=True)
|
29 |
+
bboxes = []
|
30 |
+
words = []
|
31 |
+
|
32 |
+
for idx in range(len(result)):
|
33 |
+
res = result[idx]
|
34 |
+
|
35 |
+
for line in res:
|
36 |
+
# print(line)
|
37 |
+
# print(line[0][0] + line[0][2])
|
38 |
+
bboxes.append(normalize_bbox(line[0][0]+line[0][2], image.width, image.height))
|
39 |
+
# print(line[1][0])
|
40 |
+
words.append(line[1][0])
|
41 |
+
|
42 |
+
return bboxes, words
|