youl commited on
Commit
cd20ff6
·
verified ·
1 Parent(s): 841f072

update utils

Browse files
Files changed (1) hide show
  1. utils.py +76 -23
utils.py CHANGED
@@ -1,21 +1,14 @@
1
  import os
2
  from google.cloud import vision
3
  import re
4
- import json
5
- import tempfile
 
 
 
 
6
  ##
7
-
8
- def get_credentials():
9
- creds_json_str = os.getenv("cloud_vision")
10
-
11
- #create temporale file
12
- with tempfile.NamedTemporaryFile(mode="w+",delete=False, suffix=".json") as temp:
13
- temp.write(creds_json_str) #write the content in json format
14
- temp_filename = temp.name
15
-
16
- return temp_filename
17
-
18
- os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = get_credentials()
19
 
20
  ##
21
  def info_new_cni(donnees):
@@ -116,14 +109,39 @@ def permis_de_conduite(donnees):
116
  """ Extraire les information de permis de conduire"""
117
 
118
  informations = {}
119
- tab = filtrer_elements(donnees)
120
- informations['Nom'] = tab[2]
121
- informations['Prenoms'] = tab[4]
122
- informations['Date_et_lieu_de_naissance'] = tab[6]
123
- informations['Date_et_lieu_de_délivrance'] = tab[8]
124
- informations['Categorie'] = tab[0]
125
- informations['Numéro_du_permis_de_conduire'] = tab[10]
126
- informations['Restriction(s)'] = tab[12:] if len(tab) > 11 else ''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
  return informations
129
 
@@ -161,4 +179,39 @@ def extraire_informations_carte(path, type_de_piece=1):
161
  elif type_de_piece == 3:
162
  return permis_de_conduite(donnees)
163
  else :
164
- return "Le traitement de ce type de document n'est pas encore pris en charge"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  from google.cloud import vision
3
  import re
4
+ import torch
5
+ import torchvision
6
+ import numpy as np
7
+ from PIL import Image
8
+ import albumentations as A
9
+ from albumentations.pytorch import ToTensorV2
10
  ##
11
+ os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'data/ocr_vision_token.json'
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  ##
14
  def info_new_cni(donnees):
 
109
  """ Extraire les information de permis de conduire"""
110
 
111
  informations = {}
112
+
113
+ infos = filtrer_elements(donnees)
114
+
115
+ nom_pattern = r'Nom\n(.*?)\n'
116
+ nom = re.search(nom_pattern, '\n'.join(infos))
117
+ prenom_pattern = r'Prénoms\n(.*?)\n'
118
+ prenom = re.search(prenom_pattern, '\n'.join(infos))
119
+ date_lieu_naissance_patern = r'Date et lieu de naissance\n(.*?)\n'
120
+ date_lieu_naissance = re.search(date_lieu_naissance_patern, '\n'.join(infos))
121
+ date_lieu_delivrance_patern = r'Date et lieu de délivrance\n(.*?)\n'
122
+ date_lieu_delivrance = re.search(date_lieu_delivrance_patern, '\n'.join(infos))
123
+ numero_pattern = r'Numéro du permis de conduire\n(.*?)\n'
124
+ numero = re.search(numero_pattern, '\n'.join(infos))
125
+ restriction_pattern = r'Restriction\(s\)\s+(.*?)+(.*)'
126
+ restriction = re.search(restriction_pattern, ' '.join(infos))
127
+
128
+ # Stockage des informations extraites dans un dictionnaire
129
+ if nom:
130
+ informations['Nom'] = nom.group(1)
131
+
132
+ if prenom :
133
+ informations['Prenoms'] = prenom.group(1)
134
+ if date_lieu_naissance :
135
+ informations['Date_et_lieu_de_naissance'] = date_lieu_naissance.group(1)
136
+ if date_lieu_naissance :
137
+ informations['Date_et_lieu_de_délivrance'] = date_lieu_delivrance.group(1)
138
+
139
+ informations['Categorie'] = infos[0]
140
+ if numero:
141
+ informations['Numéro_du_permis_de_conduire'] = numero.group(1)
142
+
143
+ if restriction:
144
+ informations['Restriction(s)'] = restriction.group(2)
145
 
146
  return informations
147
 
 
179
  elif type_de_piece == 3:
180
  return permis_de_conduite(donnees)
181
  else :
182
+ return "Le traitement de ce type de document n'est pas encore pris en charge"
183
+
184
+ def load_checkpoint(path):
185
+ print('--> Loading checkpoint')
186
+ return torch.load(path,map_location=torch.device('cpu'))
187
+
188
+ def make_prediction(image_path):
189
+
190
+ # define the using of GPU or CPU et background training
191
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
192
+ ## load model
193
+ model = load_checkpoint("data/model.pth")
194
+ ## transformation
195
+ test_transforms = A.Compose([
196
+ A.Resize(height=224, width=224, always_apply=True),
197
+ A.Normalize(always_apply=True),
198
+ ToTensorV2(always_apply=True),])
199
+
200
+ ## read the image
201
+ image = np.array(Image.open(image_path).convert('RGB'))
202
+ transformed = test_transforms(image= image)
203
+ image_transformed = transformed["image"]
204
+ image_transformed = image_transformed.unsqueeze(0)
205
+ image_transformed = image_transformed.to(device)
206
+
207
+ model.eval()
208
+ with torch.set_grad_enabled(False):
209
+ output = model(image_transformed)
210
+
211
+ # Post-process predictions
212
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
213
+ predicted_class = torch.argmax(probabilities).item()
214
+ proba = float(max(probabilities))
215
+
216
+
217
+ return proba, predicted_class