KZTech commited on
Commit
94d675c
·
verified ·
1 Parent(s): 9bed501

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -2,28 +2,36 @@ import streamlit as st
2
  from PIL import Image
3
  import pytesseract
4
  import io
 
5
 
6
- # Set Tesseract binary path for Hugging Face
7
- pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
 
 
 
 
8
 
9
  st.set_page_config(page_title="OCR – Image to Text", layout="centered")
10
  st.title("🖼️ OCR – Image to Text")
 
11
 
12
- st.write("Upload an image to extract text using Tesseract OCR.")
13
 
14
- uploaded_file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
15
  if uploaded_file:
16
  try:
17
  image = Image.open(io.BytesIO(uploaded_file.read()))
18
- st.image(image, caption="Uploaded Image", use_column_width=True)
19
 
20
  if st.button("Extract Text"):
21
- with st.spinner("Extracting text..."):
22
- extracted_text = pytesseract.image_to_string(image)
23
- st.success("Text extracted!")
24
- st.text_area("Extracted Text", extracted_text, height=300)
25
-
 
 
26
  except Exception as e:
27
  st.error(f"❌ Failed to process image: {e}")
28
  else:
29
- st.info("Please upload an image.")
 
 
2
  from PIL import Image
3
  import pytesseract
4
  import io
5
+ import os
6
 
7
+ # Try to set Tesseract path if available
8
+ TESSERACT_PATH = "/usr/bin/tesseract"
9
+ if os.path.exists(TESSERACT_PATH):
10
+ pytesseract.pytesseract.tesseract_cmd = TESSERACT_PATH
11
+ else:
12
+ pytesseract.pytesseract.tesseract_cmd = "tesseract" # fallback
13
 
14
  st.set_page_config(page_title="OCR – Image to Text", layout="centered")
15
  st.title("🖼️ OCR – Image to Text")
16
+ st.write("Upload a PNG or JPG image to extract text.")
17
 
18
+ uploaded_file = st.file_uploader("Choose an image file", type=["png", "jpg", "jpeg"])
19
 
 
20
  if uploaded_file:
21
  try:
22
  image = Image.open(io.BytesIO(uploaded_file.read()))
23
+ st.image(image, caption="Preview", use_column_width=True)
24
 
25
  if st.button("Extract Text"):
26
+ with st.spinner("Running OCR..."):
27
+ try:
28
+ text = pytesseract.image_to_string(image)
29
+ st.subheader("Extracted Text")
30
+ st.text_area("", text, height=300)
31
+ except pytesseract.TesseractNotFoundError:
32
+ st.error("⚠️ Tesseract is not installed on the server. Please check your `apt.txt` or switch SDK to Docker.")
33
  except Exception as e:
34
  st.error(f"❌ Failed to process image: {e}")
35
  else:
36
+ st.info("Please upload an image to begin.")
37
+