Prasada commited on
Commit
1939ca9
1 Parent(s): 61a2cab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -4,6 +4,7 @@ from PIL import Image, ImageDraw, ImageFont
4
  import json
5
  from paddleocr import PaddleOCR
6
  import gradio as gr
 
7
 
8
  # Initialize PaddleOCR
9
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
@@ -27,10 +28,10 @@ def draw_boxes_on_image(image, data):
27
  bounding_box, (text, confidence) = item
28
 
29
  # Ensure bounding_box is a list of lists
30
- if isinstance(bounding_box[0], list):
31
- box = np.array(bounding_box).astype(int)
32
- else:
33
- box = np.array([bounding_box]).astype(int)
34
 
35
  # Draw the bounding box
36
  draw.line([tuple(box[0]), tuple(box[1])], fill="green", width=2)
@@ -44,26 +45,29 @@ def draw_boxes_on_image(image, data):
44
 
45
  return pil_image
46
 
47
- # Function to save OCR results to JSON
48
- def save_results_to_json(ocr_results):
49
- results = []
50
-
51
- for line in ocr_results:
52
- for word_info in line:
53
- bounding_box = word_info[0]
54
- text, confidence = word_info[1]
55
-
56
- # Ensure bounding_box is a list of lists
57
- if not isinstance(bounding_box[0], list):
58
- bounding_box = [bounding_box]
59
-
60
- results.append({
61
- "bounding_box": [list(map(float, coord)) for coord in bounding_box],
62
- "text": text,
63
- "confidence": confidence
64
- })
65
-
66
- return results
 
 
 
67
 
68
  # Function to identify 'field', 'value' pairs
69
  def identify_field_value_pairs(ocr_results, fields):
@@ -86,16 +90,15 @@ def process_image(image):
86
  processed_image = draw_boxes_on_image(image, ocr_results[0])
87
 
88
  # Save OCR results to JSON
89
- results_json = save_results_to_json(ocr_results[0])
90
  json_path = "ocr_results.json"
91
- with open(json_path, 'w') as json_file:
92
- json.dump(results_json, json_file, indent=4)
93
 
94
  # Identify field-value pairs
95
  fields = ["Scheme Name", "Folio Number", "Number of Units", "PAN", "Signature", "Tax Status",
96
  "Mobile Number", "Email", "Address", "Bank Account Details"]
97
  field_value_pairs = identify_field_value_pairs(ocr_results[0], fields)
98
  field_value_json_path = "field_value_pairs.json"
 
99
  with open(field_value_json_path, 'w') as json_file:
100
  json.dump(field_value_pairs, json_file, indent=4)
101
 
@@ -107,8 +110,8 @@ interface = gr.Interface(
107
  inputs="image",
108
  outputs=[
109
  "image",
110
- gr.File(label="OCR Results JSON"),
111
- gr.File(label="Field-Value Pairs JSON")
112
  ],
113
  title="OCR Web Application",
114
  description="Upload an image and get OCR results with bounding boxes and two JSON outputs."
 
4
  import json
5
  from paddleocr import PaddleOCR
6
  import gradio as gr
7
+ import os
8
 
9
  # Initialize PaddleOCR
10
  ocr = PaddleOCR(use_angle_cls=True, lang='en')
 
28
  bounding_box, (text, confidence) = item
29
 
30
  # Ensure bounding_box is a list of lists
31
+ if not isinstance(bounding_box[0], list):
32
+ bounding_box = [bounding_box]
33
+
34
+ box = np.array(bounding_box).astype(int)
35
 
36
  # Draw the bounding box
37
  draw.line([tuple(box[0]), tuple(box[1])], fill="green", width=2)
 
45
 
46
  return pil_image
47
 
48
+ # Function to convert OCR results to JSON
49
+ def convert_to_json(results, output_file):
50
+ """
51
+ Converts the given results into a JSON file.
52
+
53
+ Args:
54
+ results: The list of results containing bounding box coordinates, text, and confidence.
55
+ output_file: The name of the output JSON file.
56
+ """
57
+ json_data = []
58
+ for result in results:
59
+ bounding_box = result[0]
60
+ text = result[1][0]
61
+ confidence = result[1][1]
62
+
63
+ json_data.append({
64
+ "bounding_box": [list(map(float, coord)) for coord in bounding_box],
65
+ "text": text,
66
+ "confidence": confidence
67
+ })
68
+
69
+ with open(output_file, "w") as f:
70
+ json.dump(json_data, f, indent=4)
71
 
72
  # Function to identify 'field', 'value' pairs
73
  def identify_field_value_pairs(ocr_results, fields):
 
90
  processed_image = draw_boxes_on_image(image, ocr_results[0])
91
 
92
  # Save OCR results to JSON
 
93
  json_path = "ocr_results.json"
94
+ convert_to_json(ocr_results[0], json_path)
 
95
 
96
  # Identify field-value pairs
97
  fields = ["Scheme Name", "Folio Number", "Number of Units", "PAN", "Signature", "Tax Status",
98
  "Mobile Number", "Email", "Address", "Bank Account Details"]
99
  field_value_pairs = identify_field_value_pairs(ocr_results[0], fields)
100
  field_value_json_path = "field_value_pairs.json"
101
+
102
  with open(field_value_json_path, 'w') as json_file:
103
  json.dump(field_value_pairs, json_file, indent=4)
104
 
 
110
  inputs="image",
111
  outputs=[
112
  "image",
113
+ gr.File(label="Download OCR Results JSON"),
114
+ gr.File(label="Download Field-Value Pairs JSON")
115
  ],
116
  title="OCR Web Application",
117
  description="Upload an image and get OCR results with bounding boxes and two JSON outputs."