| import streamlit as st | |
| import easyocr | |
| from PIL import Image | |
| import numpy as np | |
| from deep_translator import GoogleTranslator | |
| # Initialize EasyOCR Reader | |
| reader = easyocr.Reader(['en']) # Initialize reader for English | |
| st.title("Image to Text Extraction and Translation Using EasyOCR and Translation Service") | |
| # File uploader for images | |
| uploaded_file = st.file_uploader("Choose an image... (JPEG, PNG)", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Load the image using PIL | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption='Uploaded Image', use_container_width=True) | |
| st.write("") | |
| # Convert image to numpy array | |
| image_np = np.array(image) | |
| # Use EasyOCR to do OCR on the image | |
| results = reader.readtext(image_np) | |
| # Extract and display the detected text | |
| st.subheader("Extracted English Text:") | |
| extracted_texts = [] | |
| if results: | |
| for (bbox, text, prob) in results: | |
| st.write(f"Detected Text: '{text}' with confidence {prob:.2f}") | |
| extracted_texts.append(text) | |
| else: | |
| st.write("No text detected.") | |
| # Combine all extracted texts into a single string | |
| english_text = ' '.join(extracted_texts) | |
| # Translate English text to Persian using Google Translator | |
| st.subheader("Translated Persian Text:") | |
| if english_text: | |
| with st.spinner("Translating text..."): | |
| try: | |
| persian_text = GoogleTranslator(source='en', target='fa').translate(english_text) | |
| st.write(persian_text) | |
| except Exception as e: | |
| st.write(f"Translation failed: {e}") | |
| else: | |
| st.write("No text to translate.") | |
| # import streamlit as st | |
| # import easyocr | |
| # from PIL import Image | |
| # import numpy as np | |
| # # Initialize EasyOCR Reader | |
| # reader = easyocr.Reader(['en']) # Initialize reader for English | |
| # st.title("Image to Text Extraction Using EasyOCR") | |
| # # File uploader for images | |
| # uploaded_file = st.file_uploader("Choose an image... (JPEG, PNG)", type=["jpg", "jpeg", "png"]) | |
| # if uploaded_file is not None: | |
| # # Load the image using PIL | |
| # image = Image.open(uploaded_file) | |
| # st.image(image, caption='Uploaded Image', use_container_width=True) | |
| # st.write("") | |
| # # Convert image to numpy array | |
| # image_np = np.array(image) | |
| # # Use EasyOCR to do OCR on the image | |
| # results = reader.readtext(image_np) | |
| # # Extract and display the detected text | |
| # st.subheader("Extracted Text:") | |
| # extracted_texts = [] | |
| # if results: | |
| # for (_, text, _) in results: | |
| # st.write(text) | |
| # extracted_texts.append(text) | |
| # else: | |
| # st.write("No text detected.") |