VeuReu commited on
Commit
ea3e9f6
verified
1 Parent(s): 481090f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -10
app.py CHANGED
@@ -6,7 +6,7 @@ from typing import Dict, List, Optional, Tuple, Union
6
  import gradio as gr
7
  import spaces
8
  import torch
9
- import face_recognition
10
  import numpy as np
11
  from PIL import Image
12
  from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration
@@ -15,6 +15,9 @@ MODEL_ID = os.environ.get("MODEL_ID", "BSC-LT/salamandra-7b-vision")
15
  DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
 
 
 
 
18
  _model = None
19
  _processor = None
20
 
@@ -93,18 +96,29 @@ def describe_batch(images: List[Image.Image], context_json: str,
93
  return outputs
94
 
95
 
96
- def face_image_embedding(image: Image.Image) -> List[float]:
97
  try:
98
- face_model = face_recognition
99
- encs = face_model.face_encodings(image)
100
- if encs:
101
- embeddings = [(e / np.linalg.norm(e)).astype(float).tolist() for e in encs]
102
- return embeddings
103
- return None
 
 
 
 
 
 
 
 
 
 
 
 
104
  except Exception as e:
105
  print(f"Fallo embedding cara: {e}")
106
-
107
- return None
108
 
109
 
110
  # ----------------------------- UI & Endpoints --------------------------------
 
6
  import gradio as gr
7
  import spaces
8
  import torch
9
+ from facenet_pytorch import MTCNN, InceptionResnetV1
10
  import numpy as np
11
  from PIL import Image
12
  from transformers import AutoProcessor, LlavaOnevisionForConditionalGeneration
 
15
  DTYPE = torch.float16 if torch.cuda.is_available() else torch.float32
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
 
18
+ mtcnn = MTCNN(image_size=160, margin=0, post_process=True, device=DEVICE)
19
+ facenet = InceptionResnetV1(pretrained='vggface2').eval().to(DEVICE)
20
+
21
  _model = None
22
  _processor = None
23
 
 
96
  return outputs
97
 
98
 
99
+ def face_image_embedding(image: Image.Image) -> List[float] | None:
100
  try:
101
+ # detectar y extraer cara
102
+ face = mtcnn(image)
103
+
104
+ if face is None:
105
+ return None
106
+
107
+ # FaceNet espera tensor shape (1,3,160,160)
108
+ face = face.unsqueeze(0).to(DEVICE)
109
+
110
+ # obtener embedding
111
+ with torch.no_grad():
112
+ emb = facenet(face).cpu().numpy()[0]
113
+
114
+ # normalizar igual que tu c贸digo original
115
+ emb = emb / np.linalg.norm(emb)
116
+
117
+ return emb.astype(float).tolist()
118
+
119
  except Exception as e:
120
  print(f"Fallo embedding cara: {e}")
121
+ return None
 
122
 
123
 
124
  # ----------------------------- UI & Endpoints --------------------------------