tejasexpress commited on
Commit
6530821
·
verified ·
1 Parent(s): ed5f605

Upload sentence-annotation.py

Browse files
Files changed (1) hide show
  1. sentence-annotation.py +71 -0
sentence-annotation.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import matplotlib.pyplot as plt
4
+ import matplotlib.patches as patches
5
+
6
+
7
+ input_folder = "words"
8
+ output_folder = "ocr-text-sentence"
9
+
10
+ json_files = [file for file in os.listdir(input_folder) if file.endswith('.json')]
11
+
12
+ for idx, json_file in enumerate(json_files):
13
+ print(f'Processing {json_file}')
14
+ input_file_path = os.path.join(input_folder, json_file)
15
+ output_file_path = os.path.join(output_folder, os.path.splitext(json_file)[0] + '.txt')
16
+ img_file_path = os.path.join("images", json_file.replace("_words.json", ".jpg"))
17
+
18
+ with open(input_file_path, 'r') as file:
19
+ data = json.load(file)
20
+
21
+ words_list = data.get("words", [])
22
+
23
+ width = data["image_rect"][2]
24
+ height = data["image_rect"][3]
25
+ print(len(words_list))
26
+ sorted_list = sorted(words_list, key=lambda x: (x["bbox"][1], x["bbox"][0]))
27
+ print(sum(len(s["text"]) for s in sorted_list))
28
+
29
+ merged_list = []
30
+ for i, item in enumerate(sorted_list):
31
+ if i > 0:
32
+ prev_item = sorted_list[i - 1]
33
+ # Check if the current item is in the same line as the previous one
34
+ if abs(item["bbox"][1] - prev_item["bbox"][1]) < 4 and (item["bbox"][0] - prev_item["bbox"][2])<10:
35
+ # Merge the boxes and update the text
36
+ merged_item = {
37
+ "bbox": [min(item["bbox"][0], merged_list[-1]["bbox"][0]),
38
+ min(item["bbox"][1], merged_list[-1]["bbox"][1]),
39
+ max(item["bbox"][2], merged_list[-1]["bbox"][2]),
40
+ max(item["bbox"][3], merged_list[-1]["bbox"][3])],
41
+ "text": merged_list[-1]["text"] + " " + item["text"]
42
+ }
43
+ # Update the previous item in the merged list
44
+ merged_list[-1] = merged_item
45
+ else:
46
+ # Add the current item to the merged list
47
+ merged_list.append(item)
48
+ else:
49
+ # Add the first item to the merged list
50
+ merged_list.append(item)
51
+
52
+ print(sum(len(s["text"]) for s in merged_list))
53
+ result_strings = []
54
+ for word in merged_list:
55
+ bbox_values = word["bbox"]
56
+
57
+ #calculate (x,y,w,h)
58
+ x = int((bbox_values[0])*100/width)
59
+ y = int((bbox_values[1])*100/height)
60
+ box_width = int((abs(bbox_values[2]-bbox_values[0]))*100/width)
61
+ box_height = int((abs(bbox_values[3]-bbox_values[1]))*100/height)
62
+ #convert everything to a string with appropriate spacing and text at the end
63
+ result_string = f"{x} {y} {box_width} {box_height} {word['text']}"
64
+ result_strings.append(result_string)
65
+
66
+ result_string = ' '.join(result_strings)
67
+
68
+ with open(output_file_path, 'w', encoding='utf-8') as file:
69
+ file.write(result_string)
70
+
71
+