rahul7star commited on
Commit
e9b8d71
·
verified ·
1 Parent(s): 98c9504

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  from io import BytesIO
5
  import numpy as np
6
  import json
 
7
  import easyocr
8
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
9
 
@@ -32,7 +33,7 @@ def detect_text_trocr_json(image_file, image_url):
32
  words_json = []
33
 
34
  for bbox, _, conf in results:
35
- # Convert coordinates to float
36
  x_coords = [float(point[0]) for point in bbox]
37
  y_coords = [float(point[1]) for point in bbox]
38
  x_min, y_min = min(x_coords), min(y_coords)
@@ -52,7 +53,7 @@ def detect_text_trocr_json(image_file, image_url):
52
  "confidence": float(conf)
53
  })
54
 
55
- # For simplicity, treat words as paragraphs
56
  paragraphs_json = words_json.copy()
57
 
58
  output_json = {
@@ -60,8 +61,14 @@ def detect_text_trocr_json(image_file, image_url):
60
  "paragraphs": paragraphs_json
61
  }
62
 
63
- json_str = json.dumps(output_json, indent=2) # now serializable
64
- return image, json_str, json_str
 
 
 
 
 
 
65
 
66
  iface = gr.Interface(
67
  fn=detect_text_trocr_json,
 
4
  from io import BytesIO
5
  import numpy as np
6
  import json
7
+ import tempfile
8
  import easyocr
9
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
10
 
 
33
  words_json = []
34
 
35
  for bbox, _, conf in results:
36
+ # Convert coordinates to float for JSON serialization
37
  x_coords = [float(point[0]) for point in bbox]
38
  y_coords = [float(point[1]) for point in bbox]
39
  x_min, y_min = min(x_coords), min(y_coords)
 
53
  "confidence": float(conf)
54
  })
55
 
56
+ # Treat words as paragraphs for simplicity
57
  paragraphs_json = words_json.copy()
58
 
59
  output_json = {
 
61
  "paragraphs": paragraphs_json
62
  }
63
 
64
+ json_str = json.dumps(output_json, indent=2)
65
+
66
+ # Save JSON to a temporary file for Gradio download
67
+ tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".json", mode='w')
68
+ tmp_file.write(json_str)
69
+ tmp_file.close()
70
+
71
+ return image, json_str, tmp_file.name
72
 
73
  iface = gr.Interface(
74
  fn=detect_text_trocr_json,