fine tune; get it to work

This commit is contained in:
carl 2024-01-22 17:50:57 -04:00
parent 88ddc44313
commit 10798e3162
3 changed files with 57 additions and 28 deletions

View File

@ -20,15 +20,13 @@ from faceclass import FaceClass
class Deepfacex(FaceClass):
img1_path = ""
img2_path = ""
backend = "ssd"
imgs = {}
faces = {}
fn512_model = None
resnet_model = None
visual = 0
tree = { "img1_faces":0, "img1_qual":0, "img2_faces":0, "img2_qual":0, "threshold":0, "score":0 }
def init(self, backend, model):
print("Loading models...")
@ -53,6 +51,7 @@ class Deepfacex(FaceClass):
detector = dlib.get_frontal_face_detector()
#img = dlib.load_rgb_image(fname)
dets = detector(self.imgs[name], UPSAMPLE)
self.tree["img1_faces"] = len(dets)
print("Number of faces detected in %s: %d" % (name, len(dets)))
for i, d in enumerate(dets):
print("Detection %d: Left: %d Top: %d Right: %d Bottom: %d" % (i, d.left(), d.top(), d.right(), d.bottom() ) )
@ -64,30 +63,47 @@ class Deepfacex(FaceClass):
#img = dlib.load_rgb_image(fname)
dets, scores, idx = detector.run(self.imgs[name], UPSAMPLE, -1)
print("Number of faces detected: %d" % len(dets))
for i, d in enumerate(dets):
print("Detection %d: Score: %d Type: %d Left: %d Top: %d Right: %d Bottom: %d" % (i, scores[i], idx[i], d.left(), d.top(), d.right(), d.bottom() ) )
# Load two pics using their device labels
def loads(self, dev1, dev2, id_image_filepath, photo_image_filepath):
self.load(dev1,id_image_filepath)
self.load(dev2,photo_image_filepath)
if len(dets)>0:
print("Face scores = ",scores[0])
return dets, scores
# Detects all the faces
def detect_all(self, name):
def detect_all(self, name, fname):
print("Finding faces in %s: %s" % (name,fname))
detector = FaceDetector.build_model(self.backend) #set opencv, ssd, dlib, mtcnn or retinaface
self.faces[name] = FaceDetector.detect_faces(detector, self.backend, self.imgs[name])
print(" Found %d faces for %s" % (len(self.faces[name]), name))
self.faces[name] = FaceDetector.detect_faces(detector, self.backend, fname)
print(" Found %d faces for %s" % (len(self.faces), name))
return len(self.faces[name])
# Compare the two pics
def process(self,impath1,impath2):
print("Matching %s vs %s" % (impath1,impath2))
verification = DeepFace.verify(img1_path = impath1, img2_path = impath2, model_name=self.model,\
def process(self):
print("Matching %s vs %s" % (self.imfiles[0], self.imfiles[1]))
verification = DeepFace.verify(img1_path = self.imfiles[0], img2_path = self.imfiles[1], model_name=self.model,\
detector_backend=self.backend, distance_metric="euclidean", enforce_detection=False, align=True, normalization="base")
return json.dumps(verification)
return verification
def facematch(self):
dets1, scores1 = self.dlib_detector2("localcam")
self.tree["img1_faces"] = len(dets1)
if len(dets1) > 0:
self.tree["img1_qual"] = scores1[0]
dets2, scores2 = self.dlib_detector2("regula")
self.tree["img2_faces"] = len(dets2)
if len(dets2) > 0:
self.tree["img2_qual"] = scores2[0]
if len(dets1) < 1:
return '{ "status":787101, "remark":"no faces in cam image", "data":%s }' % (json.dumps(self.tree))
if len(dets2) < 1:
return '{ "status":787102, "remark":"no faces in ID image", "data":%s }' % (json.dumps(self.tree))
verif = d.process()
self.tree["score"] = verif["distance"]
self.tree["threshold"] = verif["threshold"]
return '{ "status":0, "remark":"OK", "data":%s }' % (json.dumps(self.tree))
def analyse(self):
@ -103,7 +119,12 @@ if __name__ == '__main__':
# Test the dlib image detector
d = Deepfacex()
d.init("dlib","Facenet512")
d.init("dlib","SFace")
# kiosk test
if sys.argv[1]=="kiosk":
print(d.load("localcam","regula","/tmp/localcam.png","/tmp/regula/Portrait_0.jpg"))
print (d.facematch())
# quick test
if sys.argv[1]=="quick":
@ -176,4 +197,4 @@ if __name__ == '__main__':
d.clear()
n+=1
if n > 4000:
sys.exit(0)
sys.exit(0)

View File

@ -8,7 +8,8 @@ class FaceClass(object):
imgs = {}
faces = {}
visual = 0
imfiles = []
imnames = {}
# Prep tasks
@ -22,18 +23,25 @@ class FaceClass(object):
def load(self, name1,name2,fname1,fname2):
print("FaceClass loading files ....................... ")
if not os.path.isfile(fname1):
print("Cant access file ",fname1)
return False
if not os.path.isfile(fname2):
print("Cant access file ",fname2)
return False
self.imfiles.append(fname1)
self.imfiles.append(fname2)
self.imnames[name1] = fname1
self.imnames[name2] = fname2
self.imgs[name1] = cv2.imread(fname1)
self.imgs[name2] = cv2.imread(fname2)
if self.visual:
p1 = plt.imshow(name1, self.imgs[name1])
p2.imshow(name2, self.imgs[name2])
p1.show()
#print(" Loaded %s from file %s" % (name, fname))
return True
print("FaceClass: Loaded %s from file %s" % (name1, fname1))
return 1
def box(self, name, x, y, w, h):

View File

@ -63,7 +63,7 @@ class yoloserv(object):
print("Loading deepface facematch...")
from deepfacex import Deepfacex
self.facematcher = Deepfacex()
self.facematcher.init()
self.facematcher.init("dlib","SFace")
if "face_recognition" in self.devices:
print("Loading deepface facematch...")
from face_recognition import FaceRecognition
@ -193,12 +193,12 @@ class yoloserv(object):
return '{ "status":0, "remark":"OK", "data":{} }'
status = self.facematcher.load(dev1, dev2, img1, img2)
if status is not None:
if not status:
return '{ "status":777242, "remark":"face loading failed", "guilty_param":"facematch", "guilty_value":"%s" }' % (status)
status = self.facematcher.analyse()
if status is not None:
return '{ "status":777242, "remark":"face loading failed", "guilty_param":"facematch", "guilty_value":"%s" }' % (status)
#status = self.facematcher.analyse()
#if status is not None:
# return '{ "status":777242, "remark":"face loading failed", "guilty_param":"facematch", "guilty_value":"%s" }' % (status)
#status = self.facematcher.get_faces()
#if status is not None: