Overglitch commited on
Commit
dfbd2d8
·
verified ·
1 Parent(s): 0d13d96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -68
app.py CHANGED
@@ -1,74 +1,32 @@
1
- from fastapi import FastAPI, File, UploadFile, HTTPException
2
- from PIL import Image
3
- import numpy as np
4
  import pickle
5
- from io import BytesIO
 
 
 
 
6
  import math
7
 
8
- app = FastAPI()
9
-
10
- # Cargar el modelo SOM previamente entrenado
11
- with open("som.pkl", "rb") as tf:
12
- som = pickle.load(tf)
13
-
14
- M = np.array([
15
- [ 0., -1., -1., -1., -1., 2., -1., -1., -1., 3.],
16
- [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
17
- [-1., -1., -1., 1., -1., -1., -1., -1., -1., -1.],
18
- [ 1., -1., -1., -1., -1., -1., -1., -1., -1., 0.],
19
- [-1., -1., -1., -1., 1., -1., -1., -1., -1., -1.],
20
- [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
21
- [ 3., -1., -1., -1., -1., -1., -1., -1., -1., 3.],
22
- [-1., -1., -1., 0., -1., -1., 3., -1., -1., -1.],
23
- [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
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
- processed_image = representativo(image)
39
- processed_image_resized = processed_image.reshape(1, -1)
40
- print(f"\n\n\nXD:{processed_image.size}\t{processed_image_resized.size}")
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
 
48
- @app.post("/predict/")
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
- image.save("temp_image.tif")
54
- image = Image.open("temp_image.tif")
55
- image.resize((256,256))
56
- print(f"\n\n\n\nSIZE:{image.size}\t{image}")
57
- fingerprint_type = predict_fingerprint(image)
58
- return {"prediction": fingerprint_type}
59
- except Exception as e:
60
- raise HTTPException(status_code=500, detail=str(e))
61
 
62
  def sobel(I):
63
  m, n = I.shape
64
  Gx = np.zeros([m-2, n-2], np.float32)
65
  Gy = np.zeros([m-2, n-2], np.float32)
66
- gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
67
- gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
68
  for j in range(1, m-2):
69
  for i in range(1, n-2):
70
- Gx[j-1, i-1] = np.sum(I[j-1:j+2, i-1:i+2] * gx)
71
- Gy[j-1, i-1] = np.sum(I[j-1:j+2, i-1:i+2] * gy)
72
  return Gx, Gy
73
 
74
  def medfilt2(G, d=3):
@@ -78,8 +36,8 @@ def medfilt2(G, d=3):
78
  temp[1:m+1, 1:n+1] = G
79
  for i in range(1, m):
80
  for j in range(1, n):
81
- A = np.sort(temp[i-1:i+2, j-1:j+2].reshape(-1))
82
- salida[i-1, j-1] = A[d+1]
83
  return salida
84
 
85
  def orientacion(patron, w):
@@ -90,14 +48,43 @@ def orientacion(patron, w):
90
  mOrientaciones = np.zeros([m//w, n//w], np.float32)
91
  for i in range(m//w):
92
  for j in range(n//w):
93
- YY = np.sum(2 * Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1])
94
- XX = np.sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2)
95
- mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi/2.0) * (180.0 / math.pi)
96
  return mOrientaciones
97
 
98
- def representativo(im):
99
- m, n = im.size
100
- imarray = np.array(im, np.float32)
101
  patron = imarray[1:m-1, 1:n-1]
102
  EE = orientacion(patron, 14)
103
- return np.asarray(EE).reshape(-1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pickle
2
+ from minisom import MiniSom
3
+ import numpy as np
4
+ from fastapi import FastAPI, HTTPException
5
+ from pydantic import BaseModel
6
+ from typing import List
7
  import math
8
 
9
+ class InputData(BaseModel):
10
+ array: List[List[int]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ app = FastAPI()
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Cargar el modelo SOM
15
+ def load_model():
16
+ with open('som.pkl', 'rb') as fid:
17
+ somecoli = pickle.load(fid)
18
+ return somecoli
 
 
 
 
 
 
 
 
19
 
20
  def sobel(I):
21
  m, n = I.shape
22
  Gx = np.zeros([m-2, n-2], np.float32)
23
  Gy = np.zeros([m-2, n-2], np.float32)
24
+ gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
25
+ gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
26
  for j in range(1, m-2):
27
  for i in range(1, n-2):
28
+ Gx[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gx))
29
+ Gy[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gy))
30
  return Gx, Gy
31
 
32
  def medfilt2(G, d=3):
 
36
  temp[1:m+1, 1:n+1] = G
37
  for i in range(1, m):
38
  for j in range(1, n):
39
+ A = np.asarray(temp[i-1:i+2, j-1:j+2]).reshape(-1)
40
+ salida[i-1, j-1] = np.sort(A)[d+1]
41
  return salida
42
 
43
  def orientacion(patron, w):
 
48
  mOrientaciones = np.zeros([m//w, n//w], np.float32)
49
  for i in range(m//w):
50
  for j in range(n//w):
51
+ YY = sum(sum(2*Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1]))
52
+ XX = sum(sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2))
53
+ mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi)
54
  return mOrientaciones
55
 
56
+ def representativo(imarray):
57
+ imarray = np.squeeze(imarray)
58
+ m, n = imarray.shape
59
  patron = imarray[1:m-1, 1:n-1]
60
  EE = orientacion(patron, 14)
61
+ return np.asarray(EE).reshape(-1)
62
+
63
+ som = load_model()
64
+
65
+ MM = np.array([
66
+ [ 0., -1., -1., -1., -1., 2., -1., -1., -1., 3.],
67
+ [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
68
+ [-1., -1., -1., 1., -1., -1., -1., -1., -1., -1.],
69
+ [ 1., -1., -1., -1., -1., -1., -1., -1., -1., 0.],
70
+ [-1., -1., -1., -1., 1., -1., -1., -1., -1., -1.],
71
+ [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
72
+ [ 3., -1., -1., -1., -1., -1., -1., -1., -1., 3.],
73
+ [-1., -1., -1., 0., -1., -1., 3., -1., -1., -1.],
74
+ [-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
75
+ [ 2., -1., -1., -1., 1., -1., -1., -1., -1., 2.]
76
+ ])
77
+
78
+ @app.post("/predict/")
79
+ async def predict(data: InputData):
80
+ try:
81
+ input_data = np.array(data.array).reshape(256, 256, 1)
82
+ representative_data = representativo(input_data)
83
+ representative_data = representative_data.reshape(1, -1)
84
+
85
+ w = som.winner(representative_data)
86
+ prediction = MM[w]
87
+
88
+ return {"prediction": prediction}
89
+ except Exception as e:
90
+ raise HTTPException(status_code=500, detail=str(e))