Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,41 +5,6 @@ import pickle
|
|
| 5 |
from io import BytesIO
|
| 6 |
import math
|
| 7 |
|
| 8 |
-
app = FastAPI()
|
| 9 |
-
|
| 10 |
-
som = load_model()
|
| 11 |
-
|
| 12 |
-
MM = np.array([
|
| 13 |
-
[ 0., -1., -1., -1., -1., 2., -1., -1., -1., 3.],
|
| 14 |
-
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 15 |
-
[-1., -1., -1., 1., -1., -1., -1., -1., -1., -1.],
|
| 16 |
-
[ 1., -1., -1., -1., -1., -1., -1., -1., -1., 0.],
|
| 17 |
-
[-1., -1., -1., -1., 1., -1., -1., -1., -1., -1.],
|
| 18 |
-
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 19 |
-
[ 3., -1., -1., -1., -1., -1., -1., -1., -1., 3.],
|
| 20 |
-
[-1., -1., -1., 0., -1., -1., 3., -1., -1., -1.],
|
| 21 |
-
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 22 |
-
[ 2., -1., -1., -1., 1., -1., -1., -1., -1., 2.]
|
| 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)
|
|
@@ -79,4 +44,40 @@ def representativo(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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
import math
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def load_model():
|
| 9 |
with open('som.pkl', 'rb') as fid:
|
| 10 |
som = pickle.load(fid)
|
|
|
|
| 44 |
m, n = imarray.shape
|
| 45 |
patron = imarray[1:m-1, 1:n-1]
|
| 46 |
EE = orientacion(patron, 14)
|
| 47 |
+
return np.asarray(EE).reshape(-1)
|
| 48 |
+
|
| 49 |
+
app = FastAPI()
|
| 50 |
+
|
| 51 |
+
som = load_model()
|
| 52 |
+
|
| 53 |
+
MM = np.array([
|
| 54 |
+
[ 0., -1., -1., -1., -1., 2., -1., -1., -1., 3.],
|
| 55 |
+
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 56 |
+
[-1., -1., -1., 1., -1., -1., -1., -1., -1., -1.],
|
| 57 |
+
[ 1., -1., -1., -1., -1., -1., -1., -1., -1., 0.],
|
| 58 |
+
[-1., -1., -1., -1., 1., -1., -1., -1., -1., -1.],
|
| 59 |
+
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 60 |
+
[ 3., -1., -1., -1., -1., -1., -1., -1., -1., 3.],
|
| 61 |
+
[-1., -1., -1., 0., -1., -1., 3., -1., -1., -1.],
|
| 62 |
+
[-1., -1., -1., -1., -1., -1., -1., -1., -1., -1.],
|
| 63 |
+
[ 2., -1., -1., -1., 1., -1., -1., -1., -1., 2.]
|
| 64 |
+
])
|
| 65 |
+
|
| 66 |
+
@app.post("/predict/")
|
| 67 |
+
async def predict_fingerprint_api(file: UploadFile = File(...)):
|
| 68 |
+
try:
|
| 69 |
+
contents = await file.read()
|
| 70 |
+
image = Image.open(BytesIO(contents)).convert('L')
|
| 71 |
+
image = np.asarray(image)
|
| 72 |
+
print(f"ARRAY{image.size}:\n\n\n{image}")
|
| 73 |
+
image = np.array(image.array).reshape(256, 256, 1)
|
| 74 |
+
representative_data = representativo(image)
|
| 75 |
+
representative_data = representative_data.reshape(1, -1)
|
| 76 |
+
|
| 77 |
+
w = som.winner(representative_data)
|
| 78 |
+
prediction = MM[w]
|
| 79 |
+
|
| 80 |
+
return {"prediction": prediction}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 83 |
+
|