Spaces:
Sleeping
Sleeping
File size: 5,649 Bytes
68d34d0 |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import sys
sys.path.append("..")
from deepface import DeepFace
from deepface.modules import modeling
from deepface.detectors import DetectorWrapper
from detectron2.data.detection_utils import pil_image_to_numpy,convert_PIL_to_numpy
from detectron2.utils.visualizer import ColorMode, Visualizer
import numpy as np
import time
from PIL import Image
# print(modeling.model_obj)
# print(DetectorWrapper.face_detector_obj)
class FaceAlgo:
need_save_image = False
backends = [
'opencv',
'ssd',
'dlib',
'mtcnn',
'fastmtcnn',
'retinaface',
'mediapipe',
'yolov8',
'yunet',
'centerface',
]
recognition_models = [
"VGG-Face",
"Facenet",
"Facenet512",
"OpenFace",
"DeepFace",
"DeepID",
"ArcFace",
"Dlib",
"SFace",
"GhostFaceNet",
]
distance_metric = [
'cosine',
'euclidean',
'euclidean_l2',
]
def __init__(self,need_save_image=False):
self.need_save_image = need_save_image
def np_to_pil(self,np_img):
# 转换 BGR 到 RGB
# rgb_image = np_img[:, :, ::-1]
np_img = (np_img * 255).astype(np.uint8)
# print(rgb_image.shape)
#convert numpy array to PIL Image
return Image.fromarray(np_img)
def draw_face_box(self,image,face_areas):
if not isinstance(image,np.ndarray):
image = convert_PIL_to_numpy(image,format=None)
image = image[:, :, ::-1]
visualizer = Visualizer(image,instance_mode=ColorMode)
for area in face_areas:
x = area['x']
y = area['y']
w = area['w']
h = area['h']
visualizer.draw_box((x,y,x+w,y+h),edge_color="r")
visualized_image = visualizer.get_output().get_image()
# [:, :, ::-1]
return Image.fromarray(visualized_image)
def predict(self,pil_image,pil_image1=None,algo_type="detect"):
image = pil_image_to_numpy(pil_image)
if pil_image1 is not None:
image1 = pil_image_to_numpy(pil_image1)
if algo_type == "detect":
return self.detect(image)
elif algo_type =="recognize":
return self.recognition(image)
elif algo_type =="compare":
return self.verify(image,image1)
elif algo_type =="feature":
return self.embeddings(image)
elif algo_type =="attr":
return self.analysis(image)
def verify(self,a,b):
#face verification
obj = DeepFace.verify(
img1_path = a,
img2_path = b,
detector_backend = self.backends[0],
distance_metric = self.distance_metric[0],
)
face_area1 = [obj['facial_areas']['img1']]
face1 = self.draw_face_box(a,face_area1)
face_area2 = [obj['facial_areas']['img2']]
face2 = self.draw_face_box(b,face_area2)
current_timestamp = time.time()
if self.need_save_image:
face1.save(f"{current_timestamp}_0.png")
face2.save(f"{current_timestamp}_1.png")
return obj,[face1,face2]
def recognition(self,a):
#face recognition
dfs = DeepFace.find(
img_path = a,
db_path = "./test/",
detector_backend = self.backends[1],
distance_metric = self.distance_metric[0],
)
json_list = [df.to_json(orient='records') for df in dfs]
top1_path = dfs[0].at[0, 'identity']
top1_pil = Image.open(top1_path)
return json_list,[top1_pil]
def embeddings(self,a):
#embeddings
embedding_objs = DeepFace.represent(
img_path = a,
detector_backend = self.backends[5],
)
face_areas = []
for obj in embedding_objs:
face_areas.append(obj['facial_area'])
face = self.draw_face_box(a,face_areas)
current_timestamp = time.time()
if self.need_save_image:
face.save(f"{current_timestamp}.png")
return embedding_objs,[face]
def analysis(self,a):
#facial analysis
demographies = DeepFace.analyze(
img_path = a,
detector_backend = self.backends[5],
)
face_areas = []
for obj in demographies:
face_areas.append(obj['region'])
face = self.draw_face_box(a,face_areas)
current_timestamp = time.time()
if self.need_save_image:
face.save(f"{current_timestamp}.png")
return demographies,[face]
def detect(self,a):
#face detection and alignment
face_objs = DeepFace.extract_faces(
img_path = a,
detector_backend = self.backends[4],
)
ret = []
faces = []
# 获取当前时间戳
current_timestamp = time.time()
for i,obj in enumerate(face_objs):
print(obj['face'])
face_image = self.np_to_pil(obj['face'])
if self.need_save_image:
face_image.save(f"{current_timestamp}_{i}.png")
item = {'facial_area':obj['facial_area'],'confidence':obj['confidence']}
ret.append(item)
faces.append(face_image)
return ret,faces
# if __name__ == "__main__":
# m = FaceAlgo(need_save_image=True) # pragma: no cover
# image = Image.open("./test/face1.jpeg")
# image1 = Image.open("./test/face2.jpeg")
# out = m.predict(image,image1,algo_type="recognize")
# print("-----------------",out) |