Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from paddleocr import PaddleOCR, draw_ocr
|
3 |
+
from PIL import Image, ImageFont
|
4 |
+
import numpy as np
|
5 |
+
from langdetect import detect
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Set up OCR for Urdu
|
9 |
+
ocr = PaddleOCR(lang='ar') # Use 'ar' for Arabic-based scripts like Urdu
|
10 |
+
|
11 |
+
# Upload or capture image
|
12 |
+
st.title("OCR Application for Urdu Text")
|
13 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
14 |
+
if uploaded_file is not None:
|
15 |
+
image = Image.open(uploaded_file)
|
16 |
+
st.image(image, caption='Uploaded Image', use_container_width=True)
|
17 |
+
|
18 |
+
# OCR and Display
|
19 |
+
st.write("Processing...")
|
20 |
+
result = ocr.ocr(np.array(image), cls=True)
|
21 |
+
|
22 |
+
# Process OCR results
|
23 |
+
boxes = [res[0] for res in result[0]]
|
24 |
+
texts = [res[1][0] for res in result[0]]
|
25 |
+
scores = [res[1][1] for res in result[0]]
|
26 |
+
|
27 |
+
# Display OCR text results
|
28 |
+
detected_text = " ".join(texts)
|
29 |
+
st.write("Detected Text")
|
30 |
+
st.write(detected_text)
|
31 |
+
|
32 |
+
# Language detection
|
33 |
+
detected_lang = detect(detected_text)
|
34 |
+
st.write("Detected Language:", detected_lang)
|
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 |
+
# Draw OCR results on image
|
42 |
+
st.write("OCR Visualization")
|
43 |
+
visualized_image = draw_ocr(np.array(image), boxes, texts, scores, font_path=font_path)
|
44 |
+
st.image(visualized_image, caption='OCR Result Visualization', use_container_width=True)
|
45 |
+
|
46 |
+
|