KZTech commited on
Commit
f0aa330
·
verified ·
1 Parent(s): 832f690

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -1,31 +1,30 @@
1
  import streamlit as st
2
  from PIL import Image
3
  import pytesseract
 
4
  import io
5
 
 
 
 
6
  st.set_page_config(page_title="OCR – Image to Text", layout="centered")
7
  st.title("🖼️ OCR – Image to Text")
8
-
9
  st.write("Upload a PNG, JPG or JPEG image to extract text.")
10
 
11
  uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
 
12
  if uploaded_file:
13
  try:
14
- # Read and open the image
15
- img_data = uploaded_file.read()
16
- image = Image.open(io.BytesIO(img_data))
17
-
18
- # Show preview
19
  st.image(image, caption="Preview", use_column_width=True)
20
 
21
- # Extraction button
22
  if st.button("Extract Text"):
23
  with st.spinner("Running OCR..."):
24
- text = pytesseract.image_to_string(image)
25
  st.subheader("Extracted Text")
26
- st.text_area("", text, height=300)
27
-
28
  except Exception as e:
29
  st.error(f"Failed to process image: {e}")
30
- else:
31
- st.info("Please upload an image to get started.")
 
1
  import streamlit as st
2
  from PIL import Image
3
  import pytesseract
4
+ import os
5
  import io
6
 
7
+ # Explicitly set tesseract path (only needed in hosted environments)
8
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
9
+
10
  st.set_page_config(page_title="OCR – Image to Text", layout="centered")
11
  st.title("🖼️ OCR – Image to Text")
 
12
  st.write("Upload a PNG, JPG or JPEG image to extract text.")
13
 
14
  uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
15
+
16
  if uploaded_file:
17
  try:
18
+ # Read and display image
19
+ image = Image.open(io.BytesIO(uploaded_file.read()))
 
 
 
20
  st.image(image, caption="Preview", use_column_width=True)
21
 
22
+ # Extract text on button click
23
  if st.button("Extract Text"):
24
  with st.spinner("Running OCR..."):
25
+ extracted_text = pytesseract.image_to_string(image)
26
  st.subheader("Extracted Text")
27
+ st.text_area(label="", value=extracted_text, height=300)
 
28
  except Exception as e:
29
  st.error(f"Failed to process image: {e}")
30
+