Hayk Arutyunyan commited on
Commit ·
5487729
1
Parent(s): 896c179
Fix gradio import
Browse files
app.py
CHANGED
|
@@ -1,87 +1,41 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
-
import cv2
|
| 4 |
-
import pandas as pd
|
| 5 |
-
from pathlib import Path
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
img = cv2.cvtColor(np.array(page), cv2.COLOR_RGB2BGR)
|
| 44 |
-
|
| 45 |
-
with st.spinner("OCR..."):
|
| 46 |
-
title_text, sensors = extract_text(img, color_ranges)
|
| 47 |
-
|
| 48 |
-
st.write("**Титул:**", title_text)
|
| 49 |
-
|
| 50 |
-
if sensors:
|
| 51 |
-
df = pd.DataFrame(sensors)
|
| 52 |
-
st.dataframe(df)
|
| 53 |
-
results_all.append(df)
|
| 54 |
-
else:
|
| 55 |
-
st.info("Сенсоры не найдены.")
|
| 56 |
-
|
| 57 |
-
if results_all:
|
| 58 |
-
df_total = pd.concat(results_all, ignore_index=True)
|
| 59 |
-
st.download_button("Скачать CSV", df_total.to_csv(index=False).encode(), "result.csv", "text/csv")
|
| 60 |
-
|
| 61 |
-
st.stop()
|
| 62 |
-
|
| 63 |
-
# ---- PNG/JPG ----
|
| 64 |
-
else:
|
| 65 |
-
file_bytes = np.frombuffer(uploaded.read(), np.uint8)
|
| 66 |
-
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
| 67 |
-
|
| 68 |
-
st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), use_column_width=True)
|
| 69 |
-
|
| 70 |
-
with st.spinner("OCR..."):
|
| 71 |
-
title_text, sensors = extract_text(img, color_ranges)
|
| 72 |
-
|
| 73 |
-
st.write("### Титул")
|
| 74 |
-
st.write(title_text)
|
| 75 |
-
|
| 76 |
-
st.write("### Сенсоры")
|
| 77 |
-
if sensors:
|
| 78 |
-
df = pd.DataFrame(sensors)
|
| 79 |
-
st.dataframe(df)
|
| 80 |
-
st.download_button(
|
| 81 |
-
"Скачать CSV",
|
| 82 |
-
df.to_csv(index=False).encode(),
|
| 83 |
-
"sensors.csv",
|
| 84 |
-
"text/csv"
|
| 85 |
-
)
|
| 86 |
-
else:
|
| 87 |
-
st.info("Сенсоры не найдены.")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Инициализируем OCR один раз
|
| 7 |
+
ocr = PaddleOCR(
|
| 8 |
+
use_angle_cls=True,
|
| 9 |
+
lang='en' # если нужно rus — скажи
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def run_ocr(image):
|
| 13 |
+
if image is None:
|
| 14 |
+
return "No image uploaded."
|
| 15 |
+
|
| 16 |
+
# Конвертируем PIL → numpy
|
| 17 |
+
img = np.array(image)
|
| 18 |
+
|
| 19 |
+
# Запуск OCR
|
| 20 |
+
result = ocr.ocr(img)
|
| 21 |
+
|
| 22 |
+
# Формируем вывод текстом
|
| 23 |
+
lines = []
|
| 24 |
+
for block in result:
|
| 25 |
+
for line in block:
|
| 26 |
+
text = line[1][0]
|
| 27 |
+
conf = line[1][1]
|
| 28 |
+
lines.append(f"{text} (conf: {conf:.2f})")
|
| 29 |
+
|
| 30 |
+
return "\n".join(lines)
|
| 31 |
+
|
| 32 |
+
demo = gr.Interface(
|
| 33 |
+
fn=run_ocr,
|
| 34 |
+
inputs=gr.Image(type="pil"),
|
| 35 |
+
outputs=gr.Textbox(label="Recognized Text"),
|
| 36 |
+
title="MNEMO OCR Demo",
|
| 37 |
+
description="Upload an image and extract text using PaddleOCR.",
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|