first pass of consolidating search in DBox for existing person, and then using the results to add override force match to that person, and WORKING version of adding refimg to existing person too. Still does not kick off new AI scan at this point, and still need to re-format dbox to be easier to use and code for resetting DB contents, rescaning files from scratch and matching overrides back

This commit is contained in:
2022-07-10 15:21:31 +10:00
parent 1507addf38
commit 6b7694f382
5 changed files with 152 additions and 64 deletions

View File

@@ -13,6 +13,10 @@ from face import Face, FaceRefimgLink, FaceOverrideType, FaceNoMatchOverride, Fa
import os
import json
import time
from PIL import Image
import base64
from io import BytesIO
import os.path
# pylint: disable=no-member
@@ -86,6 +90,32 @@ class PersonForm(FlaskForm):
save = SubmitField('Save' )
delete = SubmitField('Delete' )
################################################################################
# Helper functions
# AddRefimgToPerson( filename, person )
################################################################################
def AddRefimgToPerson( filename, person ):
refimg = Refimg( fname=os.path.basename( filename ) )
try:
refimg.thumbnail, refimg.orig_w, refimg.orig_h = GenThumb( filename )
settings = Settings.query.first()
model=AIModel.query.get(settings.default_refimg_model)
refimg.face, face_locn = GenFace( filename, model=model.name )
refimg.face_locn = json.dumps(face_locn)
refimg.model_used = settings.default_refimg_model
refimg.created_on = time.time()
os.remove(filename)
person.refimg.append(refimg)
db.session.add(person)
db.session.add(refimg)
db.session.commit()
st.SetMessage( f"Associated new Refimg ({refimg.fname}) with person: {person.tag}" )
except SQLAlchemyError as e:
st.SetMessage( f"<b>Failed to add Refimg:</b>&nbsp;{e.orig}", "danger" )
except Exception as e:
st.SetMessage( f"<b>Failed to modify Refimg:</b>&nbsp;{e}", "danger" )
return
################################################################################
# Routes for person data
#
@@ -197,33 +227,19 @@ def add_refimg():
person = Person.query.get(request.form['person_id']);
if not person:
raise Exception("could not find person to add reference image too!")
f=request.files['refimg_file']
refimg = Refimg( fname=f.filename )
try:
# save the actual uploaded image to reference_images/
f=request.files['refimg_file']
fname=secure_filename(f.filename)
if fname == "":
raise Exception("invalid filename")
fname = f"/tmp/{fname}"
f.save( fname )
refimg.thumbnail, refimg.orig_w, refimg.orig_h = GenThumb( fname )
settings = Settings.query.first()
model=AIModel.query.get(settings.default_refimg_model)
refimg.face, face_locn = GenFace( fname, model=model.name )
refimg.face_locn = json.dumps(face_locn)
refimg.model_used = settings.default_refimg_model
refimg.created_on = time.time()
os.remove(fname)
person.refimg.append(refimg)
db.session.add(person)
db.session.add(refimg)
db.session.commit()
st.SetMessage( f"Associated new Refimg ({refimg.fname}) with person: {person.tag}" )
except SQLAlchemyError as e:
st.SetMessage( f"<b>Failed to add Refimg:</b>&nbsp;{e.orig}", "danger" )
except Exception as e:
st.SetMessage( f"<b>Failed to modify Refimg:</b>&nbsp;{e}", "danger" )
st.SetMessage( f"<b>Failed to load reference image:</b>&nbsp;{e}", "danger" )
AddRefimgToPerson( fname, person )
return redirect( url_for( 'person', id=person.id) )
################################################################################
@@ -245,6 +261,44 @@ def find_persons(who):
return resp
################################################################################
# /add_refimg_to_person/ -> POST
################################################################################
@app.route("/add_refimg_to_person", methods=["POST"])
@login_required
def add_refimg_to_person():
resp={}
f = Face.query.get( request.form['face_id'] )
p = Person.query.get( request.form['person_id'] )
file_eid = request.form['file_eid']
refimg_data = request.form['refimg_data']
# undo the munging sending via http has done
refimg_data=refimg_data.replace(' ', '+' )
print( refimg_data )
# convert b64 encoded to a temp file to process...
bytes_decoded = base64.b64decode(refimg_data)
img = Image.open(BytesIO(bytes_decoded))
out_jpg = img.convert("RGB")
# save file to /tmp/<p.tag>
fname="/tmp/" + p.tag + '.jpg'
out_jpg.save(fname)
# add this fname (of temp refimg) to person
AddRefimgToPerson( fname, p )
# DDP:
# need to create a new job to re-do AI now we have a new refimg in the mix
resp['who']=p.tag
resp['distance']='0.0'
return resp
################################################################################
# /override_force_match -> POST
################################################################################
@@ -254,7 +308,7 @@ def override_force_match():
person_id = request.form['person_id']
p = Person.query.get(person_id);
if not p:
raise Exception("could not find person to add override too!")
raise Exception( f"could not find person (id={person_id}) to add override too!" )
face_id = request.form['face_id']
f = Face.query.get(face_id);