File size: 2,705 Bytes
875c5be
 
 
 
d1b5139
875c5be
 
8ed9fa5
875c5be
d1b5139
875c5be
 
 
 
 
8ed9fa5
 
 
 
 
 
 
 
 
 
 
 
d1b5139
8ed9fa5
 
d1b5139
 
8ed9fa5
 
d1b5139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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.")