farid678 commited on
Commit
c50cf46
·
verified ·
1 Parent(s): ee802a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -4
app.py CHANGED
@@ -1,12 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import easyocr
3
  from PIL import Image
4
  import numpy as np
 
5
 
6
  # Initialize EasyOCR Reader
7
- reader = easyocr.Reader(['en']) # You can specify the language(s) here
8
 
9
- st.title("Image to Text Extraction Using EasyOCR")
10
 
11
  # File uploader for images
12
  uploaded_file = st.file_uploader("Choose an image... (JPEG, PNG)", type=["jpg", "jpeg", "png"])
@@ -24,10 +63,32 @@ if uploaded_file is not None:
24
  results = reader.readtext(image_np)
25
 
26
  # Extract and display the detected text
27
- st.subheader("Extracted Text:")
28
-
29
  if results:
30
  for (bbox, text, prob) in results:
31
  st.write(f"Detected Text: '{text}' with confidence {prob:.2f}")
 
32
  else:
33
  st.write("No text detected.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import streamlit as st
2
+ # import easyocr
3
+ # from PIL import Image
4
+ # import numpy as np
5
+
6
+ # # Initialize EasyOCR Reader
7
+ # reader = easyocr.Reader(['en']) # You can specify the language(s) here
8
+
9
+ # st.title("Image to Text Extraction Using EasyOCR")
10
+
11
+ # # File uploader for images
12
+ # uploaded_file = st.file_uploader("Choose an image... (JPEG, PNG)", type=["jpg", "jpeg", "png"])
13
+
14
+ # if uploaded_file is not None:
15
+ # # Load the image using PIL
16
+ # image = Image.open(uploaded_file)
17
+ # st.image(image, caption='Uploaded Image', use_container_width=True)
18
+ # st.write("")
19
+
20
+ # # Convert image to numpy array
21
+ # image_np = np.array(image)
22
+
23
+ # # Use EasyOCR to do OCR on the image
24
+ # results = reader.readtext(image_np)
25
+
26
+ # # Extract and display the detected text
27
+ # st.subheader("Extracted Text:")
28
+
29
+ # if results:
30
+ # for (bbox, text, prob) in results:
31
+ # st.write(f"Detected Text: '{text}' with confidence {prob:.2f}")
32
+ # else:
33
+ # st.write("No text detected.")
34
+
35
+
36
+
37
+
38
+
39
  import streamlit as st
40
  import easyocr
41
  from PIL import Image
42
  import numpy as np
43
+ import requests
44
 
45
  # Initialize EasyOCR Reader
46
+ reader = easyocr.Reader(['en'])
47
 
48
+ st.title("Image to Text Extraction and Translation Using EasyOCR and Translation API")
49
 
50
  # File uploader for images
51
  uploaded_file = st.file_uploader("Choose an image... (JPEG, PNG)", type=["jpg", "jpeg", "png"])
 
63
  results = reader.readtext(image_np)
64
 
65
  # Extract and display the detected text
66
+ st.subheader("Extracted English Text:")
67
+ extracted_texts = []
68
  if results:
69
  for (bbox, text, prob) in results:
70
  st.write(f"Detected Text: '{text}' with confidence {prob:.2f}")
71
+ extracted_texts.append(text)
72
  else:
73
  st.write("No text detected.")
74
+
75
+ # Combine all extracted texts into a single string
76
+ english_text = ' '.join(extracted_texts)
77
+
78
+ # Translate English text to Persian using an API
79
+ st.subheader("Translated Persian Text:")
80
+ if english_text:
81
+ with st.spinner("Translating text..."):
82
+ # Make a request to the translation API
83
+ translation_response = requests.post('https://translate.sravn.io/translate', data={
84
+ 'q': english_text,
85
+ 'source': 'en',
86
+ 'target': 'fa'
87
+ })
88
+ if translation_response.status_code == 200:
89
+ persian_text = translation_response.json()['translatedText']
90
+ st.write(persian_text)
91
+ else:
92
+ st.write("Translation failed. Please try again later.")
93
+ else:
94
+ st.write("No text to translate.")