ITSAIDI
commited on
Commit
·
8bb3f75
1
Parent(s):
59bcc7e
sdf
Browse files- utilitis.py +48 -42
utilitis.py
CHANGED
@@ -75,10 +75,38 @@ def unnormalize_box(bbox, width, height):
|
|
75 |
width * (bbox[2] / 1000),
|
76 |
height * (bbox[3] / 1000),
|
77 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
#############################################################################
|
79 |
#############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
def Run_model(image):
|
81 |
-
encoding,offset_mapping,
|
82 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
83 |
# load the fine-tuned model from the hub
|
84 |
model = LayoutLMv3ForTokenClassification.from_pretrained(model_Hugging_path)
|
@@ -86,28 +114,20 @@ def Run_model(image):
|
|
86 |
# forward pass
|
87 |
outputs = model(**encoding)
|
88 |
|
|
|
89 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
90 |
token_boxes = encoding.bbox.squeeze().tolist()
|
|
|
|
|
|
|
91 |
|
92 |
-
width, height = image.size
|
93 |
-
|
94 |
-
id2label, _ = Labels()
|
95 |
-
is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
|
96 |
-
true_predictions = [id2label[pred] for idx, pred in enumerate(predictions) if not is_subword[idx]]
|
97 |
-
true_boxes = [unnormalize_box(box, width, height) for idx, box in enumerate(token_boxes) if not is_subword[idx]]
|
98 |
-
return true_predictions,true_boxes,words
|
99 |
-
|
100 |
#############################################################################
|
101 |
#############################################################################
|
102 |
-
def Get_Json(
|
103 |
Results = {}
|
104 |
-
|
105 |
-
for prd in true_predictions:
|
106 |
if prd in ['InvNum','Fourni', 'InvDate','TT','TTC','TVA']:
|
107 |
-
|
108 |
-
print(i,prd,words[i-1])
|
109 |
-
Results[prd] = words[i-1]
|
110 |
-
i+=1
|
111 |
key_mapping = {'InvNum':'Numéro de facture','Fourni':'Fournisseur', 'InvDate':'Date Facture','TT':'Total HT','TTC':'Total TTC','TVA':'TVA'}
|
112 |
Results = {key_mapping.get(key, key): value for key, value in Results.items()}
|
113 |
return Results
|
@@ -117,10 +137,9 @@ def Get_Json(true_predictions,words):
|
|
117 |
def Draw(image):
|
118 |
start_time = time.time()
|
119 |
|
120 |
-
image = enhance_image(image,1.3,1.
|
121 |
-
|
122 |
draw = ImageDraw.Draw(image)
|
123 |
-
|
124 |
label2color = {
|
125 |
'InvNum': 'blue',
|
126 |
'InvDate': 'green',
|
@@ -135,7 +154,6 @@ def Draw(image):
|
|
135 |
rectangle_thickness = 4
|
136 |
label_x_offset = 20
|
137 |
label_y_offset = -30
|
138 |
-
|
139 |
# Custom font size
|
140 |
custom_font_size = 25
|
141 |
|
@@ -143,34 +161,22 @@ def Draw(image):
|
|
143 |
font_path = "arial.ttf" # Specify the path to your font file
|
144 |
custom_font = ImageFont.truetype(font_path, custom_font_size)
|
145 |
|
146 |
-
for
|
147 |
-
predicted_label =
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
# Draw text using the custom font and size
|
156 |
-
draw.rectangle((box[0], box[1]+ label_y_offset,box[2],box[3]+ label_y_offset), fill=color)
|
157 |
-
draw.text((box[0] + label_x_offset, box[1] + label_y_offset), text=predicted_label, fill='white', font=custom_font)
|
158 |
-
|
159 |
-
# Get the Results Json File
|
160 |
-
Results = Get_Json(true_predictions,words)
|
161 |
|
|
|
162 |
end_time = time.time()
|
163 |
execution_time = end_time - start_time
|
164 |
|
165 |
return image,Results,execution_time
|
166 |
|
167 |
-
#############################################################################
|
168 |
-
#############################################################################
|
169 |
-
|
170 |
-
def Add_Results(data):
|
171 |
-
# Render the table
|
172 |
-
for key, value in data.items():
|
173 |
-
data[key] = st.sidebar.text_input(key, value)
|
174 |
|
175 |
#############################################################################
|
176 |
#############################################################################
|
|
|
75 |
width * (bbox[2] / 1000),
|
76 |
height * (bbox[3] / 1000),
|
77 |
]
|
78 |
+
def get_word(bboxes,image):
|
79 |
+
ocr = Paddle()
|
80 |
+
x_min, y_min, x_max, y_max = bboxes
|
81 |
+
roi = image.crop((x_min, y_min, x_max, y_max)) # Region of intrest
|
82 |
+
roi_np = np.array(roi) # To array
|
83 |
+
result = ocr.ocr(roi_np, cls=True) # Apply OCR to ROI
|
84 |
+
return result[0][0][1][0]
|
85 |
#############################################################################
|
86 |
#############################################################################
|
87 |
+
def get_Finale_results(offset_mapping,id2label,image,prediction_scores,predictions,token_boxes):
|
88 |
+
width, height = image.size
|
89 |
+
is_subword = np.array(offset_mapping.squeeze().tolist())[:,0] != 0
|
90 |
+
# Filter out subword tokens and extract true predictions and scores
|
91 |
+
true_predictions_with_scores = [(idx,id2label[pred], score[pred],unnormalize_box(box, width, height)) for idx, (pred, score,box) in enumerate(zip(predictions, prediction_scores,token_boxes)) if not is_subword[idx]]
|
92 |
+
Final_prediction = [pred for pred in true_predictions_with_scores if pred[1] != "Autre"]
|
93 |
+
# Create a dictionary to store the highest score for each prediction
|
94 |
+
Final_results = {}
|
95 |
+
# Eliminete Duplication of Predictions
|
96 |
+
for index, prediction, score, bbox in Final_prediction:
|
97 |
+
if prediction not in Final_results or score > Final_results[prediction][1]:
|
98 |
+
Final_results[prediction] = (index, score,bbox)
|
99 |
+
#print(Final_results)
|
100 |
+
|
101 |
+
for final in Final_results:
|
102 |
+
Kalma = get_word(Final_results[final][2],image)
|
103 |
+
New_tuple = (Kalma,Final_results[final][1],Final_results[final][2])
|
104 |
+
Final_results[final] = New_tuple
|
105 |
+
|
106 |
+
return Final_results
|
107 |
+
|
108 |
def Run_model(image):
|
109 |
+
encoding,offset_mapping,_ = Encode(image)
|
110 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
111 |
# load the fine-tuned model from the hub
|
112 |
model = LayoutLMv3ForTokenClassification.from_pretrained(model_Hugging_path)
|
|
|
114 |
# forward pass
|
115 |
outputs = model(**encoding)
|
116 |
|
117 |
+
prediction_scores = outputs.logits.softmax(-1).squeeze().tolist()
|
118 |
predictions = outputs.logits.argmax(-1).squeeze().tolist()
|
119 |
token_boxes = encoding.bbox.squeeze().tolist()
|
120 |
+
id2label, _ = Labels()
|
121 |
+
Finale_results=get_Finale_results(offset_mapping,id2label,image,prediction_scores,predictions,token_boxes)
|
122 |
+
return Finale_results
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
#############################################################################
|
125 |
#############################################################################
|
126 |
+
def Get_Json(Finale_results):
|
127 |
Results = {}
|
128 |
+
for prd in Finale_results:
|
|
|
129 |
if prd in ['InvNum','Fourni', 'InvDate','TT','TTC','TVA']:
|
130 |
+
Results[prd] = Finale_results[prd][0]
|
|
|
|
|
|
|
131 |
key_mapping = {'InvNum':'Numéro de facture','Fourni':'Fournisseur', 'InvDate':'Date Facture','TT':'Total HT','TTC':'Total TTC','TVA':'TVA'}
|
132 |
Results = {key_mapping.get(key, key): value for key, value in Results.items()}
|
133 |
return Results
|
|
|
137 |
def Draw(image):
|
138 |
start_time = time.time()
|
139 |
|
140 |
+
image = enhance_image(image,1.3,1.7)
|
141 |
+
Finale_results = Run_model(image)
|
142 |
draw = ImageDraw.Draw(image)
|
|
|
143 |
label2color = {
|
144 |
'InvNum': 'blue',
|
145 |
'InvDate': 'green',
|
|
|
154 |
rectangle_thickness = 4
|
155 |
label_x_offset = 20
|
156 |
label_y_offset = -30
|
|
|
157 |
# Custom font size
|
158 |
custom_font_size = 25
|
159 |
|
|
|
161 |
font_path = "arial.ttf" # Specify the path to your font file
|
162 |
custom_font = ImageFont.truetype(font_path, custom_font_size)
|
163 |
|
164 |
+
for result in Finale_results:
|
165 |
+
predicted_label = result
|
166 |
+
box = Finale_results[result][2]
|
167 |
+
color = label2color[result]
|
168 |
+
draw.rectangle(box, outline=color, width=rectangle_thickness)
|
169 |
+
#print(box)
|
170 |
+
# Draw text using the custom font and size
|
171 |
+
draw.rectangle((box[0], box[1]+ label_y_offset,box[2],box[3]+ label_y_offset), fill=color)
|
172 |
+
draw.text((box[0] + label_x_offset, box[1] + label_y_offset), text=predicted_label, fill='white', font=custom_font)
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
+
Results = Get_Json(Finale_results)
|
175 |
end_time = time.time()
|
176 |
execution_time = end_time - start_time
|
177 |
|
178 |
return image,Results,execution_time
|
179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
#############################################################################
|
182 |
#############################################################################
|