Spaces:
Running
Running
import streamlit as st | |
from paddleocr import PaddleOCR, draw_ocr | |
from PIL import Image | |
import numpy as np | |
from langdetect import detect | |
from transformers import pipeline | |
import torch | |
# Initialize PaddleOCR for multilingual text recognition | |
ocr = PaddleOCR(use_angle_cls=True, lang='ar') # Using 'ar' to support Arabic scripts like Urdu | |
# Load summarization model | |
summarizer = pipeline("summarization") | |
def recognize_text(image_path): | |
image = Image.open(image_path) | |
img_array = np.array(image) | |
# OCR processing | |
ocr_results = ocr.ocr(img_array, cls=True) | |
# Extracting text from OCR results | |
detected_text = " ".join([line[1][0] for line in ocr_results[0]]) | |
# Language detection and summarization | |
language = detect(detected_text) | |
summary = summarizer(detected_text, max_length=50, min_length=25, do_sample=False)[0]['summary_text'] | |
return detected_text, language, summary | |
def display_ocr_results(image, ocr_results): | |
boxes = [line[0] for line in ocr_results[0]] | |
texts = [line[1][0] for line in ocr_results[0]] | |
scores = [line[1][1] for line in ocr_results[0]] | |
font_path = "/path/to/font.ttf" # Replace with a valid path to a font supporting Urdu/Arabic | |
return draw_ocr(np.array(image), boxes, texts, scores, font_path=font_path) | |
# Streamlit Interface | |
st.title("Multilingual OCR and Text Summarization App") | |
st.write("Upload an image or capture one to get OCR results and text summary") | |
# Image Upload or Capture | |
image_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"]) | |
if image_file is not None: | |
with open("uploaded_image.png", "wb") as f: | |
f.write(image_file.getbuffer()) | |
st.success("Image uploaded successfully!") | |
image = Image.open("uploaded_image.png") | |
st.image(image, caption="Uploaded Image", use_container_width=True) | |
# Perform OCR and display results | |
detected_text, language, summary = recognize_text("uploaded_image.png") | |
st.write("### Detected Text") | |
st.write(detected_text) | |
st.write("### Detected Language") | |
st.write(language) | |
st.write("### Text Summary") | |
st.write(summary) | |
# Display OCR visualization | |
visualized_image = display_ocr_results(image, ocr.ocr(np.array(image), cls=True)) | |
st.image(visualized_image, caption="OCR Results Visualization", use_container_width=True) | |