artificialguybr commited on
Commit
714f9cb
·
verified ·
1 Parent(s): b44d45c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -4,58 +4,78 @@ import subprocess
4
  from PIL import Image
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
 
 
4
  from PIL import Image
5
  import os
6
  import tempfile
7
+ import logging
8
+
9
+ # Configuração básica de logging
10
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
11
 
12
  def save_temp_image(img):
13
  temp_dir = tempfile.mkdtemp()
14
  img_path = os.path.join(temp_dir, "input_image.png")
15
  img.save(img_path)
16
+ logging.info(f"Imagem salva em {img_path}")
17
  return img_path, temp_dir
18
 
19
+ def run_command(command):
20
+ try:
21
+ result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT, encoding='utf-8')
22
+ logging.info("Command Output: " + result)
23
+ return result
24
+ except subprocess.CalledProcessError as e:
25
+ logging.error(f"Command failed with error: {e.output}")
26
+ return None
27
+
28
  def ocr_function_cli(img, lang_name):
29
  img_path, temp_dir = save_temp_image(img)
30
  command = f"surya_ocr {img_path} --langs {lang_name} --images --results_dir {temp_dir}"
31
+ if run_command(command) is None:
 
 
 
32
  return img, "OCR failed"
33
+
34
  result_img_path = os.path.join(temp_dir, "image_with_text.png")
35
  result_text_path = os.path.join(temp_dir, "results.json")
36
+
37
  if os.path.exists(result_img_path):
38
  result_img = Image.open(result_img_path)
39
  else:
40
  result_img = img
41
+
42
  if os.path.exists(result_text_path):
43
  with open(result_text_path, "r", encoding='utf-8') as file:
44
  result_text = json.load(file)
45
  text_output = "\n".join([str(page) for page in result_text.values()])
46
  else:
47
  text_output = "No text detected"
48
+
49
+ # Limpeza movida para depois da leitura dos resultados
50
  os.remove(img_path)
51
+ logging.info(f"Limpeza concluída para {img_path}")
52
  return result_img, text_output
53
 
54
  def text_line_detection_function_cli(img):
55
  img_path, temp_dir = save_temp_image(img)
56
  command = f"surya_detect {img_path} --images --results_dir {temp_dir}"
57
+ if run_command(command) is None:
 
 
 
58
  return img, {"error": "Detection failed"}
59
+
60
  result_img_path = os.path.join(temp_dir, "image_with_lines.png")
61
  result_json_path = os.path.join(temp_dir, "results.json")
62
+
63
  if os.path.exists(result_img_path):
64
  result_img = Image.open(result_img_path)
65
  else:
66
  result_img = img
67
+
68
  if os.path.exists(result_json_path):
69
  with open(result_json_path, "r", encoding='utf-8') as file:
70
  result_json = json.load(file)
71
  else:
72
  result_json = {"error": "No detection results found"}
73
+
74
+ # Limpeza movida para depois da leitura dos resultados
75
  os.remove(img_path)
76
+ logging.info(f"Limpeza concluída para {img_path}")
77
  return result_img, result_json
78
+
79
  with gr.Blocks() as app:
80
  gr.Markdown("# Surya OCR and Text Line Detection via CLI")
81