|
import pandas as pd |
|
import numpy as np |
|
import streamlit as st |
|
import easyocr |
|
import PIL |
|
from PIL import Image, ImageDraw |
|
from captcha.image import ImageCaptcha |
|
import random, string |
|
|
|
def rectangle(image, result): |
|
|
|
""" draw rectangles on image based on predicted coordinates""" |
|
draw = ImageDraw.Draw(image) |
|
for res in result: |
|
top_left = tuple(res[0][0]) |
|
bottom_right = tuple(res[0][2]) |
|
draw.rectangle((top_left, bottom_right), outline="blue", width=2) |
|
|
|
st.image(image) |
|
|
|
|
|
|
|
|
|
|
|
|
|
length_captcha = 4 |
|
width = 200 |
|
height = 150 |
|
|
|
|
|
def captcha_control(): |
|
|
|
if 'controllo' not in st.session_state or st.session_state['controllo'] == False: |
|
st.title("Captcha Control on OCR") |
|
|
|
|
|
st.session_state['controllo'] = False |
|
col1, col2 = st.columns(2) |
|
|
|
|
|
if 'Captcha' not in st.session_state: |
|
st.session_state['Captcha'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length_captcha)) |
|
print("the captcha is: ", st.session_state['Captcha']) |
|
|
|
|
|
image = ImageCaptcha(width=width, height=height) |
|
data = image.generate(st.session_state['Captcha']) |
|
col1.image(data) |
|
capta2_text = col2.text_area('Enter captcha text', height=30) |
|
|
|
|
|
if st.button("Verify the code"): |
|
print(capta2_text, st.session_state['Captcha']) |
|
capta2_text = capta2_text.replace(" ", "") |
|
|
|
if st.session_state['Captcha'].lower() == capta2_text.lower().strip(): |
|
del st.session_state['Captcha'] |
|
col1.empty() |
|
col2.empty() |
|
st.session_state['controllo'] = True |
|
st.experimental_rerun() |
|
else: |
|
|
|
st.error("🚨 Error on Captcha...") |
|
del st.session_state['Captcha'] |
|
del st.session_state['controllo'] |
|
st.experimental_rerun() |
|
else: |
|
|
|
st.stop() |
|
|
|
|
|
|
|
|
|
|
|
st.title("Get text from image with Persian and Arabic OCR") |
|
|
|
|
|
st.markdown("## Persian and Arabic OCR :") |
|
|
|
|
|
|
|
def main(): |
|
holder = st.empty() |
|
|
|
file = holder.file_uploader(label = "Upload Here", type=['png', 'jpg', 'jpeg']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
if file is not None: |
|
image = Image.open(file) |
|
st.image(image) |
|
holder.empty() |
|
|
|
|
|
reader = easyocr.Reader(['fa','ar'], gpu=False) |
|
result = reader.readtext(np.array(image)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for idx in range(len(result)): |
|
pred_text = result[idx][1] |
|
st.write(pred_text) |
|
|
|
|
|
textdic_easyocr = {} |
|
for idx in range(len(result)): |
|
pred_coor = result[idx][0] |
|
pred_text = result[idx][1] |
|
pred_confidence = result[idx][2] |
|
textdic_easyocr[pred_text] = {} |
|
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence |
|
|
|
|
|
df = pd.DataFrame.from_dict(textdic_easyocr).T |
|
st.table(df) |
|
|
|
|
|
rectangle(image, result) |
|
|
|
st.spinner(text="In progress...") |
|
|
|
else: |
|
st.write("Upload your image") |
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'controllo' not in st.session_state or st.session_state['controllo'] == False: |
|
captcha_control() |
|
else: |
|
main() |
|
|