Spaces:
Runtime error
Runtime error
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') |