Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -5,104 +5,184 @@ from PIL import Image
|
|
5 |
import os
|
6 |
import tempfile
|
7 |
|
8 |
-
# Função auxiliar para salvar imagem temporariamente e retornar o caminho
|
9 |
def save_temp_image(img):
|
10 |
temp_dir = tempfile.mkdtemp()
|
11 |
img_path = os.path.join(temp_dir, "input_image.png")
|
12 |
img.save(img_path)
|
13 |
return img_path, temp_dir
|
14 |
|
15 |
-
# Função para executar o OCR via linha de comando
|
16 |
def ocr_function_cli(img, lang_name):
|
17 |
img_path, temp_dir = save_temp_image(img)
|
18 |
-
|
19 |
-
# Substitua 'surya_ocr' pelo comando correto no seu sistema
|
20 |
command = f"surya_ocr {img_path} --langs {lang_name} --images --results_dir {temp_dir}"
|
21 |
-
|
22 |
-
|
23 |
-
subprocess.
|
24 |
-
|
25 |
-
|
26 |
-
result_img_path = os.path.join(temp_dir, "image_with_text.png")
|
27 |
-
result_text_path = os.path.join(temp_dir, "results.json")
|
28 |
-
|
29 |
-
# Carregar a imagem resultante
|
30 |
if os.path.exists(result_img_path):
|
31 |
result_img = Image.open(result_img_path)
|
32 |
else:
|
33 |
-
result_img = img
|
34 |
-
|
35 |
-
# Carregar o texto resultante
|
36 |
if os.path.exists(result_text_path):
|
37 |
-
with open(result_text_path, "r") as file:
|
38 |
result_text = json.load(file)
|
39 |
-
|
40 |
-
text_output = "\n".join([str(page) for page in result_text.values()])
|
41 |
else:
|
42 |
text_output = "No text detected"
|
43 |
-
|
44 |
-
# Limpeza
|
45 |
-
os.remove(img_path) # Remove a imagem temporária
|
46 |
-
# opcional: remover diretório temporário e seus conteúdos, se necessário
|
47 |
-
|
48 |
return result_img, text_output
|
49 |
|
50 |
-
# Função para detecção de linhas de texto via linha de comando
|
51 |
def text_line_detection_function_cli(img):
|
52 |
img_path, temp_dir = save_temp_image(img)
|
53 |
-
|
54 |
-
# Substitua 'surya_detect' pelo comando correto no seu sistema
|
55 |
command = f"surya_detect {img_path} --images --results_dir {temp_dir}"
|
56 |
-
|
57 |
-
|
58 |
-
subprocess.
|
59 |
-
|
60 |
-
|
61 |
-
result_img_path = os.path.join(temp_dir, "image_with_lines.png")
|
62 |
-
result_json_path = os.path.join(temp_dir, "results.json")
|
63 |
-
|
64 |
-
# Carregar a imagem resultante
|
65 |
if os.path.exists(result_img_path):
|
66 |
result_img = Image.open(result_img_path)
|
67 |
else:
|
68 |
-
result_img = img
|
69 |
-
|
70 |
-
# Carregar os resultados JSON
|
71 |
if os.path.exists(result_json_path):
|
72 |
-
with open(result_json_path, "r") as file:
|
73 |
result_json = json.load(file)
|
74 |
else:
|
75 |
result_json = {"error": "No detection results found"}
|
76 |
-
|
77 |
-
# Limpeza
|
78 |
-
os.remove(img_path) # Remove a imagem temporária
|
79 |
-
# opcional: remover diretório temporário e seus conteúdos, se necessário
|
80 |
-
|
81 |
return result_img, result_json
|
82 |
|
83 |
-
# Interface Gradio
|
84 |
with gr.Blocks() as app:
|
85 |
-
gr.Markdown("# Surya OCR
|
|
|
86 |
with gr.Tab("OCR"):
|
87 |
with gr.Column():
|
88 |
-
ocr_input_image = gr.Image(label="
|
89 |
-
ocr_language_selector = gr.Dropdown(
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
with gr.Column():
|
92 |
-
ocr_output_image = gr.Image(label="
|
93 |
-
ocr_text_output = gr.TextArea(label="
|
94 |
|
95 |
-
ocr_run_button.click(
|
|
|
|
|
96 |
|
97 |
-
with gr.Tab("
|
98 |
with gr.Column():
|
99 |
-
detection_input_image = gr.Image(label="
|
100 |
-
detection_run_button = gr.Button("
|
|
|
101 |
with gr.Column():
|
102 |
-
detection_output_image = gr.Image(label="
|
103 |
-
detection_json_output = gr.JSON(label="
|
104 |
|
105 |
-
detection_run_button.click(
|
|
|
|
|
106 |
|
107 |
if __name__ == "__main__":
|
108 |
-
app.launch()
|
|
|
5 |
import os
|
6 |
import tempfile
|
7 |
|
|
|
8 |
def save_temp_image(img):
|
9 |
temp_dir = tempfile.mkdtemp()
|
10 |
img_path = os.path.join(temp_dir, "input_image.png")
|
11 |
img.save(img_path)
|
12 |
return img_path, temp_dir
|
13 |
|
|
|
14 |
def ocr_function_cli(img, lang_name):
|
15 |
img_path, temp_dir = save_temp_image(img)
|
|
|
|
|
16 |
command = f"surya_ocr {img_path} --langs {lang_name} --images --results_dir {temp_dir}"
|
17 |
+
try:
|
18 |
+
subprocess.run(command, shell=True, check=True, encoding='utf-8')
|
19 |
+
except subprocess.CalledProcessError as e:
|
20 |
+
print(f"OCR command failed: {e.output}")
|
21 |
+
return img, "OCR failed"
|
22 |
+
result_img_path = os.path.join(temp_dir, "image_with_text.png")
|
23 |
+
result_text_path = os.path.join(temp_dir, "results.json")
|
|
|
|
|
24 |
if os.path.exists(result_img_path):
|
25 |
result_img = Image.open(result_img_path)
|
26 |
else:
|
27 |
+
result_img = img
|
|
|
|
|
28 |
if os.path.exists(result_text_path):
|
29 |
+
with open(result_text_path, "r", encoding='utf-8') as file:
|
30 |
result_text = json.load(file)
|
31 |
+
text_output = "\n".join([str(page) for page in result_text.values()])
|
|
|
32 |
else:
|
33 |
text_output = "No text detected"
|
34 |
+
os.remove(img_path)
|
|
|
|
|
|
|
|
|
35 |
return result_img, text_output
|
36 |
|
|
|
37 |
def text_line_detection_function_cli(img):
|
38 |
img_path, temp_dir = save_temp_image(img)
|
|
|
|
|
39 |
command = f"surya_detect {img_path} --images --results_dir {temp_dir}"
|
40 |
+
try:
|
41 |
+
subprocess.run(command, shell=True, check=True, encoding='utf-8')
|
42 |
+
except subprocess.CalledProcessError as e:
|
43 |
+
print(f"Text line detection command failed: {e.output}")
|
44 |
+
return img, {"error": "Detection failed"}
|
45 |
+
result_img_path = os.path.join(temp_dir, "image_with_lines.png")
|
46 |
+
result_json_path = os.path.join(temp_dir, "results.json")
|
|
|
|
|
47 |
if os.path.exists(result_img_path):
|
48 |
result_img = Image.open(result_img_path)
|
49 |
else:
|
50 |
+
result_img = img
|
|
|
|
|
51 |
if os.path.exists(result_json_path):
|
52 |
+
with open(result_json_path, "r", encoding='utf-8') as file:
|
53 |
result_json = json.load(file)
|
54 |
else:
|
55 |
result_json = {"error": "No detection results found"}
|
56 |
+
os.remove(img_path)
|
|
|
|
|
|
|
|
|
57 |
return result_img, result_json
|
58 |
|
|
|
59 |
with gr.Blocks() as app:
|
60 |
+
gr.Markdown("# Surya OCR and Text Line Detection via CLI")
|
61 |
+
|
62 |
with gr.Tab("OCR"):
|
63 |
with gr.Column():
|
64 |
+
ocr_input_image = gr.Image(label="Input Image for OCR", type="pil")
|
65 |
+
ocr_language_selector = gr.Dropdown(
|
66 |
+
label="Select Language for OCR",
|
67 |
+
choices=[
|
68 |
+
"Afrikaans",
|
69 |
+
"Amharic",
|
70 |
+
"Arabic",
|
71 |
+
"Assamese",
|
72 |
+
"Azerbaijani",
|
73 |
+
"Belarusian",
|
74 |
+
"Bulgarian",
|
75 |
+
"Bengali",
|
76 |
+
"Breton",
|
77 |
+
"Bosnian",
|
78 |
+
"Catalan",
|
79 |
+
"Czech",
|
80 |
+
"Welsh",
|
81 |
+
"Danish",
|
82 |
+
"German",
|
83 |
+
"Greek",
|
84 |
+
"English",
|
85 |
+
"Esperanto",
|
86 |
+
"Spanish",
|
87 |
+
"Estonian",
|
88 |
+
"Basque",
|
89 |
+
"Persian",
|
90 |
+
"Finnish",
|
91 |
+
"French",
|
92 |
+
"Western Frisian",
|
93 |
+
"Irish",
|
94 |
+
"Scottish Gaelic",
|
95 |
+
"Galician",
|
96 |
+
"Gujarati",
|
97 |
+
"Hausa",
|
98 |
+
"Hebrew",
|
99 |
+
"Hindi",
|
100 |
+
"Croatian",
|
101 |
+
"Hungarian",
|
102 |
+
"Armenian",
|
103 |
+
"Indonesian",
|
104 |
+
"Icelandic",
|
105 |
+
"Italian",
|
106 |
+
"Japanese",
|
107 |
+
"Javanese",
|
108 |
+
"Georgian",
|
109 |
+
"Kazakh",
|
110 |
+
"Khmer",
|
111 |
+
"Kannada",
|
112 |
+
"Korean",
|
113 |
+
"Kurdish",
|
114 |
+
"Kyrgyz",
|
115 |
+
"Latin",
|
116 |
+
"Lao",
|
117 |
+
"Lithuanian",
|
118 |
+
"Latvian",
|
119 |
+
"Malagasy",
|
120 |
+
"Macedonian",
|
121 |
+
"Malayalam",
|
122 |
+
"Mongolian",
|
123 |
+
"Marathi",
|
124 |
+
"Malay",
|
125 |
+
"Burmese",
|
126 |
+
"Nepali",
|
127 |
+
"Dutch",
|
128 |
+
"Norwegian",
|
129 |
+
"Oromo",
|
130 |
+
"Oriya",
|
131 |
+
"Punjabi",
|
132 |
+
"Polish",
|
133 |
+
"Pashto",
|
134 |
+
"Portuguese",
|
135 |
+
"Romanian",
|
136 |
+
"Russian",
|
137 |
+
"Sanskrit",
|
138 |
+
"Sindhi",
|
139 |
+
"Sinhala",
|
140 |
+
"Slovak",
|
141 |
+
"Slovenian",
|
142 |
+
"Somali",
|
143 |
+
"Albanian",
|
144 |
+
"Serbian",
|
145 |
+
"Sundanese",
|
146 |
+
"Swedish",
|
147 |
+
"Swahili",
|
148 |
+
"Tamil",
|
149 |
+
"Telugu",
|
150 |
+
"Thai",
|
151 |
+
"Tagalog",
|
152 |
+
"Turkish",
|
153 |
+
"Uyghur",
|
154 |
+
"Ukrainian",
|
155 |
+
"Urdu",
|
156 |
+
"Uzbek",
|
157 |
+
"Vietnamese",
|
158 |
+
"Xhosa",
|
159 |
+
"Yiddish",
|
160 |
+
"Chinese"
|
161 |
+
],
|
162 |
+
value="English"
|
163 |
+
)
|
164 |
+
ocr_run_button = gr.Button("Run OCR")
|
165 |
+
|
166 |
with gr.Column():
|
167 |
+
ocr_output_image = gr.Image(label="OCR Output Image", type="pil", interactive=False)
|
168 |
+
ocr_text_output = gr.TextArea(label="Recognized Text")
|
169 |
|
170 |
+
ocr_run_button.click(
|
171 |
+
fn=ocr_function_cli, inputs=[ocr_input_image, ocr_language_selector], outputs=[ocr_output_image, ocr_text_output]
|
172 |
+
)
|
173 |
|
174 |
+
with gr.Tab("Text Line Detection"):
|
175 |
with gr.Column():
|
176 |
+
detection_input_image = gr.Image(label="Input Image for Detection", type="pil")
|
177 |
+
detection_run_button = gr.Button("Run Text Line Detection")
|
178 |
+
|
179 |
with gr.Column():
|
180 |
+
detection_output_image = gr.Image(label="Detection Output Image", type="pil", interactive=False)
|
181 |
+
detection_json_output = gr.JSON(label="Detection JSON Output")
|
182 |
|
183 |
+
detection_run_button.click(
|
184 |
+
fn=text_line_detection_function_cli, inputs=detection_input_image, outputs=[detection_output_image, detection_json_output]
|
185 |
+
)
|
186 |
|
187 |
if __name__ == "__main__":
|
188 |
+
app.launch()
|