Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +4 -3
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import easyocr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
st.set_page_config(page_title="OCR Text Extractor", layout="centered")
|
| 8 |
+
st.title("🧠 Image to Text Detection (OCR)")
|
| 9 |
+
st.write("Upload an image to extract text more accurately!")
|
| 10 |
+
|
| 11 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 12 |
+
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
# Convert to grayscale
|
| 15 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 16 |
+
|
| 17 |
+
# Resize image to double the size (helps OCR sometimes)
|
| 18 |
+
scale_percent = 200
|
| 19 |
+
width = int(gray.shape[1] * scale_percent / 100)
|
| 20 |
+
height = int(gray.shape[0] * scale_percent / 100)
|
| 21 |
+
resized = cv2.resize(gray, (width, height), interpolation=cv2.INTER_LINEAR)
|
| 22 |
+
|
| 23 |
+
# Apply thresholding (binary black & white)
|
| 24 |
+
_, thresh = cv2.threshold(resized, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 25 |
+
|
| 26 |
+
return thresh
|
| 27 |
+
|
| 28 |
+
if uploaded_file:
|
| 29 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 30 |
+
st.image(image, caption="Original Image", use_container_width=True)
|
| 31 |
+
|
| 32 |
+
img_np = np.array(image)
|
| 33 |
+
processed_img = preprocess_image(img_np)
|
| 34 |
+
|
| 35 |
+
st.image(processed_img, caption="Preprocessed Image", use_container_width=True, channels="GRAY")
|
| 36 |
+
|
| 37 |
+
# OCR
|
| 38 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 39 |
+
results = reader.readtext(processed_img)
|
| 40 |
+
|
| 41 |
+
st.subheader("📃 Extracted Text:")
|
| 42 |
+
if results:
|
| 43 |
+
for bbox, text, prob in results:
|
| 44 |
+
st.write(f"- {text}")
|
| 45 |
+
else:
|
| 46 |
+
st.warning("❌ No text found.")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
easyocr
|
| 3 |
+
opencv-python-headless
|
| 4 |
+
pillow
|