yoloserv/src/paravisionx.py

104 lines
4.0 KiB
Python

#
# Paravision based face matcher
#
import json
import sys
### export PYTHONPATH=/wherever/yoloserv/modules ... as long as "paravision/.../" is in there
from paravision.recognition.exceptions import ParavisionException
from paravision.recognition.engine import Engine
from paravision.recognition.sdk import SDK
import paravision.recognition.utils as pru
#from openvino.inference_engine import Engineq
#from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace, DeepID
from faceclass import FaceClass
class Paravision(FaceClass):
def init(self,backend=None,model=None):
print("@@@ initialising paravision")
try:
self.sdk = SDK(engine=Engine.AUTO)
except ParavisionException:
pass
# @doc find all the faces in the named image
def detect(self, name):
boxes = []
self.boxes = []
print("** face_recognition::detect ... %s" % name)
try:
# Get all faces from images with qualities, landmarks, and embeddings
res = self.sdk.get_faces([self.imgs[name]])
boxes = res.faces
for b in boxes:
b = b.bounding_box
#print(b)
self.boxes.append((int(b.origin.x),int(b.origin.y),int(b.origin.x+b.width),int(b.origin.y+b.height)))
print("found %d boxes for %s" % (len(self.boxes), name) )
except Exception as ex:
self.errstr = "image processing exception at get_faces: "+str(ex)
return '{ "status":222310, "remark":"image processing exception", "guilty_param":"error", "guilty_value":"%s" }' % str(ex)
return '{ "status":0, "remark":"OK", "faces":%d, "boxes":%s }' % (len(self.boxes), json.dumps(self.boxes))
# @doc compare two named images, previously loaded
def compare(self, name1, name2):
print("** face_recognition::compare ... %s vs %s" % (name1,name2))
self.encs[name1] = self.sdk.get_embedding_from_prepared_image(self.imgs[name1])
self.encs[name2] = self.sdk.get_embedding_from_prepared_image(self.imgs[name2])
#if self.encs[name1]==[]:
# return '{ "status":14330, "remark":"could not encode image", "guilty_param":"img1", "guilty_value":"%s" }' % name1
#if self.encs[name2]==[]:
# return '{ "status":14331, "remark":"could not encode image", "guilty_param":"img2", "guilty_value":"%s" }' % name2
print(self.encs.keys())
print(self.encs[name1])
print(self.encs[name2])
try:
res = self.sdk.get_match_score(self.model, other.model)
print("Match is ",res)
#self.match_score = 1000 * (1 - face_recognition.face_distance([self.encs[name1][0]], self.encs[name2][0]))
#print("Score is ",self.match_score)
# Create .json
self.tree["score"] = self.match_score[0]
except Exception as ex:
print("** paravision::compare exception ... " + str(ex) )
self.errstr = "image comparison exception at compute_scores: "+str(ex)
return '{ "status":332410, "remark":"%s" }' % self.errstr
return '{ "status":0, "remark":"OK", "score":%d }' % self.match_score[0]
if __name__ == '__main__':
d = Paravision()
d.init()
if sys.argv[1]=="messia":
jsonstr = d.load1("pic1", "testimg/messi4.jpg")
print(jsonstr)
jsonstr = d.detect("pic1")
print(jsonstr)
if sys.argv[1]=="test":
d.load1("pic1", "testimg/ox.jpg")
d.detect("pic1")
if sys.argv[1]=="kiosk":
jsonstr = d.crowd_vs_govid("pic1", "testimg/ox.jpg", 0, "pic2", "testimg/ox_govid.jpg", 0.25)
print(jsonstr)
if sys.argv[1]=="messi":
jsonstr = d.crowd_vs_govid("pic1", "testimg/messi4.jpg", 0, "pic2", "testimg/messi2.jpg", 0)
print(jsonstr)
if sys.argv[1]=="maiden":
jsonstr = d.crowd_vs_govid("pic1", "testimg/ironmaiden.jpg", 0, "pic2", "testimg/davemurray.jpg", 0)
print(jsonstr)