From 4266448f68829aa09b11b1034d878988fdae107b Mon Sep 17 00:00:00 2001 From: carl Date: Tue, 22 Oct 2024 22:25:43 -0300 Subject: [PATCH] get paraVISION working again --- src/faceclass.py | 4 ++- src/paravisionx.py | 89 ++++++++++++++++++++++++++++++++-------------- src/yoloserv.py | 8 ++--- 3 files changed, 69 insertions(+), 32 deletions(-) diff --git a/src/faceclass.py b/src/faceclass.py index 9c58697fd..833513386 100644 --- a/src/faceclass.py +++ b/src/faceclass.py @@ -252,7 +252,9 @@ class FaceClass(object): # @doc crop an image, allowing a gutter. def shrink(self, name, skale=0.5): - print ("shrinking ",name) + print ("shrinking ",name,skale) + if skale == 0: + return self.imgs[name] = cv2.resize(self.imgs[name],None,fx=skale,fy=skale) diff --git a/src/paravisionx.py b/src/paravisionx.py index f61eecd19..9d5eb3641 100644 --- a/src/paravisionx.py +++ b/src/paravisionx.py @@ -3,21 +3,32 @@ # import json import sys +import os +import cv2 +import torchvision.transforms ### 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 +from paravision.recognition.engine import Engine import paravision.recognition.utils as pru -#from openvino.inference_engine import Engineq + #from openvino.inference_engine import Engineq #from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace, DeepID from faceclass import FaceClass - +# Paravision is quite unlike all the other open source img recognition packages +# which cause problems when image formats move between the two. +# So this class has quite a few differences. class Paravision(FaceClass): + models = {} + scores = {} + quality = {} + sdk = None + match_score = 0 + def init(self,backend=None,model=None): print("@@@ initialising paravision") try: @@ -26,6 +37,18 @@ class Paravision(FaceClass): pass + # @doc Load a pic using the device label + def load1(self, name,fname): + print(" Loading image '%s' from file %s" % (name, fname)) + if not os.path.isfile(fname): + print(" * file not found: %s" % (fname)) + return '{ "status":442565, "remark":"file name not found", "guilty_param":"fname", "guilty_value":"%s" }' % (fname) + self.files[name] = fname + self.imgs[name] = pru.load_image(fname) + print(" Loaded %s from file %s" % (name, fname)) + return '{ "status":0, "remark":"OK", "name":"%s", "fname":"%s" }' % (name,fname) + + # @doc find all the faces in the named image def detect(self, name): boxes = [] @@ -33,46 +56,58 @@ class Paravision(FaceClass): 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) ) + faces = self.sdk.get_faces([self.imgs[name]], qualities=True, landmarks=True, embeddings=True) + inferences = faces.image_inferences + ix = inferences[0].most_prominent_face_index() + self.models[name] = inferences[0].faces[ix].embedding + self.quality[name] = round(1000*inferences[0].faces[ix].quality) + self.boxes = [ (0,0,0,0) ] 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 This does everything for you. + # If you are smartserv, "crowd" means cam and "govid" means regula pic + def crowd_vs_govid(self, name1,file1,scale1, name2,file2,scale2): + print("##1##") + if self.json2obj(self.load1(name1, file1))["status"] != 0: + return self.jsonx + if self.json2obj(self.detect(name1))["status"] != 0: + return self.jsonx + self.save(name1,"/tmp") + + print("##2##") + if self.json2obj(self.load1(name2, file2))["status"] != 0: + return self.jsonx + self.save(name2,"/tmp") + if self.json2obj(self.detect(name2))["status"]!=0: + return self.jsonx + self.save(name2,"/tmp") + + print("##R##") + jsonstr = self.compare(name1,name2) + print(jsonstr) + return jsonstr + # @doc compare two named images, previously loaded - def compare(self, name1, name2): + 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) + res = self.sdk.get_match_score(self.models[name1], self.models[name2]) 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] + self.match_score = res 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] + return '{ "status":0, "remark":"OK", "score":%d }' % self.match_score + + def scores(self): + return '{ "status":0, "remark":"OK", "score":%d }' % self.match_score diff --git a/src/yoloserv.py b/src/yoloserv.py index bb7c9ed00..9b6fb709b 100644 --- a/src/yoloserv.py +++ b/src/yoloserv.py @@ -240,12 +240,12 @@ class yoloserv(object): # Match faces together @cherrypy.expose - def svc_match_faces(self,dev1,fil1,scl1,dev2,fil2,scl2): - jsonstr = self.facematcher.crowd_vs_govid(dev1,self.conf["yolo_indir"]+fil1,scl1, dev2,self.conf["yolo_outdir"]+fil2,scl2) + def svc_match_faces(self,dev1,fil1,scl1s,dev2,fil2,scl2s): + scl1 = float(scl1s) + scl2 = float(scl2s) + jsonstr = self.facematcher.crowd_vs_govid(dev1,self.conf["yolo_indir"]+fil1,scl1, dev2,self.conf["yolo_indir"]+fil2,scl2) obj = self.json2obj(jsonstr) return jsonstr - if obj.status > 0: - return jsonstr def json2obj(self,jsonx): return json.loads(jsonx)