Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import pickle
|
2 |
-
from minisom import MiniSom
|
3 |
import numpy as np
|
4 |
from fastapi import FastAPI, HTTPException
|
5 |
from pydantic import BaseModel
|
@@ -14,30 +13,29 @@ app = FastAPI()
|
|
14 |
# Cargar el modelo SOM
|
15 |
def load_model():
|
16 |
with open('som.pkl', 'rb') as fid:
|
17 |
-
|
18 |
-
return
|
19 |
|
20 |
def sobel(I):
|
21 |
m, n = I.shape
|
22 |
-
Gx = np.zeros([m
|
23 |
-
Gy = np.zeros([m
|
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-
|
27 |
-
for i in range(1, n-
|
28 |
-
Gx[j
|
29 |
-
Gy[j
|
30 |
return Gx, Gy
|
31 |
|
32 |
def medfilt2(G, d=3):
|
33 |
m, n = G.shape
|
34 |
-
temp = np.
|
35 |
salida = np.zeros([m, n], np.float32)
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
salida[i-1, j-1] = np.sort(A)[d+1]
|
41 |
return salida
|
42 |
|
43 |
def orientacion(patron, w):
|
@@ -48,8 +46,8 @@ 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(
|
52 |
-
XX = sum(
|
53 |
mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi)
|
54 |
return mOrientaciones
|
55 |
|
@@ -78,13 +76,13 @@ MM = np.array([
|
|
78 |
@app.post("/predict/")
|
79 |
async def predict(data: InputData):
|
80 |
try:
|
81 |
-
input_data = np.array(data.array).reshape(256, 256
|
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))
|
|
|
1 |
import pickle
|
|
|
2 |
import numpy as np
|
3 |
from fastapi import FastAPI, HTTPException
|
4 |
from pydantic import BaseModel
|
|
|
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):
|
|
|
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 |
|
|
|
76 |
@app.post("/predict/")
|
77 |
async def predict(data: InputData):
|
78 |
try:
|
79 |
+
input_data = np.array(data.array).reshape(256, 256)
|
80 |
representative_data = representativo(input_data)
|
81 |
representative_data = representative_data.reshape(1, -1)
|
82 |
|
83 |
w = som.winner(representative_data)
|
84 |
prediction = MM[w]
|
85 |
|
86 |
+
return {"prediction": prediction.tolist()}
|
87 |
except Exception as e:
|
88 |
raise HTTPException(status_code=500, detail=str(e))
|