Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import numpy as np
|
|
4 |
import pickle
|
5 |
from io import BytesIO
|
6 |
import math
|
7 |
-
import base64
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
@@ -25,33 +24,24 @@ M = np.array([
|
|
25 |
[ 2., -1., -1., -1., 1., -1., -1., -1., -1., 2.]
|
26 |
])
|
27 |
|
28 |
-
# Funci贸n para realizar la predicci贸n de huellas dactilares
|
29 |
def predict_fingerprint(image):
|
30 |
try:
|
31 |
-
# Preprocesar la imagen para que coincida con las dimensiones esperadas por el SOM
|
32 |
processed_image = preprocess_image(image)
|
33 |
-
# Obtener la ubicaci贸n del nodo ganador en el SOM
|
34 |
winner = som.winner(processed_image)
|
35 |
-
# Asignar la etiqueta correspondiente a la ubicaci贸n ganadora en el SOM
|
36 |
fingerprint_type = get_fingerprint_type(winner)
|
37 |
return fingerprint_type
|
38 |
except Exception as e:
|
39 |
-
print("prediccion")
|
40 |
raise HTTPException(status_code=500, detail=str(e))
|
41 |
|
42 |
def preprocess_image(image):
|
43 |
# Guardar la imagen en formato TIFF
|
44 |
image.save("temp_image.tif")
|
45 |
-
print(f"se guard贸{image}")
|
46 |
-
# Aplicar el mismo preprocesamiento que a arco1.tif
|
47 |
processed_image = representativo("temp_image.tif")
|
48 |
-
# Redimensionar la imagen procesada para que coincida con las dimensiones esperadas por el modelo SOM
|
49 |
processed_image_resized = processed_image.reshape(1, -1)
|
50 |
return processed_image_resized
|
51 |
|
52 |
def get_fingerprint_type(winner):
|
53 |
-
|
54 |
-
labels = {0: "LL", 1: "RL", 2: "WH", 3: "AR"} # Mapa de etiquetas
|
55 |
fingerprint_type = labels[int(M[winner[0], winner[1]])]
|
56 |
return fingerprint_type
|
57 |
|
@@ -59,58 +49,52 @@ def get_fingerprint_type(winner):
|
|
59 |
async def predict_fingerprint_api(file: UploadFile = File(...)):
|
60 |
try:
|
61 |
contents = await file.read()
|
62 |
-
print(f"contenido:\n{contents}")
|
63 |
image = Image.open(BytesIO(contents)).convert('L')
|
64 |
-
print(f"imagen:\n{image}")
|
65 |
fingerprint_type = predict_fingerprint(image)
|
66 |
-
|
67 |
return {"prediction": fingerprint_type}
|
68 |
except Exception as e:
|
69 |
-
print("api")
|
70 |
raise HTTPException(status_code=500, detail=str(e))
|
71 |
|
72 |
def sobel(I):
|
73 |
-
m,n = I.shape
|
74 |
-
Gx = np.zeros([m-2,n-2],np.float32)
|
75 |
-
Gy = np.zeros([m-2,n-2],np.float32)
|
76 |
-
gx = [[-1,0,1],[
|
77 |
-
gy = [[1,2,1],[
|
78 |
-
for j in range(1,m-2):
|
79 |
-
for i in range(1,n-2):
|
80 |
-
Gx[j-1,i-1] = sum(
|
81 |
-
Gy[j-1,i-1] = sum(
|
82 |
-
return Gx,Gy
|
83 |
|
84 |
-
def medfilt2(G,d=3):
|
85 |
-
m,n = G.shape
|
86 |
-
temp = np.zeros([m+2*(d//2),n+2*(d//2)],np.float32)
|
87 |
-
salida = np.zeros([m,n],np.float32)
|
88 |
-
temp[1:m+1,1:n+1] = G
|
89 |
-
for i in range(1,m):
|
90 |
-
for j in range(1,n):
|
91 |
-
A = np.
|
92 |
-
salida[i-1,j-1] =
|
93 |
return salida
|
94 |
|
95 |
-
def orientacion(patron,w):
|
96 |
-
Gx,Gy = sobel(patron)
|
97 |
-
Gx = medfilt2(Gx)
|
98 |
-
Gy = medfilt2(Gy)
|
99 |
-
m,n = Gx.shape
|
100 |
-
mOrientaciones = np.zeros([m//w,n//w],np.float32)
|
101 |
for i in range(m//w):
|
102 |
for j in range(n//w):
|
103 |
-
YY = sum(
|
104 |
-
XX = sum(
|
105 |
-
|
106 |
-
#XX = sum(sum(Gx[i*w:(i+1)*w,0:1]**2-Gy[i*w:(i+1)*w,0:1]**2))
|
107 |
-
mOrientaciones[i,j] = (0.5*math.atan2(YY,XX) + math.pi/2.0)*(180.0/math.pi)
|
108 |
return mOrientaciones
|
109 |
|
110 |
def representativo(archivo):
|
111 |
im = Image.open(archivo).convert('L')
|
112 |
-
m,n = im.size
|
113 |
-
imarray = np.array(im,np.float32)
|
114 |
-
patron = imarray[1:m-1,1:n-1]
|
115 |
-
EE = orientacion(patron,14)
|
116 |
-
return np.asarray(EE).reshape(-1)
|
|
|
4 |
import pickle
|
5 |
from io import BytesIO
|
6 |
import math
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
|
|
24 |
[ 2., -1., -1., -1., 1., -1., -1., -1., -1., 2.]
|
25 |
])
|
26 |
|
|
|
27 |
def predict_fingerprint(image):
|
28 |
try:
|
|
|
29 |
processed_image = preprocess_image(image)
|
|
|
30 |
winner = som.winner(processed_image)
|
|
|
31 |
fingerprint_type = get_fingerprint_type(winner)
|
32 |
return fingerprint_type
|
33 |
except Exception as e:
|
|
|
34 |
raise HTTPException(status_code=500, detail=str(e))
|
35 |
|
36 |
def preprocess_image(image):
|
37 |
# Guardar la imagen en formato TIFF
|
38 |
image.save("temp_image.tif")
|
|
|
|
|
39 |
processed_image = representativo("temp_image.tif")
|
|
|
40 |
processed_image_resized = processed_image.reshape(1, -1)
|
41 |
return processed_image_resized
|
42 |
|
43 |
def get_fingerprint_type(winner):
|
44 |
+
labels = {0: "LL", 1: "RL", 2: "WH", 3: "AR"}
|
|
|
45 |
fingerprint_type = labels[int(M[winner[0], winner[1]])]
|
46 |
return fingerprint_type
|
47 |
|
|
|
49 |
async def predict_fingerprint_api(file: UploadFile = File(...)):
|
50 |
try:
|
51 |
contents = await file.read()
|
|
|
52 |
image = Image.open(BytesIO(contents)).convert('L')
|
|
|
53 |
fingerprint_type = predict_fingerprint(image)
|
|
|
54 |
return {"prediction": fingerprint_type}
|
55 |
except Exception as e:
|
|
|
56 |
raise HTTPException(status_code=500, detail=str(e))
|
57 |
|
58 |
def sobel(I):
|
59 |
+
m, n = I.shape
|
60 |
+
Gx = np.zeros([m-2, n-2], np.float32)
|
61 |
+
Gy = np.zeros([m-2, n-2], np.float32)
|
62 |
+
gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
|
63 |
+
gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
|
64 |
+
for j in range(1, m-2):
|
65 |
+
for i in range(1, n-2):
|
66 |
+
Gx[j-1, i-1] = np.sum(I[j-1:j+2, i-1:i+2] * gx)
|
67 |
+
Gy[j-1, i-1] = np.sum(I[j-1:j+2, i-1:i+2] * gy)
|
68 |
+
return Gx, Gy
|
69 |
|
70 |
+
def medfilt2(G, d=3):
|
71 |
+
m, n = G.shape
|
72 |
+
temp = np.zeros([m+2*(d//2), n+2*(d//2)], np.float32)
|
73 |
+
salida = np.zeros([m, n], np.float32)
|
74 |
+
temp[1:m+1, 1:n+1] = G
|
75 |
+
for i in range(1, m):
|
76 |
+
for j in range(1, n):
|
77 |
+
A = np.sort(temp[i-1:i+2, j-1:j+2].reshape(-1))
|
78 |
+
salida[i-1, j-1] = A[d+1]
|
79 |
return salida
|
80 |
|
81 |
+
def orientacion(patron, w):
|
82 |
+
Gx, Gy = sobel(patron)
|
83 |
+
Gx = medfilt2(Gx)
|
84 |
+
Gy = medfilt2(Gy)
|
85 |
+
m, n = Gx.shape
|
86 |
+
mOrientaciones = np.zeros([m//w, n//w], np.float32)
|
87 |
for i in range(m//w):
|
88 |
for j in range(n//w):
|
89 |
+
YY = np.sum(2 * Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1])
|
90 |
+
XX = np.sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2)
|
91 |
+
mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi/2.0) * (180.0 / math.pi)
|
|
|
|
|
92 |
return mOrientaciones
|
93 |
|
94 |
def representativo(archivo):
|
95 |
im = Image.open(archivo).convert('L')
|
96 |
+
m, n = im.size
|
97 |
+
imarray = np.array(im, np.float32)
|
98 |
+
patron = imarray[1:m-1, 1:n-1]
|
99 |
+
EE = orientacion(patron, 14)
|
100 |
+
return np.asarray(EE).reshape(-1)
|