Julia-Amadio commited on
Commit ·
0f0e135
1
Parent(s): 3e4dc6c
Fazendo interface swagger exibir vetor.
Browse files- feature_extractor_single.py +6 -7
- main.py +9 -6
feature_extractor_single.py
CHANGED
|
@@ -35,20 +35,19 @@ class FeatureExtractor:
|
|
| 35 |
#Função principal
|
| 36 |
def process_single_image(image_path: str, output_dir: str = "processed"):
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
- Extração de features ConvNeXt (sem segmentação)
|
| 40 |
"""
|
| 41 |
#1) Extração de features
|
| 42 |
extractor = FeatureExtractor()
|
| 43 |
# Passa o CAMINHO ORIGINAL da imagem (ex: /tmp/temp_foto.jpg)
|
| 44 |
features = extractor.extract_convnext(image_path)
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
feat_path = os.path.join(output_dir, "features_single.npy")
|
| 48 |
-
np.save(feat_path, features)
|
| 49 |
-
print(f"Vetor de características salvo em: {feat_path}")
|
| 50 |
|
| 51 |
-
return
|
| 52 |
|
| 53 |
#Execução direta
|
| 54 |
if __name__ == "__main__":
|
|
|
|
| 35 |
#Função principal
|
| 36 |
def process_single_image(image_path: str, output_dir: str = "processed"):
|
| 37 |
"""
|
| 38 |
+
Extração de features ConvNeXt (sem segmentação)
|
|
|
|
| 39 |
"""
|
| 40 |
#1) Extração de features
|
| 41 |
extractor = FeatureExtractor()
|
| 42 |
# Passa o CAMINHO ORIGINAL da imagem (ex: /tmp/temp_foto.jpg)
|
| 43 |
features = extractor.extract_convnext(image_path)
|
| 44 |
|
| 45 |
+
#NÃO PRECISAMOS MAIS SALVAR O ARQUIVO .NPY
|
| 46 |
+
#feat_path = os.path.join(output_dir, "features_single.npy")
|
| 47 |
+
#np.save(feat_path, features)
|
| 48 |
+
#print(f"Vetor de características salvo em: {feat_path}")
|
| 49 |
|
| 50 |
+
return features #RETORNA O ARRAY DE DADOS
|
| 51 |
|
| 52 |
#Execução direta
|
| 53 |
if __name__ == "__main__":
|
main.py
CHANGED
|
@@ -42,22 +42,25 @@ async def verify_token(credentials: HTTPAuthorizationCredentials = Depends(secur
|
|
| 42 |
#2. Adiciona 'Depends(verify_token)' para proteger endpoint
|
| 43 |
async def extract_features(file: UploadFile = File(...), token: str = Depends(verify_token)):
|
| 44 |
"""
|
| 45 |
-
Endpoint que recebe uma imagem, valida o token
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
#A única pasta em um contêiner Docker que tem permissão de escrita garantida é a pasta /tmp
|
| 49 |
#Precisamos direcionar todos os arquivos temporários para lá
|
| 50 |
-
temp_path = f"/tmp/temp_{file.filename}" #<-- Adiciona /tmp/
|
| 51 |
with open(temp_path, "wb") as buffer:
|
| 52 |
shutil.copyfileobj(file.file, buffer)
|
| 53 |
|
| 54 |
-
#Processa a imagem (
|
| 55 |
-
|
| 56 |
|
| 57 |
#Remove imagem temporária
|
| 58 |
os.remove(temp_path)
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@app.get("/health", status_code=status.HTTP_200_OK)
|
| 63 |
async def health_check():
|
|
|
|
| 42 |
#2. Adiciona 'Depends(verify_token)' para proteger endpoint
|
| 43 |
async def extract_features(file: UploadFile = File(...), token: str = Depends(verify_token)):
|
| 44 |
"""
|
| 45 |
+
Endpoint que recebe uma imagem, valida o token
|
| 46 |
+
e retorna vetor de características extraído.
|
| 47 |
"""
|
| 48 |
#A única pasta em um contêiner Docker que tem permissão de escrita garantida é a pasta /tmp
|
| 49 |
#Precisamos direcionar todos os arquivos temporários para lá
|
|
|
|
| 50 |
with open(temp_path, "wb") as buffer:
|
| 51 |
shutil.copyfileobj(file.file, buffer)
|
| 52 |
|
| 53 |
+
#Processa a imagem (agora retorna o array de dados)
|
| 54 |
+
features_array = process_single_image(temp_path, output_dir="/tmp")
|
| 55 |
|
| 56 |
#Remove imagem temporária
|
| 57 |
os.remove(temp_path)
|
| 58 |
|
| 59 |
+
#Converte o array Numpy (ex: (1536,)) em uma lista Python padrão
|
| 60 |
+
features_list = features_array.tolist()
|
| 61 |
+
|
| 62 |
+
#Retorna o vetor de features REAL no JSON
|
| 63 |
+
return {"features": features_list}
|
| 64 |
|
| 65 |
@app.get("/health", status_code=status.HTTP_200_OK)
|
| 66 |
async def health_check():
|