Julia-Amadio
commited on
Commit
·
0611c9b
1
Parent(s):
5f430b2
tentando sem segmentação
Browse files- Dockerfile +1 -2
- download_models.py +1 -20
- feature_extractor_single.py +5 -41
- requirements.txt +0 -3
Dockerfile
CHANGED
|
@@ -6,8 +6,7 @@ WORKDIR /app
|
|
| 6 |
|
| 7 |
#Instala dependências de sistema
|
| 8 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 9 |
-
build-essential \
|
| 10 |
-
libgl1 && \
|
| 11 |
rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
#Copia o requirements.txt primeiro
|
|
|
|
| 6 |
|
| 7 |
#Instala dependências de sistema
|
| 8 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 9 |
+
build-essential && \
|
|
|
|
| 10 |
rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
#Copia o requirements.txt primeiro
|
download_models.py
CHANGED
|
@@ -1,25 +1,6 @@
|
|
| 1 |
-
import os
|
| 2 |
import sys
|
| 3 |
from huggingface_hub import snapshot_download
|
| 4 |
-
from transparent_background import Remover
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
#1. Baixa o modelo ConvNext
|
| 8 |
print("Downloading ConvNext-LARGE model (snapshot)...")
|
| 9 |
snapshot_download(repo_id='facebook/convnext-large-224-22k-1k')
|
| 10 |
-
print("ConvNext-LARGE model downloaded.")
|
| 11 |
-
|
| 12 |
-
#2. Baixa o modelo backgroundremover
|
| 13 |
-
print("Downloading backgroundremover model (u2net)...")
|
| 14 |
-
#Usa o truque da imagem falsa de 1x1 para forçar o download
|
| 15 |
-
img = Image.new('RGB', (1, 1), 'black')
|
| 16 |
-
try:
|
| 17 |
-
remover = Remover()
|
| 18 |
-
remover.process(img)
|
| 19 |
-
except Exception as e:
|
| 20 |
-
# A execução vai falhar na imagem 1x1, o que é esperado.
|
| 21 |
-
# Nós não nos importamos, contanto que o download tenha sido disparado.
|
| 22 |
-
print(f"Disparado o download (ignora erro esperado: {e})")
|
| 23 |
-
pass
|
| 24 |
-
|
| 25 |
-
print("Model downloads finished.")
|
|
|
|
|
|
|
| 1 |
import sys
|
| 2 |
from huggingface_hub import snapshot_download
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
print("Downloading ConvNext-LARGE model (snapshot)...")
|
| 5 |
snapshot_download(repo_id='facebook/convnext-large-224-22k-1k')
|
| 6 |
+
print("ConvNext-LARGE model downloaded.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
feature_extractor_single.py
CHANGED
|
@@ -2,40 +2,9 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
from transparent_background import Remover
|
| 6 |
from torchvision import transforms
|
| 7 |
from transformers import ConvNextImageProcessor, ConvNextForImageClassification
|
| 8 |
|
| 9 |
-
#Função de segmentação + fundo branco
|
| 10 |
-
# Mude esta função
|
| 11 |
-
def segment_and_whitebg(image_path: str, save_dir: str = "processed") -> str:
|
| 12 |
-
"""
|
| 13 |
-
Remove o fundo de uma folha e substitui por branco
|
| 14 |
-
Retorna o caminho da imagem salva
|
| 15 |
-
"""
|
| 16 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 17 |
-
|
| 18 |
-
#Remove fundo
|
| 19 |
-
input_img = Image.open(image_path).convert("RGB")
|
| 20 |
-
remover = Remover() #Inicializa o modelo
|
| 21 |
-
output = remover.process(input_img, type='rgba') #Processa e retorna PIL
|
| 22 |
-
|
| 23 |
-
#Adiciona fundo branco
|
| 24 |
-
bg = Image.new("RGBA", output.size, (255, 255, 255, 255))
|
| 25 |
-
whitebg = Image.alpha_composite(bg, output).convert("RGB")
|
| 26 |
-
|
| 27 |
-
#Caminho da imagem processada
|
| 28 |
-
base_name = os.path.basename(image_path)
|
| 29 |
-
name, _ = os.path.splitext(base_name)
|
| 30 |
-
save_path = os.path.join(save_dir, f"{name}_whitebg.jpg")
|
| 31 |
-
|
| 32 |
-
#Salva
|
| 33 |
-
whitebg.save(save_path, "JPEG", quality=95)
|
| 34 |
-
print(f"Imagem segmentada e salva em: {save_path}")
|
| 35 |
-
|
| 36 |
-
return save_path
|
| 37 |
-
|
| 38 |
-
|
| 39 |
#Classe para extração de features (ConvNeXt)
|
| 40 |
class FeatureExtractor:
|
| 41 |
def __init__(self, device=None):
|
|
@@ -63,29 +32,24 @@ class FeatureExtractor:
|
|
| 63 |
print(f"Vetor de características extraído com shape: {features_np.shape}")
|
| 64 |
return features_np
|
| 65 |
|
| 66 |
-
|
| 67 |
#Função principal
|
| 68 |
def process_single_image(image_path: str, output_dir: str = "processed"):
|
| 69 |
"""
|
| 70 |
Faz todo o pipeline em uma única imagem:
|
| 71 |
-
-
|
| 72 |
-
- Extração de features ConvNeXt
|
| 73 |
"""
|
| 74 |
-
#1)
|
| 75 |
-
processed_path = segment_and_whitebg(image_path, save_dir=output_dir)
|
| 76 |
-
|
| 77 |
-
#2) Extração de features
|
| 78 |
extractor = FeatureExtractor()
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
feat_path = os.path.join(output_dir, "features_single.npy")
|
| 83 |
np.save(feat_path, features)
|
| 84 |
print(f"Vetor de características salvo em: {feat_path}")
|
| 85 |
|
| 86 |
return feat_path
|
| 87 |
|
| 88 |
-
|
| 89 |
#Execução direta
|
| 90 |
if __name__ == "__main__":
|
| 91 |
#Caminho da imagem
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from torchvision import transforms
|
| 6 |
from transformers import ConvNextImageProcessor, ConvNextForImageClassification
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
#Classe para extração de features (ConvNeXt)
|
| 9 |
class FeatureExtractor:
|
| 10 |
def __init__(self, device=None):
|
|
|
|
| 32 |
print(f"Vetor de características extraído com shape: {features_np.shape}")
|
| 33 |
return features_np
|
| 34 |
|
|
|
|
| 35 |
#Função principal
|
| 36 |
def process_single_image(image_path: str, output_dir: str = "processed"):
|
| 37 |
"""
|
| 38 |
Faz todo o pipeline em uma única imagem:
|
| 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 |
+
#2) Salvar o vetor de características
|
| 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 feat_path
|
| 52 |
|
|
|
|
| 53 |
#Execução direta
|
| 54 |
if __name__ == "__main__":
|
| 55 |
#Caminho da imagem
|
requirements.txt
CHANGED
|
@@ -13,14 +13,11 @@ torchvision==0.17.2+cpu
|
|
| 13 |
-f https://download.pytorch.org/whl/torch_stable.html
|
| 14 |
|
| 15 |
transformers==4.39.3
|
| 16 |
-
timm
|
| 17 |
|
| 18 |
# =====================================
|
| 19 |
# Processamento de imagem
|
| 20 |
# =====================================
|
| 21 |
pillow==10.3.0
|
| 22 |
-
transparent-background==1.3.4
|
| 23 |
-
onnxruntime==1.19.2
|
| 24 |
numpy==1.26.4
|
| 25 |
|
| 26 |
# =====================================
|
|
|
|
| 13 |
-f https://download.pytorch.org/whl/torch_stable.html
|
| 14 |
|
| 15 |
transformers==4.39.3
|
|
|
|
| 16 |
|
| 17 |
# =====================================
|
| 18 |
# Processamento de imagem
|
| 19 |
# =====================================
|
| 20 |
pillow==10.3.0
|
|
|
|
|
|
|
| 21 |
numpy==1.26.4
|
| 22 |
|
| 23 |
# =====================================
|