File size: 1,757 Bytes
e66cbf8
d1278a0
e66cbf8
 
 
d1278a0
 
47219a9
 
 
 
 
 
 
 
 
 
 
 
d1278a0
47219a9
 
 
 
 
 
 
 
 
 
d4a7cc5
 
 
47219a9
d1278a0
d4a7cc5
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
import paddlehub as hub
from PIL import Image
import io


from paddleocr import PaddleOCR,draw_ocr
def ocr(img,lang):
    # Paddleocr supports Chinese, English, French, German, Korean and Japanese.
    # You can set the parameter `lang` as `ch`, `en`, `french`, `german`, `korean`, `japan`
    # to switch the language model in order.
    ocr = PaddleOCR(use_angle_cls=True, lang=lang) # need to run only once to download and load model into memory
    img_path = 'PaddleOCR/doc/imgs_en/img_12.jpg'
    result = ocr.ocr(img, cls=True)
    for idx in range(len(result)):
        res = result[idx]
        for line in res:
            print(line)

    # draw result
    from PIL import Image
    result = result[0]
    image = Image.open(img_path).convert('RGB')
    boxes = [line[0] for line in result]
    st.text(boxes)
    txts = [line[1][0] for line in result]
    st.text(txts)
    scores = [line[1][1] for line in result]
    st.text(scores)
    im_show = draw_ocr(image, boxes, txts, scores, font_path='/simfang.ttf')
    im_show = Image.fromarray(im_show)
    st.image(im_show, caption='Sunrise by the mountains')
    #im_show.save('result.jpg')

_, col0, _ = st.columns([1, 3, 1])
with col0:
    st.header("OCR")
    form = st.form(key='my-form')
uploaded_file = form.file_uploader('Choose your img file', type=['png', 'jpg'],accept_multiple_files=False)
submit = form.form_submit_button('Submit')
if submit:
    if uploaded_file is not None:
        bytes_data = uploaded_file.getvalue()
        if bytes_data is not None:
            bytes_data = uploaded_file.read()
            image = Image.open(io.BytesIO(bytes_data))
            st.image(image, caption='Sunrise by the mountains')
            ocr(image,'ch')