Procesa DNI Listo
Browse files- __pycache__/app.cpython-311.pyc +0 -0
- __pycache__/funciones.cpython-311.pyc +0 -0
- app.py +2 -2
- documentos.py +10 -0
- funciones.py +21 -7
- herramientas.py +47 -0
- obtenCampo.py +13 -0
__pycache__/app.cpython-311.pyc
CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
|
|
__pycache__/funciones.cpython-311.pyc
CHANGED
Binary files a/__pycache__/funciones.cpython-311.pyc and b/__pycache__/funciones.cpython-311.pyc differ
|
|
app.py
CHANGED
@@ -29,9 +29,9 @@ async def echo_image(image: UploadFile = File(...)):
|
|
29 |
return StreamingResponse(BytesIO(contents), media_type=image.content_type)
|
30 |
|
31 |
@app.post(
|
32 |
-
"/
|
33 |
summary="Procesamiento de DNI")
|
34 |
async def comidas(image: UploadFile = File(...)):
|
35 |
if not image.content_type.startswith("image/"):
|
36 |
return {"error": "El archivo no es una imagen"}
|
37 |
-
return funciones.procesa_dni(image)
|
|
|
29 |
return StreamingResponse(BytesIO(contents), media_type=image.content_type)
|
30 |
|
31 |
@app.post(
|
32 |
+
"/procesa_dni/",
|
33 |
summary="Procesamiento de DNI")
|
34 |
async def comidas(image: UploadFile = File(...)):
|
35 |
if not image.content_type.startswith("image/"):
|
36 |
return {"error": "El archivo no es una imagen"}
|
37 |
+
return await funciones.procesa_dni(image)
|
documentos.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import obtenCampo
|
2 |
+
|
3 |
+
def dni(textos_extraidos):
|
4 |
+
textos_extraidos_simplificados = [texto.lower().replace(" ", "") for texto in textos_extraidos]
|
5 |
+
|
6 |
+
#Campos Buscados
|
7 |
+
nombre, apellido = obtenCampo.Nombre(textos_extraidos, textos_extraidos_simplificados)
|
8 |
+
identificacion = obtenCampo.Identificacion(textos_extraidos, textos_extraidos_simplificados)
|
9 |
+
|
10 |
+
return nombre, apellido, identificacion
|
funciones.py
CHANGED
@@ -1,15 +1,29 @@
|
|
|
|
|
|
|
|
1 |
from gradio_client import Client, handle_file
|
2 |
|
3 |
-
def procesa_dni(image):
|
4 |
|
5 |
-
|
6 |
|
7 |
client = Client("Moibe/api_rapicash")
|
8 |
-
|
9 |
-
img=handle_file(
|
10 |
lang="en",
|
11 |
api_name="/predict"
|
12 |
-
)
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import herramientas
|
2 |
+
import documentos
|
3 |
+
import time
|
4 |
from gradio_client import Client, handle_file
|
5 |
|
6 |
+
async def procesa_dni(image):
|
7 |
|
8 |
+
temp_image = await herramientas.imageToTemp(image)
|
9 |
|
10 |
client = Client("Moibe/api_rapicash")
|
11 |
+
dict_recibido = client.predict(
|
12 |
+
img=handle_file(temp_image),
|
13 |
lang="en",
|
14 |
api_name="/predict"
|
15 |
+
) #Ésto porque gradio envía un dict con headers y su key data es el que contiene nuestro result original que tenía en la API original.
|
16 |
+
|
17 |
+
#Aquí es donde personalizo el proceso:
|
18 |
+
textos_extraidos = herramientas.listaTextosExtraidos(dict_recibido)
|
19 |
|
20 |
+
#Campos DNI Panamá.
|
21 |
+
nombre, apellido, identificacion = documentos.dni(textos_extraidos)
|
22 |
+
|
23 |
+
print(f"Hola: {nombre}, {apellido} con identificación: {identificacion}")
|
24 |
+
|
25 |
+
return {
|
26 |
+
"nombre": nombre,
|
27 |
+
"apellido": apellido,
|
28 |
+
"identificacion": identificacion
|
29 |
+
}
|
herramientas.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import time
|
3 |
+
|
4 |
+
async def imageToTemp(image):
|
5 |
+
print("Estoy en imageToTemp...")
|
6 |
+
|
7 |
+
try:
|
8 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=f"_{image.filename}") as tmp_file:
|
9 |
+
contents = await image.read()
|
10 |
+
tmp_file.write(contents)
|
11 |
+
temp_file_path = tmp_file.name
|
12 |
+
|
13 |
+
print(f"Archivo temporal guardado en: {temp_file_path}")
|
14 |
+
return temp_file_path
|
15 |
+
|
16 |
+
except Exception as e:
|
17 |
+
print(f"Error al procesar el archivo: {e}")
|
18 |
+
return {"error": "Error al procesar la imagen"}
|
19 |
+
|
20 |
+
|
21 |
+
def listaTextosExtraidos(dict_recibido):
|
22 |
+
|
23 |
+
result = dict_recibido['data']
|
24 |
+
print("Datos extraídos (acceso directo):")
|
25 |
+
|
26 |
+
textos_extraidos = []
|
27 |
+
print("Líneas encontradas con modelo IA:")
|
28 |
+
for item in result:
|
29 |
+
texto = item[1][0]
|
30 |
+
print(texto)
|
31 |
+
textos_extraidos.append(texto)
|
32 |
+
return textos_extraidos
|
33 |
+
|
34 |
+
#Herramientas para DNI Panamá.
|
35 |
+
|
36 |
+
def buscaIndexPalabra(arreglo, palabra):
|
37 |
+
palabra_limpia = palabra.lower().replace(" ", "")
|
38 |
+
for i, texto_limpio in enumerate(arreglo):
|
39 |
+
if palabra_limpia in texto_limpio:
|
40 |
+
return i
|
41 |
+
return 'error'
|
42 |
+
|
43 |
+
def buscarPatronCedula(lista_textos):
|
44 |
+
for i, texto in enumerate(lista_textos):
|
45 |
+
if texto and texto[0].isdigit() and '-' in texto:
|
46 |
+
return i
|
47 |
+
return 'error'
|
obtenCampo.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import herramientas
|
2 |
+
|
3 |
+
def Nombre(textos_extraidos, textos_extraidos_limpios):
|
4 |
+
indice = herramientas.buscaIndexPalabra(textos_extraidos_limpios, 'usual')
|
5 |
+
print(f"Indice es: {indice} y es del tipo {type(indice)}...")
|
6 |
+
nombre = textos_extraidos[indice-2]
|
7 |
+
apellido = textos_extraidos[indice-1]
|
8 |
+
return nombre, apellido
|
9 |
+
|
10 |
+
def Identificacion(textos_extraidos, textos_extraidos_limpios):
|
11 |
+
indice = herramientas.buscarPatronCedula(textos_extraidos_limpios)
|
12 |
+
identificacion = textos_extraidos[indice]
|
13 |
+
return identificacion
|