Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,90 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
from fastapi import FastAPI, HTTPException
|
4 |
-
from pydantic import BaseModel
|
5 |
import numpy as np
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
class InputData(BaseModel):
|
10 |
-
data: List[float] # Lista de caracter铆sticas num茅ricas (flotantes)
|
11 |
-
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
|
|
|
|
|
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
model = Sequential(
|
19 |
-
[
|
20 |
-
InputLayer(
|
21 |
-
input_shape=(5,), name="dense_input"
|
22 |
-
), # Ajusta el tama帽o de entrada seg煤n tu modelo
|
23 |
-
Dense(16, activation="relu", name="dense_16_relu"),
|
24 |
-
Dense(1, activation="sigmoid", name="dense_1_sigmoid"),
|
25 |
-
]
|
26 |
-
)
|
27 |
-
model.load_weights(
|
28 |
-
"model.h5"
|
29 |
-
) # Aseg煤rate de que los nombres de las capas coincidan para que los pesos se carguen correctamente
|
30 |
-
model.compile(
|
31 |
-
loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"]
|
32 |
-
)
|
33 |
-
return model
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Ruta de predicci贸n
|
40 |
@app.post("/predict/")
|
41 |
-
async def predict(
|
42 |
-
print(f"Data: {data}")
|
43 |
-
global model
|
44 |
try:
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
except Exception as e:
|
52 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from minisom import MiniSom
|
|
|
|
|
3 |
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import pickle
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
9 |
+
# Cargar el modelo SOM entrenado
|
10 |
+
with open('som.pkl', 'rb') as infile:
|
11 |
+
som_model = pickle.load(infile)
|
12 |
|
13 |
+
# Etiquetas para los tipos de huellas dactilares
|
14 |
+
LABELS = {0: 'LL', 1: 'RL', 2: 'WH', 3: 'AR'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Funci贸n para procesar y clasificar una imagen de huella dactilar
|
17 |
+
def classify_fingerprint(image_path):
|
18 |
+
# Funci贸n para calcular las orientaciones de la imagen
|
19 |
+
def sobel(I):
|
20 |
+
m,n = I.shape# I de 254x254
|
21 |
+
Gx = np.zeros([m-2,n-2],np.float32)# Gx de 252x252
|
22 |
+
Gy = np.zeros([m-2,n-2],np.float32)# Gy de 252x252
|
23 |
+
gx = [[-1,0,1],[ -2,0,2],[ -1,0,1]]
|
24 |
+
gy = [[1,2,1],[ 0,0,0],[ -1,-2,-1]]
|
25 |
+
for j in range(1,m-2):
|
26 |
+
for i in range(1,n-2):
|
27 |
+
Gx[j-1,i-1] = sum(sum(I[j-1:j+2,i-1:i+2]*gx))
|
28 |
+
Gy[j-1,i-1] = sum(sum(I[j-1:j+2,i-1:i+2]*gy))
|
29 |
+
return Gx,Gy
|
30 |
|
31 |
+
def medfilt2(G,d=3):
|
32 |
+
m,n = G.shape
|
33 |
+
temp = np.zeros([m+2*(d//2),n+2*(d//2)],np.float32)
|
34 |
+
salida = np.zeros([m,n],np.float32)
|
35 |
+
temp[1:m+1,1:n+1] = G
|
36 |
+
for i in range(1,m):
|
37 |
+
for j in range(1,n):
|
38 |
+
A = np.asarray(temp[i-1:i+2,j-1:j+2]).reshape(-1)
|
39 |
+
salida[i-1,j-1] = np.sort(A)[d+1]
|
40 |
+
return salida
|
41 |
+
|
42 |
+
def orientacion(patron,w):
|
43 |
+
Gx,Gy = sobel(patron)# patron de 254x254
|
44 |
+
Gx = medfilt2(Gx)# Gx de 252x252
|
45 |
+
Gy = medfilt2(Gy)# Gy de 252x252
|
46 |
+
m,n = Gx.shape
|
47 |
+
mOrientaciones = np.zeros([m//w,n//w],np.float32)# de una matriz de 18x18
|
48 |
+
for i in range(m//w):
|
49 |
+
for j in range(n//w):
|
50 |
+
YY = sum(sum(2*Gx[i*w:(i+1)*w,j:j+1]*Gy[i*w:(i+1)*w,j:j+1]))
|
51 |
+
XX = sum(sum(Gx[i*w:(i+1)*w,j:j+1]**2-Gy[i*w:(i+1)*w,j:j+1]**2))
|
52 |
+
#YY = sum(sum(2*Gx[i*w:(i+1)*w,0:1]*Gy[i*w:(i+1)*w,0:1]))
|
53 |
+
#XX = sum(sum(Gx[i*w:(i+1)*w,0:1]**2-Gy[i*w:(i+1)*w,0:1]**2))
|
54 |
+
mOrientaciones[i,j] = (0.5*math.atan2(YY,XX) + math.pi/2.0)*(180.0/math.pi)
|
55 |
+
return mOrientaciones
|
56 |
+
|
57 |
+
def representativo(archivo):
|
58 |
+
im = Image.open(archivo)
|
59 |
+
m,n = im.size
|
60 |
+
imarray = np.array(im,np.float32)
|
61 |
+
patron = imarray[1:m-1,1:n-1]# de 256x256 a 254x254
|
62 |
+
EE = orientacion(patron,14)# retorna EE de 18x18
|
63 |
+
return np.asarray(EE).reshape(-1)
|
64 |
|
65 |
+
# Obtener el vector representativo de la imagen de huella dactilar
|
66 |
+
fingerprint_vector = representativo(image_path)
|
67 |
+
|
68 |
+
# Calcular la ubicaci贸n ganadora en el SOM para el vector de huella dactilar
|
69 |
+
winner_position = som_model.winner(fingerprint_vector)
|
70 |
+
|
71 |
+
# Obtener la etiqueta correspondiente a la ubicaci贸n ganadora
|
72 |
+
label = LABELS[som_model[winner_position[0], winner_position[1]]]
|
73 |
+
|
74 |
+
return label
|
75 |
|
76 |
# Ruta de predicci贸n
|
77 |
@app.post("/predict/")
|
78 |
+
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
79 |
try:
|
80 |
+
# Guardar la imagen subida
|
81 |
+
with open("temp_image.tif", "wb") as buffer:
|
82 |
+
buffer.write(await file.read())
|
83 |
+
|
84 |
+
# Clasificar la imagen de huella dactilar
|
85 |
+
prediction = classify_fingerprint("temp_image.tif")
|
86 |
+
|
87 |
+
return {"prediction": prediction}
|
88 |
+
|
89 |
except Exception as e:
|
90 |
raise HTTPException(status_code=500, detail=str(e))
|