File size: 1,697 Bytes
ac510cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62

from detectron2.data.detection_utils import read_image,pil_image_to_numpy
from detectron2.utils.visualizer import Visualizer
from sam_everything import visimage_to_pil
import numpy as np
def do_ocr(ocr_type,input):
    print(ocr_type)
    result = None
    np_image = pil_image_to_numpy(input)
    if ocr_type == "OCR":
        from paddleocr import PaddleOCR
        ocr = PaddleOCR(lang='ch', use_angle_cls=True)
        # img_path = 'exp.jpeg'
        result = ocr.ocr(np_image)
        print(result)
        result = parse_paddle_result(result)
 
    elif ocr_type == "Easy":
        import easyocr
        reader = easyocr.Reader(['en','ch_sim'])  # 初始化 EasyOCR,选择需要支持的语言(例如英文)
        result = reader.readtext(np_image)
        result = parse_esay_result(result)

    view = Visualizer(np_image)
    for item in result:
        polygon = np.array(item['box'])
        view.draw_polygon(polygon, "k")

    vis_image = view.get_output()
    pil_images = visimage_to_pil([vis_image])
    return pil_images[0],result

def parse_esay_result(data):
    results = []
    for entry in data:
        box = entry[0]
        text = entry[1]
        confidence = entry[2]
        result = {
            'box': box,
            'text': text,
            'confidence': confidence
        }
        results.append(result)
    return results

def parse_paddle_result(data):
    results = []
    for entry in data[0]:
        box = entry[0]
        text = entry[1][0]
        confidence = entry[1][1]
        result = {
            'box': box,
            'text': text,
            'confidence': confidence
        }
        results.append(result)
    return results