File size: 2,002 Bytes
e66cbf8
d1278a0
e66cbf8
 
 
4fb684e
d714153
d1278a0
47219a9
 
 
 
 
 
2208ac7
 
47219a9
 
 
 
 
d1278a0
47219a9
 
 
70a8600
 
 
47219a9
 
 
 
 
 
8344c73
d4a7cc5
70a8600
47219a9
d1278a0
d4a7cc5
 
 
 
 
 
 
 
 
 
 
2208ac7
 
b2b7097
 
 
 
 
 
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
import os
import streamlit as st
import paddlehub as hub
from PIL import Image
import io
import numpy
import cv2

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
    print(img)
    print(type(img))
    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]
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    im_pil = Image.fromarray(img)
    image = im_pil
    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='ocr output')
    #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='your img')
            pil_image=image.convert('RGB') 
            open_cv_image = numpy.array(pil_image) 
            # Convert RGB to BGR 
            open_cv_image = open_cv_image[:, :, ::-1].copy() 
            
            ocr(open_cv_image,'ch')