File size: 2,222 Bytes
cf33acb
 
3501e86
cf33acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3501e86
cf33acb
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
44
45
46
47
48
49
50
51
52
# 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.")