mp-02 commited on
Commit
de04f9b
1 Parent(s): e41ca05

Update utils.py

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