Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, File, UploadFile, HTTPException | |
from minisom import MiniSom | |
import numpy as np | |
from PIL import Image | |
import pickle | |
app = FastAPI() | |
# Cargar el modelo SOM entrenado | |
with open('som.pkl', 'rb') as infile: | |
som_model = pickle.load(infile) | |
# Etiquetas para los tipos de huellas dactilares | |
LABELS = {0: 'LL', 1: 'RL', 2: 'WH', 3: 'AR'} | |
# Funci贸n para procesar y clasificar una imagen de huella dactilar | |
def classify_fingerprint(image_path): | |
# Funci贸n para calcular las orientaciones de la imagen | |
def sobel(I): | |
m,n = I.shape# I de 254x254 | |
Gx = np.zeros([m-2,n-2],np.float32)# Gx de 252x252 | |
Gy = np.zeros([m-2,n-2],np.float32)# Gy de 252x252 | |
gx = [[-1,0,1],[ -2,0,2],[ -1,0,1]] | |
gy = [[1,2,1],[ 0,0,0],[ -1,-2,-1]] | |
for j in range(1,m-2): | |
for i in range(1,n-2): | |
Gx[j-1,i-1] = sum(sum(I[j-1:j+2,i-1:i+2]*gx)) | |
Gy[j-1,i-1] = sum(sum(I[j-1:j+2,i-1:i+2]*gy)) | |
return Gx,Gy | |
def medfilt2(G,d=3): | |
m,n = G.shape | |
temp = np.zeros([m+2*(d//2),n+2*(d//2)],np.float32) | |
salida = np.zeros([m,n],np.float32) | |
temp[1:m+1,1:n+1] = G | |
for i in range(1,m): | |
for j in range(1,n): | |
A = np.asarray(temp[i-1:i+2,j-1:j+2]).reshape(-1) | |
salida[i-1,j-1] = np.sort(A)[d+1] | |
return salida | |
def orientacion(patron,w): | |
Gx,Gy = sobel(patron)# patron de 254x254 | |
Gx = medfilt2(Gx)# Gx de 252x252 | |
Gy = medfilt2(Gy)# Gy de 252x252 | |
m,n = Gx.shape | |
mOrientaciones = np.zeros([m//w,n//w],np.float32)# de una matriz de 18x18 | |
for i in range(m//w): | |
for j in range(n//w): | |
YY = sum(sum(2*Gx[i*w:(i+1)*w,j:j+1]*Gy[i*w:(i+1)*w,j:j+1])) | |
XX = sum(sum(Gx[i*w:(i+1)*w,j:j+1]**2-Gy[i*w:(i+1)*w,j:j+1]**2)) | |
#YY = sum(sum(2*Gx[i*w:(i+1)*w,0:1]*Gy[i*w:(i+1)*w,0:1])) | |
#XX = sum(sum(Gx[i*w:(i+1)*w,0:1]**2-Gy[i*w:(i+1)*w,0:1]**2)) | |
mOrientaciones[i,j] = (0.5*math.atan2(YY,XX) + math.pi/2.0)*(180.0/math.pi) | |
return mOrientaciones | |
def representativo(archivo): | |
im = Image.open(archivo) | |
m,n = im.size | |
imarray = np.array(im,np.float32) | |
patron = imarray[1:m-1,1:n-1]# de 256x256 a 254x254 | |
EE = orientacion(patron,14)# retorna EE de 18x18 | |
return np.asarray(EE).reshape(-1) | |
# Obtener el vector representativo de la imagen de huella dactilar | |
fingerprint_vector = representativo(image_path) | |
# Calcular la ubicaci贸n ganadora en el SOM para el vector de huella dactilar | |
winner_position = som_model.winner(fingerprint_vector) | |
# Obtener la etiqueta correspondiente a la ubicaci贸n ganadora | |
label = LABELS[som_model[winner_position[0], winner_position[1]]] | |
return label | |
# Ruta de predicci贸n | |
async def predict(file: UploadFile = File(...)): | |
try: | |
# Guardar la imagen subida | |
with open("temp_image.tif", "wb") as buffer: | |
buffer.write(await file.read()) | |
# Clasificar la imagen de huella dactilar | |
prediction = classify_fingerprint("temp_image.tif") | |
return {"prediction": prediction} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |