Spaces:
Sleeping
Sleeping
| import pickle | |
| from minisom import MiniSom | |
| import numpy as np | |
| import json | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import List | |
| import math | |
| class InputData(BaseModel): | |
| data: List[float] # Lista de características numéricas (floats) | |
| app = FastAPI() | |
| # Função para construir o modelo manualmente | |
| def build_model(): | |
| with open('somcancer.pkl', 'rb') as fid: | |
| somhuella = pickle.load(fid) | |
| MM = np.loadtxt('matrizMM.txt', delimiter=" ") | |
| return somhuella,MM | |
| #with open('label_map.json', 'r') as json_file: | |
| # loaded_label_map_str_keys = json.load(json_file) | |
| # Convertendo as chaves de volta para tuplas | |
| #loaded_label_map = {eval(k): v for k, v in loaded_label_map_str_keys.items()} | |
| #return somecoli, loaded_label_map | |
| def sobel(I): | |
| m, n = I.shape | |
| Gx = np.zeros([m-2, n-2], np.float32) | |
| Gy = np.zeros([m-2, n-2], np.float32) | |
| gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]] | |
| gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]] | |
| for j in range(1, m-2): | |
| for i in range(1, n-2): | |
| Gx[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gx)) | |
| Gy[j-1, i-1] = sum(sum(I[j-1:j+2, i-1:i+2] * gy)) | |
| return Gx, Gy | |
| def medfilt2(G, d=3): | |
| m, n = G.shape | |
| temp = np.zeros([m+2*(d//2), n+2*(d//2)], np.float32) | |
| salida = np.zeros([m, n], np.float32) | |
| temp[1:m+1, 1:n+1] = G | |
| for i in range(1, m): | |
| for j in range(1, n): | |
| A = np.asarray(temp[i-1:i+2, j-1:j+2]).reshape(-1) | |
| salida[i-1, j-1] = np.sort(A)[d+1] | |
| return salida | |
| 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], np.float32) | |
| for i in range(m//w): | |
| for j in range(n//w): | |
| YY = sum(sum(2*Gx[i*w:(i+1)*w, j:j+1] * Gy[i*w:(i+1)*w, j:j+1])) | |
| XX = sum(sum(Gx[i*w:(i+1)*w, j:j+1]**2 - Gy[i*w:(i+1)*w, j:j+1]**2)) | |
| mOrientaciones[i, j] = (0.5 * math.atan2(YY, XX) + math.pi / 2.0) * (180.0 / math.pi) | |
| return mOrientaciones | |
| def representativo(imarray): | |
| imarray = np.squeeze(imarray) # Remover a dimensão extra do canal | |
| m, n = imarray.shape | |
| patron = imarray[1:m-1, 1:n-1] # de 256x256 a 254x254 | |
| EE = orientacion(patron, 14) # retorna EE de 18x18 | |
| return np.asarray(EE).reshape(-1) | |
| som, MM = build_model() # Construir o modelo ao iniciar a aplicação | |
| # Rota de previsão | |
| async def predict(data: InputData): | |
| print(f"Data: {data}") | |
| global som | |
| global MM | |
| try: | |
| # Converter a lista de entrada para um array de NumPy para a previsão | |
| input_data = np.array(data.data).reshape(256, 256, 1) # Assumindo que a entrada é uma imagem de 256x256 com 1 canal | |
| representative_data = representativo(input_data) | |
| representative_data = representative_data.reshape(1, -1) # Reformatar para (1, num_features) | |
| w = som.winner(representative_data) | |
| prediction = MM[w] | |
| return {"prediction": prediction} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) |