Essa20001 commited on
Commit
0373fcc
1 Parent(s): c7d6bcb

Upload yolo_text_extraction.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. yolo_text_extraction.py +171 -0
yolo_text_extraction.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ from PIL import Image,ImageDraw
3
+ import numpy as np
4
+ from PIL import ImageFilter
5
+ from dotenv import load_dotenv
6
+ import numpy as np
7
+
8
+ from ocr_functions import paddle_ocr,textract_ocr,tesseract_ocr
9
+ from pdf2image import convert_from_bytes
10
+ from multiprocessing import Pool
11
+
12
+
13
+
14
+ model =YOLO("yolo_model/model_3_openvino_model")
15
+ labels = ['Achievement', 'Certifications', 'Community', 'Contact', 'Education', 'Experience', 'Interests', 'Languages', 'Name', 'Profil', 'Projects', 'image', 'resume', 'skills']
16
+
17
+
18
+ def check_intersection(bbox1, bbox2):
19
+ # Check for intersection between two bounding boxes
20
+ x1, y1, x2, y2 = bbox1
21
+ x3, y3, x4, y4 = bbox2
22
+ return not (x3 > x2 or x4 < x1 or y3 > y2 or y4 < y1)
23
+
24
+ def check_inclusion(bbox1, bbox2):
25
+ # Check if one bounding box is completely inside another
26
+ x1, y1, x2, y2 = bbox1
27
+ x3, y3, x4, y4 = bbox2
28
+ return x1 >= x3 and y1 >= y3 and x2 <= x4 and y2 <= y4
29
+
30
+ def union_bbox(bbox1, bbox2):
31
+ # Calculate the union of two bounding boxes
32
+ x1 = min(bbox1[0], bbox2[0])
33
+ y1 = min(bbox1[1], bbox2[1])
34
+ x2 = max(bbox1[2], bbox2[2])
35
+ y2 = max(bbox1[3], bbox2[3])
36
+ return [x1, y1, x2, y2]
37
+
38
+ def filter_bboxes(bboxes):
39
+ # Iterate through each pair of bounding boxes and filter out those that intersect or are completely contained within another
40
+ filtered_bboxes = []
41
+ for bbox1 in bboxes:
42
+ is_valid = True
43
+ for bbox2 in filtered_bboxes:
44
+ if check_intersection(bbox1, bbox2):
45
+ # If the two bounding boxes intersect, compute their union
46
+ bbox1 = union_bbox(bbox1, bbox2)
47
+ # Mark the current bbox as invalid to be removed
48
+ is_valid = False
49
+ break
50
+ elif check_inclusion(bbox1, bbox2):
51
+ # If bbox1 is completely contained within bbox2, mark bbox1 as invalid to be removed
52
+ is_valid = False
53
+ break
54
+ if is_valid:
55
+ filtered_bboxes.append(bbox1)
56
+ return filtered_bboxes
57
+
58
+
59
+
60
+
61
+ def draw_bboxes(image, bboxes ):
62
+ draw = ImageDraw.Draw(image)
63
+ for bbox in bboxes:
64
+ x1, y1, x2, y2 = bbox
65
+
66
+ x1,y1,x2,y2 = int(x1),int(y1),int(x2),int(y2)
67
+ draw.rectangle([(x1, y1), (x2, y2)], outline=(255, 0, 0), width=2)
68
+
69
+
70
+
71
+ def extract_image(image,box):
72
+ x1, y1, x2, y2 = box
73
+ cropped_image = image.crop((x1, y1, x2, y2))
74
+
75
+
76
+
77
+
78
+ def process_bbox(args):
79
+ image, bbox = args
80
+ return textract_ocr(image, bbox)
81
+
82
+ def convert_bboxes_to_original(bboxes, original_size, resized_size):
83
+ """
84
+ Convert bounding boxes from resized image size to original image size using NumPy.
85
+
86
+ :param bboxes: NumPy array of bounding boxes in format [x1, y1, x2, y2] for resized image
87
+ :param original_size: Tuple (original_width, original_height)
88
+ :param resized_size: Tuple (resized_width, resized_height)
89
+ :return: NumPy array of bounding boxes in format [x1, y1, x2, y2] for original image
90
+ """
91
+ original_width, original_height = original_size
92
+ resized_width, resized_height = resized_size
93
+
94
+ # Calculate scaling factors
95
+ x_scale = original_width / resized_width
96
+ y_scale = original_height / resized_height
97
+
98
+ # Convert bounding boxes using broadcasting
99
+ bboxes_np = np.array(bboxes)
100
+ bboxes_np[:, 0] *= x_scale # Scale x1
101
+ bboxes_np[:, 1] *= y_scale # Scale y1
102
+ bboxes_np[:, 2] *= x_scale # Scale x2
103
+ bboxes_np[:, 3] *= y_scale # Scale y2
104
+
105
+ return bboxes_np
106
+
107
+
108
+ def correct_to_by_classifier(text):
109
+ pass
110
+
111
+ def extract_text_from_sections(image):
112
+ cv_parse = {}
113
+ original_size = image.size
114
+ original_img = image
115
+ image = image.resize((640, 640))
116
+ image = image.convert("RGB")
117
+ image_np = np.array(image)
118
+
119
+ # Perform model prediction
120
+ result = model(source=image_np, conf=0.20)
121
+ names = result[0].names # Class names
122
+ data = result[0].boxes.data.numpy()
123
+
124
+ # Extract bounding boxes and their corresponding class labels
125
+ bboxes = data[:, 0:4].tolist()
126
+
127
+
128
+ class_ids = data[:, 5].astype(int).tolist()
129
+
130
+ bboxes_filter = filter_bboxes(bboxes)
131
+ original_bboxes = convert_bboxes_to_original(bboxes_filter, original_size, (640,640))
132
+
133
+
134
+
135
+
136
+ for bbox, class_id in zip(original_bboxes, class_ids):
137
+ class_name = names[class_id]
138
+ if class_name !="image":
139
+ text = textract_ocr(original_img, bbox)
140
+ if class_name in cv_parse:
141
+ cv_parse[class_name] += "\n" + text
142
+ else:
143
+ cv_parse[class_name] = text
144
+
145
+ return cv_parse
146
+
147
+ def merge_dicts_append_strings(dict1, dict2):
148
+ # Create a new dictionary to hold the merged results
149
+ merged_dict = {}
150
+
151
+ # Add all key-value pairs from dict1 to merged_dict
152
+ for key, value in dict1.items():
153
+ merged_dict[key] = value
154
+
155
+ # Append values from dict2 to merged_dict
156
+ for key, value in dict2.items():
157
+ if key in merged_dict:
158
+ merged_dict[key] += "\n" + value
159
+ else:
160
+ merged_dict[key] = value
161
+
162
+ return merged_dict
163
+
164
+
165
+ def cv_to_json(file):
166
+ cv_parsing = {}
167
+ images = convert_from_bytes(file.read())
168
+ for image in images :
169
+ cv_parsing = merge_dicts_append_strings(cv_parsing,extract_text_from_sections(image))
170
+ return cv_parsing
171
+