File size: 5,661 Bytes
8e212da
 
 
c9443bb
 
8e212da
c9443bb
 
 
 
 
 
 
 
 
 
 
 
8e212da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
from google.cloud import vision
import re
import json
import tempfile
##

def get_credentials():
    creds_json_str = os.getenv("cloud_vision")

    #create temporale file
    with tempfile.NamedTemporaryFile(mode="w+",delete=False, suffix=".json") as temp:
        temp.write(creds_json_str) #write the content in json format
        temp_filename = temp.name
        
    return temp_filename
    
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = get_credentials()

##
def info_new_cni(donnees):
    ##
    informations = {}

    # Utilisation d'expressions régulières pour extraire les informations spécifiques
    numero_carte = re.search(r'n° (C\d+)', ' '.join(donnees))
    #prenom_nom = re.search(r'Prénom\(s\)\s+(.*?)\s+Nom\s+(.*?)\s+Signature', ' '.join(donnees))
    nom = re.search(r'Nom\s+(.*?)\s', ' '.join(donnees))
    prenom = re.search(r'Prénom\(s\)\s+(.*?)\s+Nom\s+(.*?)', ' '.join(donnees))
    date_naissance = re.search(r'Date de Naissance\s+(.*?)+(\d{2}/\d{2}/\d{4})', ' '.join(donnees))
    lieu_naissance = re.search(r'Lieu de Naissance\s+(.*?)\s', ' '.join(donnees))
    taille = re.search(r'Sexe Taille\s+(.*?)+(\d+,\d+)', ' '.join(donnees))
    nationalite = re.search(r'Nationalité\s+(.*?)\s+\d+', ' '.join(donnees))
    date_expiration = re.search(r'Date d\'expiration\s+(\d+/\d+/\d+)', ' '.join(donnees))
    sexe = re.search(r'Date de Naissance\s+(.*?)+(\d{2}/\d{2}/\d{4})+(.*)', ' '.join(donnees))

    # Stockage des informations extraites dans un dictionnaire
    if numero_carte:
        informations['Numéro de carte'] = numero_carte.group(1)
    if nom :
        informations['Nom'] = nom.group(1)

    if prenom:
        informations['Prénom'] = prenom.group(1)

    if date_naissance:
        informations['Date de Naissance'] = date_naissance.group(2)
    if lieu_naissance:
        informations['Lieu de Naissance'] = lieu_naissance.group(1)
    if taille:
        informations['Taille'] = taille.group(2)
    if nationalite:
        informations['Nationalité'] = nationalite.group(1)
    if date_expiration:
        informations['Date d\'expiration'] = date_expiration.group(1)
    if sexe :
      informations['sexe'] =  sexe.group(3)[:2]

    return informations

##

def info_ancien_cni(infos):
  """ Extract information in row data of ocr"""

  informations = {}

  immatriculation_patern = r'Immatriculation:\s+(C \d{4} \d{4} \d{2})'
  immatriculation = re.search(immatriculation_patern, ''.join(infos))
  nom = infos[4]
  prenom_pattern = r'Nom\n(.*?)\n'
  prenom = re.search(prenom_pattern, '\n'.join(infos))
  sexe_pattern = r'Prénoms\n(.*?)\n'
  sexe = re.search(sexe_pattern, '\n'.join(infos))
  taille_pattern = r'Sexe\n(.*?)\n'
  taille = re.search(taille_pattern, '\n'.join(infos))
  date_naiss_pattern = r'Taille\s+(.*?)+(\d+/\d+/\d+)' # r'Taille (m)\n(.*?)\n'
  date_naissance = re.search(date_naiss_pattern, ' '.join(infos))
  lieu_pattern = r'Date de Naissance\n(.*?)\n'
  lieu_naissance = re.search(lieu_pattern, '\n'.join(infos))
  valide_pattern = r'Valide jusqu\'au+(.*?)+(\d+/\d+/\d+)'
  validite = re.search(valide_pattern, ' '.join(infos))

  # Stockage des informations extraites dans un dictionnaire
  if immatriculation:
      informations['Immatriculation'] = immatriculation.group(1)
  if nom :
      informations['Nom'] =  infos[4]

  if prenom:
      informations['Prénom'] = prenom.group(1)

  if date_naissance:
      informations['Date de Naissance'] = date_naissance.group(2)
  if lieu_naissance:
      informations['Lieu de Naissance'] = lieu_naissance.group(1)
  if taille:
      informations['Taille'] = taille.group(1)
  
  if validite:
      informations['Date d\'expiration'] = validite.group(2)
  if sexe :
    informations['sexe'] =  sexe.group(1)

  return informations

  ##
def filtrer_elements(liste):
  elements_filtres = []
  for element in liste:
      if element not in ['\r',"RÉPUBLIQUE DE CÔTE D'IVOIRE", "MINISTÈRE DES TRANSPORTS", "PERMIS DE CONDUIRE"]:
          elements_filtres.append(element)
  return elements_filtres

def permis_de_conduite(donnees):
  """ Extraire les information de permis de conduire"""

  informations = {}
  tab = filtrer_elements(donnees)
  informations['Nom'] = tab[2]
  informations['Prenoms'] = tab[4]
  informations['Date_et_lieu_de_naissance'] = tab[6]
  informations['Date_et_lieu_de_délivrance'] = tab[8]
  informations['Categorie'] = tab[0]
  informations['Numéro_du_permis_de_conduire'] = tab[10]
  informations['Restriction(s)'] = tab[12:] if len(tab) > 11 else ''

  return informations


# Fonction pour extraire les informations individuelles
def extraire_informations_carte(path, type_de_piece=1):
    """ Detect text in identity card"""

    client = vision.ImageAnnotatorClient()

    with open(path,'rb') as image_file:
      content = image_file.read()
    
    image = vision.Image(content = content)

    # for non dense text
    #response = client.text_detection(image=image)
    #for dense text
    response = client.document_text_detection(image = image)
    texts = response.text_annotations
    ocr_texts = []

    for text in texts:
      ocr_texts.append(f"\r\n{text.description}")

    if response.error.message :
      raise Exception("{}\n For more informations check : https://cloud.google.com/apis/design/errors".format(response.error.message))

    donnees = ocr_texts[0].split('\n')

    if type_de_piece ==1:
      return info_new_cni(donnees)
    elif type_de_piece == 2:
      return info_ancien_cni(donnees)
    elif type_de_piece == 3:
      return permis_de_conduite(donnees)
    else :
      return "Le traitement de ce type de document n'est pas encore pris en charge"