File size: 1,842 Bytes
e0b339d
 
d35e97b
 
e0b339d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35e97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tempfile
import time
import herramientas
from gradio_client import Client, handle_file

async def imageToTemp(image):
    print("Estoy en imageToTemp...")

    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix=f"_{image.filename}") as tmp_file:
            contents = await image.read()
            tmp_file.write(contents)
            temp_file_path = tmp_file.name
        
        print(f"Archivo temporal guardado en: {temp_file_path}")
        return temp_file_path

    except Exception as e:
        print(f"Error al procesar el archivo: {e}")
        return {"error": "Error al procesar la imagen"}
    

def listaTextosExtraidos(dict_recibido):

    result = dict_recibido['data']
    print("Datos extraídos (acceso directo):")
    
    textos_extraidos = [] 
    print("Líneas encontradas con modelo IA:")
    for item in result:
        texto = item[1][0] 
        print(texto)
        textos_extraidos.append(texto)
    return textos_extraidos

#Herramientas para DNI Panamá.

def buscaIndexPalabra(arreglo, palabra):
    palabra_limpia = palabra.lower().replace(" ", "")
    for i, texto_limpio in enumerate(arreglo):        
        if palabra_limpia in texto_limpio:
            return i
    return 'error'

def buscarPatronCedula(lista_textos):    
    for i, texto in enumerate(lista_textos):
        if texto and texto[0].isdigit() and '-' in texto:     
            return i 
    return 'error'

async def procesaImagen(image):

    temp_image = await imageToTemp(image)

    client = Client("Moibe/api_rapicash")
    dict_recibido = client.predict(
            img=handle_file(temp_image),
            lang="en",
            api_name="/predict"
    )             
    #Aquí es donde personalizo el proceso:
    textos_extraidos = listaTextosExtraidos(dict_recibido) 
    return textos_extraidos