OCR-img2txt / app.py
sundeveloper's picture
Update app.py
cf33acb verified
# streamlit_app.py
import streamlit as st
from app2 import OCRProcessor # Импортира OCRProcessor от модифицирания app.py
from PIL import Image
# Инициализация на OCR процесора
ocr_processor = OCRProcessor()
# Streamlit интерфейс
st.title("OCR Application with Streamlit")
# Избор на език за OCR
language = st.selectbox("Select Language:", ocr_processor.languages, index=0)
# Зона за качване на изображения
uploaded_images = st.file_uploader("Upload Images", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
if uploaded_images:
ocr_text = ""
st.write("### OCR Results:")
for uploaded_image in uploaded_images:
# Преобразуване на изображението за OCR
image = Image.open(uploaded_image)
ocr_result = ocr_processor.extract_text(uploaded_image, lang=language)
# Показване на OCR резултата за всяко изображение
st.image(image, caption=f"OCR for {uploaded_image.name}", use_column_width=True)
st.text_area(f"OCR Text for {uploaded_image.name}", value=ocr_result, height=200)
# Добавяне на текста в общия резултат
ocr_text += f"--- OCR result for {uploaded_image.name} ---\n{ocr_result}\n\n"
# Опции за копиране на текста и запазване като DOCX
if st.button("Copy OCR Text to Clipboard"):
st.write("Copy to clipboard functionality is currently not available in Streamlit directly.")
st.warning("You can manually copy the text from the text areas above.")
if st.button("Save as DOCX"):
docx_file_path = ocr_processor.save_as_docx(ocr_text)
if docx_file_path:
with open(docx_file_path, "rb") as f:
st.download_button(
label="Download DOCX",
data=f,
file_name="OCR_Result.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
else:
st.error("No OCR text to save.")