144 lines
5.0 KiB
Python
144 lines
5.0 KiB
Python
import sys
|
|
import os
|
|
import cv2
|
|
import json
|
|
import base64
|
|
|
|
from matplotlib import pyplot as plt
|
|
|
|
class FaceClass(object):
|
|
|
|
imgs = {}
|
|
encs = {}
|
|
faces = {}
|
|
visual = 0
|
|
imfiles = []
|
|
imnames = {}
|
|
errstr = ""
|
|
tree = { "device1":"NA", "device2":"NA", "threshold":380, "device1_qual":0.5, "device2_qual":0.5, "score":0 }
|
|
|
|
|
|
# Prep tasks
|
|
def init(self):
|
|
return None
|
|
|
|
|
|
# Load a pics using the device label
|
|
def load1(self, name,fname):
|
|
if not os.path.isfile(fname):
|
|
return '{ "status":442565, "remark":"file name not found", "guilty_param":"fname", "guilty_value":"%s" }' % (fname)
|
|
self.imgs[name] = cv2.imread(fname)
|
|
print(" Loaded %s from file %s" % (name, fname))
|
|
return '{ "status":0, "remark":"OK", "name":"%s", "fname":"%s" }' % (name,fname)
|
|
|
|
|
|
def load2(self, name1,fname1,name2,fname2):
|
|
print("FaceClass loading files ....................... ")
|
|
if not os.path.isfile(fname1):
|
|
print("Cant access file ",fname1)
|
|
return '{ "status":442566, "remark":"file not found", "guilty_param":"fname", "guilty_value":"%s" }' % (fname1)
|
|
if not os.path.isfile(fname2):
|
|
print("Cant access file ",fname2)
|
|
return '{ "status":442567, "remark":"file not found", "guilty_param":"fname", "guilty_value":"%s" }' % (fname2)
|
|
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("FaceClass: Loaded %s from file %s" % (name1, fname1))
|
|
print("FaceClass: Loaded %s from file %s" % (name2, fname2))
|
|
return '{ "status":0, "remark":"OK", "name1":"%s", "fname1":"%s", "name2":"%s", "fname2":"%s" }' % (name1,fname1,name2,fname2)
|
|
|
|
|
|
# @doc draw a box and save with a new name
|
|
def rect(self, name, x1, y1, x2, y2, newname):
|
|
self.imgs[newname] = cv2.rectangle(self.imgs[name],(x1,y1),(x2,y2), (0,255,0), 4)
|
|
|
|
# @doc crop an image and save with a new name
|
|
def crop(self, name, x1, y1, x2, y2, newname):
|
|
print(x1,y1,x2,y2)
|
|
self.imgs[newname] = self.imgs[name][x1:x2, y1:y2]
|
|
|
|
# Load the config
|
|
def init(self):
|
|
with open("/etc/ukdi.json","r") as f:
|
|
self.conf = json.loads(f.read())
|
|
|
|
# Find all faces
|
|
def get_faces(self,name):
|
|
return '{ "status":88241, "remark":"override this!" }'
|
|
|
|
# Find best face
|
|
def get_best_face(self):
|
|
return '{ "status":88242, "remark":"override this!" }'
|
|
|
|
|
|
# @doc find the biggest face in the named image
|
|
def get_ideal(self, name):
|
|
self.boxes = []
|
|
self.get_faces(name)
|
|
found = -1
|
|
ix = 0
|
|
biggest = -1
|
|
for b in self.boxes:
|
|
print(json.dumps(b))
|
|
area = b[3] * b[2]
|
|
print(b,area)
|
|
if area > biggest:
|
|
found = ix
|
|
biggest = area
|
|
# box returns (left,top,right,bottom) - ffs
|
|
#self.crop(name,b[1],b[0],b[3],b[2],"_crop")
|
|
#self.rect(name,b[0],b[1],b[2],b[3],"_rect")
|
|
# rect expects x1,y1,x2,y2
|
|
self.rect(name,b[0],b[1],b[1]+b[3],b[0]+b[2],"_rect")
|
|
ix+=1
|
|
if found < 0:
|
|
return '{ "status":8572421, "remark":"no ideal face", "guilty_param":"name", "guilty_value":"%s" }' % name
|
|
return '{ "status":0, "remark":"OK", "faces":%d, "ideal_ix":%s, "ideal_area":%d, "boxes":%s }' % (len(self.boxes), found, biggest, json.dumps(self.boxes))
|
|
|
|
|
|
# return a base64 version of the pic in memory
|
|
def dump64(self,which,format="png"):
|
|
if format=="jpg":
|
|
_ , im_arr = cv2.imencode('.jpg', self.imgs[which])
|
|
img_as_txt = base64.b64encode(im_arr)
|
|
return b'data:image/jpeg;base64,'+img_as_txt
|
|
_ , im_arr = cv2.imencode('.png', self.imgs[which])
|
|
img_as_txt = base64.b64encode(im_arr)
|
|
return b'data:image/png;base64, '+img_as_txt
|
|
|
|
# Find landmarks
|
|
def get_landmarks(self):
|
|
return '{ "status":88243, "remark":"override this!" }'
|
|
|
|
# Find metadata (age etc)
|
|
def get_metadata(self,name):
|
|
return '{ "status":88244, "remark":"override this!" }'
|
|
|
|
# Match two faces
|
|
def process(self,name1,name2):
|
|
return '{ "status":88245, "remark":"override this!" }'
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# tests
|
|
# big/clear big/clear small/side small/clear obscured ig card IR cam some rotated
|
|
picfiles = [ "gerrard.jpg", "ronaldo.jpg", "messi1.jpg", "messi2.jpg", "ox_matt.png", "ox_govid.jpg", "cg_ir.jpg", "fivepeople.jpg" ]
|
|
|
|
# Test the dlib image detector
|
|
d = FaceClass()
|
|
|
|
# quick test
|
|
if sys.argv[1]=="quick":
|
|
d.visual = 1
|
|
d.load("messi1","messi2","testimg/messi1.jpg","testimg/messi2.jpg")
|
|
sys.exit(0)
|