Spaces:
Running
Running
| # app.py | |
| import streamlit as st | |
| from PIL import Image, ImageEnhance, ImageFilter | |
| import easyocr | |
| import numpy as np | |
| from io import BytesIO | |
| import matplotlib.pyplot as plt | |
| # Set up the OCR reader for Nepali | |
| reader = easyocr.Reader(['ne']) | |
| # Function to enhance and apply scan-like effects to an image | |
| def scan_effect(img): | |
| # Convert the image to grayscale | |
| gray_img = img.convert("L") | |
| # Apply contrast enhancement | |
| enhancer = ImageEnhance.Contrast(gray_img) | |
| contrast_img = enhancer.enhance(2.0) | |
| # Apply sharpness | |
| sharp_img = contrast_img.filter(ImageFilter.SHARPEN) | |
| return sharp_img | |
| # Streamlit app layout | |
| st.title("Nepali OCR Detector") | |
| # File upload | |
| uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
| if uploaded_file is not None: | |
| # Display the uploaded image | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Apply scan effect to the image | |
| scanned_image = scan_effect(image) | |
| # Display scanned image | |
| st.image(scanned_image, caption="Scanned Image Effect", use_column_width=True) | |
| # Convert scanned image to bytes for OCR processing | |
| image_bytes = BytesIO() | |
| scanned_image.save(image_bytes, format="PNG") | |
| image_bytes = image_bytes.getvalue() | |
| # Run OCR on the scanned image | |
| output = reader.readtext(np.array(scanned_image)) | |
| # Display OCR results | |
| st.write("### Detected Text:") | |
| for i, (bbox, text, confidence) in enumerate(output): | |
| st.write(f"{i+1}. {text} (Confidence: {confidence:.2f})") | |
| # Draw bounding boxes on the text | |
| fig, ax = plt.subplots() | |
| ax.imshow(scanned_image) | |
| for (bbox, text, _) in output: | |
| rect_x = [p[0] for p in bbox] | |
| rect_y = [p[1] for p in bbox] | |
| ax.plot(rect_x + [rect_x[0]], rect_y + [rect_y[0]], "r-", linewidth=2) | |
| st.pyplot(fig) | |
| else: | |
| st.info("Please upload an image to start the OCR process.") | |