Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,10 @@ 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='
|
10 |
|
11 |
# Load summarization model
|
12 |
summarizer = pipeline("summarization")
|
@@ -14,24 +15,21 @@ summarizer = pipeline("summarization")
|
|
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 |
-
|
|
|
35 |
|
36 |
# Streamlit Interface
|
37 |
st.title("Multilingual OCR and Text Summarization App")
|
@@ -39,13 +37,12 @@ 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",
|
49 |
|
50 |
# Perform OCR and display results
|
51 |
detected_text, language, summary = recognize_text("uploaded_image.png")
|
@@ -58,5 +55,4 @@ if image_file is not None:
|
|
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",
|
62 |
-
|
|
|
4 |
import numpy as np
|
5 |
from langdetect import detect
|
6 |
from transformers import pipeline
|
7 |
+
import torch
|
8 |
|
9 |
# Initialize PaddleOCR for multilingual text recognition
|
10 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='ar') # Using 'ar' to support Arabic scripts like Urdu
|
11 |
|
12 |
# Load summarization model
|
13 |
summarizer = pipeline("summarization")
|
|
|
15 |
def recognize_text(image_path):
|
16 |
image = Image.open(image_path)
|
17 |
img_array = np.array(image)
|
|
|
18 |
# OCR processing
|
19 |
ocr_results = ocr.ocr(img_array, cls=True)
|
|
|
20 |
# Extracting text from OCR results
|
21 |
detected_text = " ".join([line[1][0] for line in ocr_results[0]])
|
|
|
22 |
# Language detection and summarization
|
23 |
language = detect(detected_text)
|
24 |
summary = summarizer(detected_text, max_length=50, min_length=25, do_sample=False)[0]['summary_text']
|
|
|
25 |
return detected_text, language, summary
|
26 |
|
27 |
def display_ocr_results(image, ocr_results):
|
28 |
boxes = [line[0] for line in ocr_results[0]]
|
29 |
texts = [line[1][0] for line in ocr_results[0]]
|
30 |
scores = [line[1][1] for line in ocr_results[0]]
|
31 |
+
font_path = "/path/to/font.ttf" # Replace with a valid path to a font supporting Urdu/Arabic
|
32 |
+
return draw_ocr(np.array(image), boxes, texts, scores, font_path=font_path)
|
33 |
|
34 |
# Streamlit Interface
|
35 |
st.title("Multilingual OCR and Text Summarization App")
|
|
|
37 |
|
38 |
# Image Upload or Capture
|
39 |
image_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
|
|
40 |
if image_file is not None:
|
41 |
with open("uploaded_image.png", "wb") as f:
|
42 |
f.write(image_file.getbuffer())
|
43 |
st.success("Image uploaded successfully!")
|
44 |
image = Image.open("uploaded_image.png")
|
45 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
46 |
|
47 |
# Perform OCR and display results
|
48 |
detected_text, language, summary = recognize_text("uploaded_image.png")
|
|
|
55 |
|
56 |
# Display OCR visualization
|
57 |
visualized_image = display_ocr_results(image, ocr.ocr(np.array(image), cls=True))
|
58 |
+
st.image(visualized_image, caption="OCR Results Visualization", use_container_width=True)
|
|