ngor21/txt
0
1import pandas as pd2import numpy as np3import streamlit as st4import easyocr5import PIL6from PIL import Image, ImageDraw7 8def rectangle(image, result):9 # https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/10 """ draw rectangles on image based on predicted coordinates"""11 draw = ImageDraw.Draw(image)12 for res in result:13 top_left = tuple(res[0][0]) # top left coordinates as tuple14 bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple15 draw.rectangle((top_left, bottom_right), outline="blue", width=2)16 #display image on streamlit17 st.image(image)18 19 20# main title21st.title("Text recognition on engineering drawings")22 23# subtitle24st.markdown("## Neural network model for text recognition")25 26# upload image file27file = st.file_uploader(label = "Upload Here", type=['png', 'jpg', 'jpeg'])28 29#read the csv file and display the dataframe30if file is not None:31 image = Image.open(file) # read image with PIL library32 st.image(image) #display33 34 # it will only detect the English and Turkish part of the image as text35 reader = easyocr.Reader(['en'], gpu=False) 36 result = reader.readtext(np.array(image)) # turn image to numpy array37 38 # Add a placeholder39 # latest_iteration = st.empty()40 # bar = st.progress(0)41 42 # for i in range(100):43 # Update the progress bar with each iteration.44 # latest_iteration.text(f'Iteration {i+1}')45 # bar.progress(i + 1)46 # time.sleep(0.1)47 48 # collect the results in the dictionary:49 textdic_easyocr = {}50 for idx in range(len(result)):51 pred_coor = result[idx][0]52 pred_text = result[idx][1]53 pred_confidence = result[idx][2]54 textdic_easyocr[pred_text] = {}55 textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence56 57 # create a data frame which shows the predicted text and prediction confidence58 df = pd.DataFrame.from_dict(textdic_easyocr).T59 st.table(df)60 61 # get boxes on the image 62 rectangle(image, result)63 64 st.spinner(text="In progress...")65 66else:67 st.write("Upload your image")