Spaces:
Running
Running
import logging | |
import pytesseract | |
from PIL import Image | |
import os | |
import streamlit as st | |
# Configure logging to display debug information | |
logging.basicConfig(level=logging.DEBUG) | |
# Function to extract text from an image | |
def extract_text_from_image(image): | |
try: | |
logging.info("Starting text extraction from image...") | |
# Verify the image is not corrupted | |
image.verify() # Verifies the image is not corrupted | |
logging.info("Image opened and verified successfully.") | |
# Resize the image to improve performance (optional) | |
image = image.resize((image.width // 2, image.height // 2)) # Resize image to 50% of the original size | |
# Extract text using pytesseract | |
text = pytesseract.image_to_string(image) | |
logging.info("Text extraction completed successfully.") | |
return text | |
except Exception as e: | |
logging.error(f"An error occurred while processing the image: {str(e)}") | |
return f"Error: {str(e)}" | |
# Streamlit web application | |
def main(): | |
st.title("Lab Report Analyzer") | |
st.markdown("Upload an image file to extract text from it.") | |
# File uploader widget | |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"]) | |
if uploaded_file is not None: | |
# Save the uploaded file temporarily | |
with open("temp_image.jpg", "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
# Open the image file | |
image = Image.open("temp_image.jpg") | |
# Extract text from the uploaded image | |
extracted_text = extract_text_from_image(image) | |
# Display extracted text | |
st.subheader("Extracted Text") | |
st.text(extracted_text) | |
# Optionally, delete the temporary file after processing | |
os.remove("temp_image.jpg") | |
if __name__ == "__main__": | |
main() | |