File size: 7,139 Bytes
b9dea2c
ee94965
dbb32ab
ee94965
dbb32ab
 
714f9cb
 
 
 
b9dea2c
dbb32ab
 
 
 
714f9cb
dbb32ab
b9dea2c
714f9cb
2c60ec4
714f9cb
 
 
 
 
 
 
 
2c60ec4
dbb32ab
 
 
714f9cb
b44d45c
714f9cb
b44d45c
 
714f9cb
dbb32ab
 
82673a2
b44d45c
714f9cb
dbb32ab
b44d45c
dbb32ab
b44d45c
9aaed47
dbb32ab
714f9cb
 
b44d45c
714f9cb
dbb32ab
b9dea2c
dbb32ab
 
 
714f9cb
b44d45c
714f9cb
b44d45c
 
714f9cb
dbb32ab
 
 
b44d45c
714f9cb
dbb32ab
b44d45c
dbb32ab
 
 
714f9cb
 
b44d45c
714f9cb
dbb32ab
714f9cb
b9dea2c
b44d45c
 
ee94965
b3bb9ad
b44d45c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3bb9ad
b44d45c
 
b3bb9ad
b44d45c
 
 
b3bb9ad
b44d45c
b3bb9ad
b44d45c
 
 
b3bb9ad
b44d45c
 
b3bb9ad
b44d45c
 
 
b9dea2c
 
b44d45c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import gradio as gr
import json
import subprocess
from PIL import Image
import os
import tempfile
import logging

# Configuração básica de logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def save_temp_image(img):
    temp_dir = tempfile.mkdtemp()
    img_path = os.path.join(temp_dir, "input_image.png")
    img.save(img_path)
    logging.info(f"Imagem salva em {img_path}")
    return img_path, temp_dir

def run_command(command):
    logging.info(f"Executing command: {command}")  # Adiciona o log do comando
    try:
        result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, encoding='utf-8')
        logging.info("Command Output: " + result)
        return result
    except subprocess.CalledProcessError as e:
        logging.error(f"Command failed with error: {e.output}")
        return None


def ocr_function_cli(img, lang_name):
    img_path, temp_dir = save_temp_image(img)
    command = f"surya_ocr {img_path} --langs {lang_name} --images --results_dir {temp_dir}"
    if run_command(command) is None:
        return img, "OCR failed"

    result_img_path = os.path.join(temp_dir, "image_with_text.png")
    result_text_path = os.path.join(temp_dir, "results.json")

    if os.path.exists(result_img_path):
        result_img = Image.open(result_img_path)
    else:
        result_img = img

    if os.path.exists(result_text_path):
        with open(result_text_path, "r", encoding='utf-8') as file:
            result_text = json.load(file)
        text_output = "\n".join([str(page) for page in result_text.values()])
    else:
        text_output = "No text detected"

    # Limpeza movida para depois da leitura dos resultados
    os.remove(img_path)
    logging.info(f"Limpeza concluída para {img_path}")
    return result_img, text_output

def text_line_detection_function_cli(img):
    img_path, temp_dir = save_temp_image(img)
    command = f"surya_detect {img_path} --images --results_dir {temp_dir}"
    if run_command(command) is None:
        return img, {"error": "Detection failed"}

    result_img_path = os.path.join(temp_dir, "image_with_lines.png")
    result_json_path = os.path.join(temp_dir, "results.json")

    if os.path.exists(result_img_path):
        result_img = Image.open(result_img_path)
    else:
        result_img = img

    if os.path.exists(result_json_path):
        with open(result_json_path, "r", encoding='utf-8') as file:
            result_json = json.load(file)
    else:
        result_json = {"error": "No detection results found"}

    # Limpeza movida para depois da leitura dos resultados
    os.remove(img_path)
    logging.info(f"Limpeza concluída para {img_path}")
    return result_img, result_json
    
with gr.Blocks() as app:
    gr.Markdown("# Surya OCR and Text Line Detection via CLI")

    with gr.Tab("OCR"):
        with gr.Column():
            ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
            ocr_language_selector = gr.Dropdown(
                label="Select Language for OCR",
                choices=[
                    "Afrikaans",
                    "Amharic",
                    "Arabic",
                    "Assamese",
                    "Azerbaijani",
                    "Belarusian",
                    "Bulgarian",
                    "Bengali",
                    "Breton",
                    "Bosnian",
                    "Catalan",
                    "Czech",
                    "Welsh",
                    "Danish",
                    "German",
                    "Greek",
                    "English",
                    "Esperanto",
                    "Spanish",
                    "Estonian",
                    "Basque",
                    "Persian",
                    "Finnish",
                    "French",
                    "Western Frisian",
                    "Irish",
                    "Scottish Gaelic",
                    "Galician",
                    "Gujarati",
                    "Hausa",
                    "Hebrew",
                    "Hindi",
                    "Croatian",
                    "Hungarian",
                    "Armenian",
                    "Indonesian",
                    "Icelandic",
                    "Italian",
                    "Japanese",
                    "Javanese",
                    "Georgian",
                    "Kazakh",
                    "Khmer",
                    "Kannada",
                    "Korean",
                    "Kurdish",
                    "Kyrgyz",
                    "Latin",
                    "Lao",
                    "Lithuanian",
                    "Latvian",
                    "Malagasy",
                    "Macedonian",
                    "Malayalam",
                    "Mongolian",
                    "Marathi",
                    "Malay",
                    "Burmese",
                    "Nepali",
                    "Dutch",
                    "Norwegian",
                    "Oromo",
                    "Oriya",
                    "Punjabi",
                    "Polish",
                    "Pashto",
                    "Portuguese",
                    "Romanian",
                    "Russian",
                    "Sanskrit",
                    "Sindhi",
                    "Sinhala",
                    "Slovak",
                    "Slovenian",
                    "Somali",
                    "Albanian",
                    "Serbian",
                    "Sundanese",
                    "Swedish",
                    "Swahili",
                    "Tamil",
                    "Telugu",
                    "Thai",
                    "Tagalog",
                    "Turkish",
                    "Uyghur",
                    "Ukrainian",
                    "Urdu",
                    "Uzbek",
                    "Vietnamese",
                    "Xhosa",
                    "Yiddish",
                    "Chinese"
                ],
                value="English"
            )
            ocr_run_button = gr.Button("Run OCR")

        with gr.Column():
            ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
            ocr_text_output = gr.TextArea(label="Recognized Text")

        ocr_run_button.click(
            fn=ocr_function_cli, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_text_output]
        )

    with gr.Tab("Text Line Detection"):
        with gr.Column():
            detection_input_image = gr.Image(label="Input Image for Detection", type="pil")
            detection_run_button = gr.Button("Run Text Line Detection")

        with gr.Column():
            detection_output_image = gr.Image(label="Detection Output Image", type="pil", interactive=False)
            detection_json_output = gr.JSON(label="Detection JSON Output")

        detection_run_button.click(
            fn=text_line_detection_function_cli, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output]
        )

if __name__ == "__main__":
    app.launch()