Overglitch commited on
Commit
0eef1a6
1 Parent(s): a8e3ede

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -28
app.py CHANGED
@@ -1,9 +1,15 @@
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
  def load_model():
9
  with open('somlucuma.pkl', 'rb') as fid:
@@ -11,40 +17,36 @@ def load_model():
11
  MM = np.loadtxt('matrizMM.txt', delimiter=" ")
12
  return som, MM
13
 
14
- def sobel(I):
15
- m, n = I.shape
16
- Gx = np.zeros([m-2, n-2], np.float32)
17
- Gy = np.zeros([m-2, n-2], np.float32)
18
- gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
19
- gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
20
- for j in range(1, m-2):
21
- for i in range(1, n-2):
22
- Gx[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gx))
23
- Gy[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gy))
24
  return Gx, Gy
25
 
26
  def medfilt2(G, d=3):
27
- m, n = G.shape
28
- temp = np.zeros([m+2*(d//2), n+2*(d//2)], np.float32)
29
- salida = np.zeros([m, n], np.float32)
30
- temp[1:m+1, 1:n+1] = G
31
- for i in range(1, m):
32
- for j in range(1, n):
33
- A = np.asarray(temp[i-1:i+2, j-1:j+2]).reshape(-1)
34
- salida[i-1, j-1] = np.sort(A)[d+1]
35
- return salida
36
 
37
  def orientacion(patron, w):
38
  Gx, Gy = sobel(patron)
39
  Gx = medfilt2(Gx)
40
  Gy = medfilt2(Gy)
 
41
  m, n = Gx.shape
42
- mOrientaciones = np.zeros([m//w, n//w], np.float32)
43
- for i in range(m//w):
44
- for j in range(n//w):
45
- YY = sum(sum(2*Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1]))
46
- XX = sum(sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2))
47
- mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi)
 
 
 
 
 
 
48
  return mOrientaciones
49
 
50
  def representativo(imarray):
@@ -54,6 +56,9 @@ def representativo(imarray):
54
  EE = orientacion(patron, 14)
55
  return np.asarray(EE).reshape(-1)
56
 
 
 
 
57
  app = FastAPI()
58
 
59
  som, MM = load_model()
 
1
+ import cv2
 
2
  import numpy as np
3
  import pickle
 
4
  import math
5
+ from fastapi import FastAPI, File, UploadFile, HTTPException
6
+ from PIL import Image
7
+ from io import BytesIO
8
+ from pydantic import BaseModel
9
+ from typing import List
10
+ from scipy.ndimage import median_filter
11
+ from scipy.signal import convolve2d
12
+ from minisom import MiniSom
13
 
14
  def load_model():
15
  with open('somlucuma.pkl', 'rb') as fid:
 
17
  MM = np.loadtxt('matrizMM.txt', delimiter=" ")
18
  return som, MM
19
 
20
+ def sobel(patron):
21
+ gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32)
22
+ gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)
23
+
24
+ Gx = convolve2d(patron, gx, mode='valid')
25
+ Gy = convolve2d(patron, gy, mode='valid')
26
+
 
 
 
27
  return Gx, Gy
28
 
29
  def medfilt2(G, d=3):
30
+ return median_filter(G, size=d)
 
 
 
 
 
 
 
 
31
 
32
  def orientacion(patron, w):
33
  Gx, Gy = sobel(patron)
34
  Gx = medfilt2(Gx)
35
  Gy = medfilt2(Gy)
36
+
37
  m, n = Gx.shape
38
+ mOrientaciones = np.zeros((m // w, n // w), dtype=np.float32)
39
+
40
+ for i in range(m // w):
41
+ for j in range(n // w):
42
+ Gx_patch = Gx[i*w:(i+1)*w, j*w:(j+1)*w]
43
+ Gy_patch = Gy[i*w:(i+1)*w, j*w:(j+1)*w]
44
+
45
+ YY = np.sum(2 * Gx_patch * Gy_patch)
46
+ XX = np.sum(Gx_patch**2 - Gy_patch**2)
47
+
48
+ mOrientaciones[i, j] = (0.5 * np.arctan2(YY, XX) + np.pi / 2.0) * (18.0 / np.pi)
49
+
50
  return mOrientaciones
51
 
52
  def representativo(imarray):
 
56
  EE = orientacion(patron, 14)
57
  return np.asarray(EE).reshape(-1)
58
 
59
+ class InputData(BaseModel):
60
+ data: List[float]
61
+
62
  app = FastAPI()
63
 
64
  som, MM = load_model()