2025-04-12 10:51:07 +02:00
|
|
|
import base64
|
|
|
|
import io
|
2025-04-12 20:12:14 +02:00
|
|
|
from tempfile import NamedTemporaryFile
|
2025-04-12 10:51:07 +02:00
|
|
|
from PIL import Image
|
|
|
|
import pytesseract
|
2025-04-12 20:12:14 +02:00
|
|
|
from passporteye import read_mrz
|
|
|
|
import json
|
2025-04-12 10:51:07 +02:00
|
|
|
|
|
|
|
def process_passport(passport_b64: str) -> str:
|
|
|
|
"""
|
|
|
|
Traite le passport :
|
|
|
|
- Décodage de l'image en base64.
|
|
|
|
- Application de l'OCR pour extraire le texte.
|
|
|
|
|
|
|
|
:param passport_b64: Chaîne base64 représentant l'image du passport.
|
|
|
|
:return: Texte extrait de l'image.
|
|
|
|
"""
|
|
|
|
image_bytes = base64.b64decode(passport_b64)
|
2025-04-12 20:12:14 +02:00
|
|
|
# image = Image.open(io.BytesIO(image_bytes))
|
|
|
|
# text = pytesseract.image_to_string(image, lang='eng')
|
|
|
|
with NamedTemporaryFile(mode="wb") as tmp_img:
|
|
|
|
tmp_img.write(image_bytes)
|
|
|
|
with open(tmp_img.name, "rb") as read_img:
|
|
|
|
text = read_mrz(read_img)
|
|
|
|
# text = json.dumps(text)
|
|
|
|
# TODO CONTINUE
|
2025-04-12 10:51:07 +02:00
|
|
|
return text
|