File size: 2,413 Bytes
c27272e
 
 
542b39e
 
 
 
 
 
c27272e
 
 
 
542b39e
c27272e
 
 
542b39e
c27272e
542b39e
 
 
 
 
 
 
 
 
 
c27272e
e2a99d1
542b39e
 
e2a99d1
542b39e
 
e2a99d1
542b39e
c27272e
 
542b39e
c27272e
 
542b39e
 
 
e2a99d1
542b39e
 
e2a99d1
542b39e
 
 
 
c27272e
542b39e
 
c27272e
542b39e
e2a99d1
542b39e
e2a99d1
 
542b39e
 
 
 
 
 
 
 
 
 
 
 
 
 
c27272e
542b39e
c27272e
542b39e
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import pickle
from minisom import MiniSom
import numpy as np
import cv2

import urllib.request
import uuid

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List

class InputData(BaseModel):
  data: str  # image url

app = FastAPI()

# Funci贸n para construir el modelo manualmente
def build_model():
  with open('somlucuma.pkl', 'rb') as fid:
    somecoli = pickle.load(fid)
  MM = np.loadtxt('matrizMM.txt', delimiter=" ")
  return somecoli,MM

som,MM = build_model()  # Construir el modelo al iniciar la aplicaci贸n


from scipy.ndimage import median_filter
from scipy.signal import convolve2d

def sobel(patron):
  gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32)
  gy = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], dtype=np.float32)

  Gx = convolve2d(patron, gx, mode='valid')
  Gy = convolve2d(patron, gy, mode='valid')

  return Gx, Gy

def medfilt2(G, d=3):
  return median_filter(G, size=d)

def orientacion(patron, w):
  Gx, Gy = sobel(patron)
  Gx = medfilt2(Gx)
  Gy = medfilt2(Gy)

  m, n = Gx.shape
  mOrientaciones = np.zeros((m // w, n // w), dtype=np.float32)

  for i in range(m // w):
    for j in range(n // w):
      Gx_patch = Gx[i*w:(i+1)*w, j*w:(j+1)*w]
      Gy_patch = Gy[i*w:(i+1)*w, j*w:(j+1)*w]

      YY = np.sum(2 * Gx_patch * Gy_patch)
      XX = np.sum(Gx_patch**2 - Gy_patch**2)

      mOrientaciones[i, j] = (0.5 * np.arctan2(YY, XX) + np.pi / 2.0) * (18.0 / np.pi)

  return mOrientaciones

def redimensionar(img, h, v):
  return cv2.resize(img, (h, v), interpolation=cv2.INTER_AREA)


def prediction(som, imgurl):
  archivo = f"/tmp/test-{uuid.uuid4()}.jpg"
  urllib.request.urlretrieve(imgurl, archivo)
  Xtest = redimensionar(cv2.imread(archivo),256,256)
  Xtest = np.array(Xtest)
  Xtest = cv2.cvtColor(Xtest, cv2.COLOR_BGR2GRAY)

  orientaciones = orientacion(Xtest, w=14)
  Xtest = Xtest.astype('float32') / 255.0
  orientaciones = orientaciones.reshape(-1)
  return som.winner(orientaciones)

# Ruta de predicci贸n
@app.post("/predict/")
async def predict(data: InputData):
  print(f"Data: {data}")
  global som
  global MM
  try:
    # Convertir la lista de entrada a un array de NumPy para la predicci贸n
    imgurl = data.data
    print(type(data.data))
    w = prediction(som, imgurl)
    return {"prediction": MM[w]}
  except Exception as e:
    raise HTTPException(status_code=500, detail=str(e))