rajsecrets0 commited on
Commit
48de7d6
โ€ข
1 Parent(s): a006020

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -1,28 +1,29 @@
1
  import streamlit as st
2
  from PIL import Image
3
  import pytesseract
4
- import shutil
5
 
6
- # Streamlit app title
7
- st.title("Image to Text Extraction App ๐Ÿ–ผ๏ธ๐Ÿ“")
 
 
 
 
8
 
9
- # Prompt for image upload
10
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
11
 
12
- # Check if tesseract is installed and in PATH
13
- pytesseract.pytesseract.tesseract_cmd = shutil.which("tesseract") or None
14
 
15
- # If an image is uploaded, perform OCR
16
- if uploaded_file is not None:
17
- image = Image.open(uploaded_file)
18
- st.image(image, caption="Uploaded Image", use_column_width=True)
19
-
20
- # Perform OCR using Tesseract
21
- with st.spinner("Extracting text..."):
22
- text = pytesseract.image_to_string(image)
23
-
24
- # Display the extracted text
25
- st.subheader("Extracted Text:")
26
- st.write(text)
27
- else:
28
- st.warning("Please upload an image file to extract text.")
 
1
  import streamlit as st
2
  from PIL import Image
3
  import pytesseract
4
+ import io
5
 
6
+ def perform_ocr(image):
7
+ try:
8
+ text = pytesseract.image_to_string(image)
9
+ return text
10
+ except Exception as e:
11
+ return f"An error occurred: {str(e)}"
12
 
13
+ def main():
14
+ st.title("Image to Text Converter")
15
+ st.write("Upload an image and get the extracted text!")
16
 
17
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
18
 
19
+ if uploaded_file is not None:
20
+ image = Image.open(uploaded_file)
21
+ st.image(image, caption='Uploaded Image', use_column_width=True)
22
+ st.write("")
23
+ st.write("Extracting text...")
24
+ text = perform_ocr(image)
25
+ st.write("Extracted Text:")
26
+ st.write(text)
27
+
28
+ if __name__ == "__main__":
29
+ main()