Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,62 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from paddleocr import PaddleOCR, draw_ocr
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
from langdetect import detect
|
| 6 |
-
import
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
ocr = PaddleOCR(lang='
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
image = Image.open(
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# OCR and Display
|
| 19 |
-
st.write("Processing...")
|
| 20 |
-
result = ocr.ocr(np.array(image), cls=True)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
texts = [res[1][0] for res in result[0]]
|
| 25 |
-
scores = [res[1][1] for res in result[0]]
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
detected_text = " ".join(
|
| 29 |
-
st.write("Detected Text")
|
| 30 |
-
st.write(detected_text)
|
| 31 |
|
| 32 |
-
# Language detection
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Font setup
|
| 37 |
-
font_path = "/content/drive/MyDrive/Colab Notebooks/NOORIN59.TTF" # Update with an Urdu-compatible font if possible
|
| 38 |
-
if not os.path.exists(font_path):
|
| 39 |
-
st.write("Font file not found. Using default.")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from paddleocr import PaddleOCR, draw_ocr
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
from langdetect import detect
|
| 6 |
+
from transformers import pipeline
|
| 7 |
|
| 8 |
+
# Initialize PaddleOCR for multilingual text recognition
|
| 9 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en') # For language options, use 'ch' for Chinese, etc.
|
| 10 |
|
| 11 |
+
# Load summarization model
|
| 12 |
+
summarizer = pipeline("summarization")
|
| 13 |
+
|
| 14 |
+
def recognize_text(image_path):
|
| 15 |
+
image = Image.open(image_path)
|
| 16 |
+
img_array = np.array(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# OCR processing
|
| 19 |
+
ocr_results = ocr.ocr(img_array, cls=True)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Extracting text from OCR results
|
| 22 |
+
detected_text = " ".join([line[1][0] for line in ocr_results[0]])
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Language detection and summarization
|
| 25 |
+
language = detect(detected_text)
|
| 26 |
+
summary = summarizer(detected_text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
return detected_text, language, summary
|
| 29 |
+
|
| 30 |
+
def display_ocr_results(image, ocr_results):
|
| 31 |
+
boxes = [line[0] for line in ocr_results[0]]
|
| 32 |
+
texts = [line[1][0] for line in ocr_results[0]]
|
| 33 |
+
scores = [line[1][1] for line in ocr_results[0]]
|
| 34 |
+
return draw_ocr(np.array(image), boxes, texts, scores, font_path='path_to_font.ttf')
|
| 35 |
+
|
| 36 |
+
# Streamlit Interface
|
| 37 |
+
st.title("Multilingual OCR and Text Summarization App")
|
| 38 |
+
st.write("Upload an image or capture one to get OCR results and text summary")
|
| 39 |
+
|
| 40 |
+
# Image Upload or Capture
|
| 41 |
+
image_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
| 42 |
+
|
| 43 |
+
if image_file is not None:
|
| 44 |
+
with open("uploaded_image.png", "wb") as f:
|
| 45 |
+
f.write(image_file.getbuffer())
|
| 46 |
+
st.success("Image uploaded successfully!")
|
| 47 |
+
image = Image.open("uploaded_image.png")
|
| 48 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 49 |
+
|
| 50 |
+
# Perform OCR and display results
|
| 51 |
+
detected_text, language, summary = recognize_text("uploaded_image.png")
|
| 52 |
+
st.write("### Detected Text")
|
| 53 |
+
st.write(detected_text)
|
| 54 |
+
st.write("### Detected Language")
|
| 55 |
+
st.write(language)
|
| 56 |
+
st.write("### Text Summary")
|
| 57 |
+
st.write(summary)
|
| 58 |
|
| 59 |
+
# Display OCR visualization
|
| 60 |
+
visualized_image = display_ocr_results(image, ocr.ocr(np.array(image), cls=True))
|
| 61 |
+
st.image(visualized_image, caption="OCR Results Visualization", use_column_width=True)
|
| 62 |
|