fixed multi-faces recognition
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
|||||||
|
import cv2
|
||||||
|
|
||||||
|
class Face():
|
||||||
|
|
||||||
|
def __init__(self, data, img, name):
|
||||||
|
self.data = data
|
||||||
|
self.img = img
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
self.feature = None
|
||||||
|
|
||||||
|
self.l2 = None
|
||||||
|
self.cosine = None
|
||||||
|
|
||||||
|
def set_feature(self, fetaure):
|
||||||
|
self.feature = feature
|
||||||
|
|
||||||
|
def set_cosine(self, cosine:float):
|
||||||
|
self.cosine = cosine
|
||||||
|
|
||||||
|
def set_l2(self, l2:float):
|
||||||
|
self.l2 = l2
|
||||||
|
|
||||||
|
def save(self, path:str, extension:str = "png"):
|
||||||
|
cv2.imwrite(f"{path}/{self.name}.{extension}", self.img)
|
||||||
+10
-5
@@ -1,16 +1,21 @@
|
|||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
from recognizer import Recognizer
|
from recognizer import Recognizer
|
||||||
|
from face import *
|
||||||
|
|
||||||
def extract_faces(faces:list, img:list, filename:str):
|
def extract_faces(faces_data:list, img:list, filename:str):
|
||||||
|
|
||||||
recognizer = Recognizer.get()
|
recognizer = Recognizer.get()
|
||||||
|
res = []
|
||||||
|
for i in range(1, len(faces_data[1])):
|
||||||
|
face_data = faces_data[1][i]
|
||||||
|
|
||||||
for i in range(1, len(faces)):
|
croped_img = recognizer.alignCrop(img, face_data)
|
||||||
face = faces[i]
|
face = Face(face_data, croped_img, f"{filename}_{i}")
|
||||||
|
|
||||||
croped_img = recognizer.alignCrop(img, face[0])
|
res.append(face)
|
||||||
cv2.imwrite(f"./data/extracted/{filename}_{i}.png", croped_img)
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def is_same_face(
|
def is_same_face(
|
||||||
|
|||||||
+8
-3
@@ -9,6 +9,7 @@ from face_recognition import *
|
|||||||
files = list_all_files("./data/raw", ["jpg", "webp"])
|
files = list_all_files("./data/raw", ["jpg", "webp"])
|
||||||
|
|
||||||
# process each files
|
# process each files
|
||||||
|
faces = []
|
||||||
for file_path in files:
|
for file_path in files:
|
||||||
|
|
||||||
filename = file_path.split("/")[-1].split(".")[0]
|
filename = file_path.split("/")[-1].split(".")[0]
|
||||||
@@ -18,12 +19,16 @@ for file_path in files:
|
|||||||
if img is None:
|
if img is None:
|
||||||
print(f"ERROR: Could not read image at {file_path}")
|
print(f"ERROR: Could not read image at {file_path}")
|
||||||
continue
|
continue
|
||||||
cv2.imshow("image1", img)
|
|
||||||
|
|
||||||
# detect faces file
|
# detect faces file
|
||||||
faces = detect_face_in_image(img, scale=1)
|
faces_data = detect_face_in_image(img, scale=1)
|
||||||
|
|
||||||
|
print("faces_data: ", faces_data[1])
|
||||||
# extract faces from file
|
# extract faces from file
|
||||||
extract_faces(faces, img, filename)
|
faces += extract_faces(faces_data, img, filename)
|
||||||
|
|
||||||
|
# classify each faces
|
||||||
|
for face in faces:
|
||||||
|
face.save("./data/extracted")
|
||||||
|
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
Reference in New Issue
Block a user