Overglitch commited on
Commit
06a5287
·
verified ·
1 Parent(s): 177f456

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -60
app.py CHANGED
@@ -1,63 +1,12 @@
1
- import pickle
 
2
  import numpy as np
3
- from fastapi import FastAPI, HTTPException
4
- from pydantic import BaseModel
5
- from typing import List
6
  import math
7
 
8
- class InputData(BaseModel):
9
- array: List[List[int]]
10
-
11
  app = FastAPI()
12
 
13
- # Cargar el modelo SOM
14
- def load_model():
15
- with open('som.pkl', 'rb') as fid:
16
- som = pickle.load(fid)
17
- return som
18
-
19
- def sobel(I):
20
- m, n = I.shape
21
- Gx = np.zeros([m, n], np.float32)
22
- Gy = np.zeros([m, n], np.float32)
23
- gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
24
- gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
25
- for j in range(1, m-1):
26
- for i in range(1, n-1):
27
- Gx[j, i] = np.sum(I[j-1:j+2, i-1:i+2] * gx)
28
- Gy[j, i] = np.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.pad(G, pad_width=d//2, mode='constant', constant_values=0)
34
- salida = np.zeros([m, n], np.float32)
35
- for i in range(m):
36
- for j in range(n):
37
- A = temp[i:i+d, j:j+d].flatten()
38
- salida[i, j] = np.median(A)
39
- return salida
40
-
41
- def orientacion(patron, w):
42
- Gx, Gy = sobel(patron)
43
- Gx = medfilt2(Gx)
44
- Gy = medfilt2(Gy)
45
- m, n = Gx.shape
46
- mOrientaciones = np.zeros([m//w, n//w], np.float32)
47
- for i in range(m//w):
48
- for j in range(n//w):
49
- YY = np.sum(2 * Gx[i*w:(i+1)*w, j*w:(j+1)*w] * Gy[i*w:(i+1)*w, j*w:(j+1)*w])
50
- XX = np.sum(Gx[i*w:(i+1)*w, j*w:(j+1)*w]**2 - Gy[i*w:(i+1)*w, j*w:(j+1)*w]**2)
51
- mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi)
52
- return mOrientaciones
53
-
54
- def representativo(imarray):
55
- imarray = np.squeeze(imarray)
56
- m, n = imarray.shape
57
- patron = imarray[1:m-1, 1:n-1]
58
- EE = orientacion(patron, 14)
59
- return np.asarray(EE).reshape(-1)
60
-
61
  som = load_model()
62
 
63
  MM = np.array([
@@ -74,16 +23,60 @@ MM = np.array([
74
  ])
75
 
76
  @app.post("/predict/")
77
- async def predict(data: InputData):
78
  try:
79
- print(data.array)
80
- input_data = np.array(data.array).reshape(256, 256)
81
- representative_data = representativo(input_data)
 
 
 
82
  representative_data = representative_data.reshape(1, -1)
83
 
84
  w = som.winner(representative_data)
85
  prediction = MM[w]
86
 
87
- return {"prediction": prediction.tolist()}
88
  except Exception as e:
89
  raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  som = load_model()
11
 
12
  MM = np.array([
 
23
  ])
24
 
25
  @app.post("/predict/")
26
+ async def predict_fingerprint_api(file: UploadFile = File(...)):
27
  try:
28
+ contents = await file.read()
29
+ image = Image.open(BytesIO(contents)).convert('L')
30
+ image = np.asarray(image)
31
+ print(f"ARRAY{image.size}:\n\n\n{image}")
32
+ image = np.array(image.array).reshape(256, 256, 1)
33
+ representative_data = representativo(image)
34
  representative_data = representative_data.reshape(1, -1)
35
 
36
  w = som.winner(representative_data)
37
  prediction = MM[w]
38
 
39
+ return {"prediction": prediction}
40
  except Exception as e:
41
  raise HTTPException(status_code=500, detail=str(e))
42
+
43
+ def load_model():
44
+ with open('som.pkl', 'rb') as fid:
45
+ som = pickle.load(fid)
46
+ return som
47
+
48
+ def sobel(I):
49
+ m, n = I.shape
50
+ Gx = np.zeros([m-2, n-2], np.float32)
51
+ Gy = np.zeros([m-2, n-2], np.float32)
52
+ gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
53
+ gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
54
+ for j in range(1, m-2):
55
+ for i in range(1, n-2):
56
+ Gx[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gx))
57
+ Gy[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gy))
58
+ return Gx, Gy
59
+
60
+ def medfilt2(G, d=3):
61
+ temp[1:m+1, 1:n+1] = G
62
+ for i in range(1, m):
63
+ for j in range(1, n):
64
+ A = np.asarray(temp[i-1:i+2, j-1:j+2]).reshape(-1)
65
+ salida[i-1, j-1] = np.sort(A)[d+1]
66
+ return salida
67
+
68
+ def orientacion(patron, w):
69
+ mOrientaciones = np.zeros([m//w, n//w], np.float32)
70
+ for i in range(m//w):
71
+ for j in range(n//w):
72
+ YY = sum(sum(2*Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1]))
73
+ XX = sum(sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2))
74
+ mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi)
75
+ return mOrientaciones
76
+
77
+ def representativo(imarray):
78
+ imarray = np.squeeze(imarray)
79
+ m, n = imarray.shape
80
+ patron = imarray[1:m-1, 1:n-1]
81
+ EE = orientacion(patron, 14)
82
+ return np.asarray(EE).reshape(-1)